zoukankan      html  css  js  c++  java
  • DES/3DES/AES 三种对称加密算法实现

    1. 简单介绍

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。

    2. 对称加密

    2.1 介绍

    对称密码算法是当今应用范围最广,使用频率最高的加密算法。它不仅应用于软件行业,在硬件行业同样流行。各种基础设施凡是涉及到安全需求,都会优先考虑对称加密算法。对称密码算法的加密密钥和解密密钥相同,对于大多数对称密码算法,加解密过程互逆。

    • 特点:算法公开、计算量小、加密速度快、加密效率高。

    • 弱点:双方都使用同样密钥,安全性得不到保证。

    对称密码有流密码和分组密码两种,但是现在普遍使用的是分组密码:

    2.2 分组密码工作模式

    • ECB:电子密码本(最常用的,每次加密均产生独立的密文分组,并且对其他的密文分组不会产生影响,也就是相同的明文加密后产生相同的密文)
    • CBC:密文链接(常用的,明文加密前需要先和前面的密文进行异或运算,也就是相同的明文加密后产生不同的密文)
    • CFB:密文反馈
    • OFB:输出反馈
    • CTR:计数器

    2.3 常用对称密码:

    • DES(Data Encryption Standard,数据加密标准)
    • 3DES(Triple DES、DESede,进行了三重DES加密的算法)
    • AES(Advanced Encryption Standard,高级数据加密标准,AES算法可以有效抵制针对DES的攻击算法

    3. DES / 3DES / AES 三种算法实现

    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    
    import com.newland.csf.common.business.IBusinessComponent;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.nio.charset.StandardCharsets;
    
    /**
     * 
     * 
     * 
     */
    public class TripleDes  {
    
        //指定要使用的算法  DES / 3DES / AES 分别对应的 值为: DES / DESede / AES
    	public static final String ALGORITHM_3DES = "DESede";
    
    	/**
    	 * 解密算法
    	 * @param hexString 密文手机号
    	 * @param skString	密钥
    	 * @return
    	 * @throws Exception
    	 */
    	public static String tripleDesDecrypt(String skString, String hexString) throws Exception {
    		SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
    		byte[] input = fromHexString(hexString);
    		byte[] output = tripleDesDecryptBytes(secretKey, input);
    		return new String(output, StandardCharsets.UTF_8);
    	}
    
    	/**
    	 * 加密算法
    	 * @param hexString 明文手机号
    	 * @param skString 密钥
    	 * @return
    	 * @throws Exception
    	 */
    	public static String tripleDesEncrypt(String skString, String hexString) throws Exception {
    		SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
    		byte[] output = tripleDesEncryptBytes(secretKey, hexString.getBytes(StandardCharsets.UTF_8));
    		return bytes2Hex(output, false);
    	}
    
    	public static String bytes2Hex(byte[] bytes, boolean upperCase) {
    		if (bytes == null || bytes.length <= 0) {
    			return "";
    		}
    		StringBuilder sb = new StringBuilder();
    		for (byte b : bytes) {
    			sb.append(String.format("%02x", b));
    		}
    		return upperCase ? sb.toString().toUpperCase() : sb.toString();
    	}
    
    	public static byte[] fromHexString(final String hexString) {
    		if ((hexString.length() % 2) != 0) {
    			throw new IllegalArgumentException(
    					"hexString.length not is an even number");
    		}
    
    		final byte[] result = new byte[hexString.length() / 2];
    		final char[] enc = hexString.toCharArray();
    		StringBuilder sb = new StringBuilder(2);
    		for (int i = 0; i < enc.length; i += 2) {
    			sb.delete(0, sb.length());
    			sb.append(enc[i]).append(enc[i + 1]);
    			result[i / 2] = (byte) Integer.parseInt(sb.toString(), 16);
    		}
    		return result;
    	}
    
    	public static byte[] tripleDesEncryptBytes(SecretKey secretKey, byte[] src) throws Exception {
    		Cipher c1 = Cipher.getInstance(ALGORITHM_3DES);
    		c1.init(Cipher.ENCRYPT_MODE, secretKey);
    		return c1.doFinal(src);
    	}
    
    	public static byte[] tripleDesDecryptBytes(SecretKey secretKey, byte[] src) throws Exception {
    		Cipher c1 = Cipher.getInstance(ALGORITHM_3DES);
    		c1.init(Cipher.DECRYPT_MODE, secretKey);
    		return c1.doFinal(src);
    	}
    
        /**
         * 加密文件
         * @param skString
         * @param srcFilePath
         * @param desFilePath
         * @throws Exception
         */
        public static void tripleDesEncryptFile(String skString,String srcFilePath,String desFilePath) throws Exception {
            Cipher cipher = Cipher.getInstance(ALGORITHM_3DES);
            SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            writeFile(cipher,srcFilePath,desFilePath);
        }
    
        /**
         * 解密文件
         * @param skString
         * @param srcFilePath
         * @param desFilePath
         * @throws Exception
         */
        public static void tripleDesDecryptFile(String skString,String srcFilePath,String desFilePath) throws Exception {
            Cipher cipher = Cipher.getInstance(ALGORITHM_3DES);
            SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            writeFile(cipher,srcFilePath,desFilePath);
        }
    
        private static void writeFile(Cipher cipher,String srcFilePath,String desFilePath) throws Exception{
            byte[] buff = new byte[512];
            byte[] temp = null;
            int len = 0;
            try (FileInputStream fis = new FileInputStream(new File(srcFilePath));
                 FileOutputStream fos = new FileOutputStream(new File(desFilePath))) {
                while ((len = fis.read(buff)) > 0) {
                    temp = cipher.update(buff, 0, len);
                    fos.write(temp);
                }
                temp = cipher.doFinal();
                if (temp != null) {
                    fos.write(temp);
                }
            }
        }
    }
    
    

    本文由AnonyStar 发布,可转载但需声明原文出处。
    仰慕「优雅编码的艺术」 坚信熟能生巧,努力改变人生
    欢迎关注微信公账号 :云栖简码 获取更多优质文章
    更多文章关注笔者博客 :云栖简码

  • 相关阅读:
    嵌入式交叉编译环境的搭建
    linux驱动模块编写规范以及Makefiel文件的编写规范
    socket通信
    傀儡进程脱壳三步曲
    Thymeleaf 学习笔记-实例demo(中文教程)
    IntelliJ IDEA 快捷键
    github团队协作教程
    thymeleaf 学习笔记-基础篇(中文教程)
    二维码的生成
    .Net Core Web Api实践(四)填坑连接Redis时Timeout performing EVAL
  • 原文地址:https://www.cnblogs.com/i-code/p/13095884.html
Copyright © 2011-2022 走看看