zoukankan      html  css  js  c++  java
  • Java对称加密算法

    一、对称加密算法概念

    • 加密密钥和解密密钥相同,大部分算法加密揭秘过程互逆。

    • 特点:算法公开、(相比非对称加密)计算量小、加密速度快、效率高。

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

    二、常见对称加密算法

    1、DES
    已破解,不再安全,基本没有企业在用了
    是对称加密算法的基石,具有学习价值
    密钥长度56(JDK)、56/64(BC)


    2、DESede(三重DES)
    早于AES出现来替代DES
    计算密钥时间太长、加密效率不高,所以也基本上不用
    密钥长度112/168(JDK)、128/192(BC)


    3、AES
    最常用的对称加密算法
    密钥建立时间短、灵敏性好、内存需求低(不管怎样,反正就是好)
    实际使用中,使用工作模式为CTR(最好用BC去实现),此工作模式需要引入IV参数(16位的字节数组)
    密钥长度128/192/256,其中192与256需要配置无政策限制权限文件(JDK6)
    填充模式最常用的两种PKCS5Padding和PKCS7Padding,其中后者只有BC独有。


    4、IDEA
    常用的电子邮件加密算法
    工作模式只有ECB
    密钥长度128位


    5、PBE
    综合了消息摘要算法和对称加密算法,最常见的是PBEWithMD5AndDES
    工作模式只有CBC(已丧失安全性,不推荐使用),所以PBE也不推荐使用了

    三、JDK版算法调用模板

    1. 生成密钥

    //KeyGenerator,密钥生成器
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");//算法:DES,DESede,AES
    
    //初始化密钥生成器
    keyGen.init(56); //各算法密钥长度不同,参见说明
    
    //生成密钥
    SecretKey secretKey = keyGen.generateKey();
    
    //生产字节码数据
    byte[] key = secretKey.getEncoded();

    说明:
    1.通过「KeyGenerator.getInstance("DES")」生成密钥,
    2.参数为算法名称:分别对应DES、DESede(即3DES)、AES
    3.每种算法密钥长度参数:DES(56),3DES(112,168),AES(192,256)

    2.加/解密

    //通过字节码数据key 恢复密钥
    SecretKey secretKey = new SecretKeySpec(key, "DES");
    
    //Cipher完成加密/解密工作
    Cipher cipher = Cipher.getInstance("DES");
    
    //根据密钥,对Cipher初始化,并选择加密还是解密
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    
    byte[] result = cipher.doFinal(data);

    1.加密或解密都通过cipher.init()设置,参数:ENCRYPT_MODE/DECRYPT_MODE
    2.加密或解密都通过cipher.doFinal() 执行,获得byte[]类型结果

    四、代码示例
    package minyuantec.backupsystem.action;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    
    public class DESUtil {
    
        /*
         * 生成密钥
         */
        public static byte[] initKey() throws Exception{
            KeyGenerator keyGen = KeyGenerator.getInstance("DES");
            keyGen.init(56);
            SecretKey secretKey = keyGen.generateKey();
            return secretKey.getEncoded();
        }
    
        
        /*
         * DES 加密
         */
        public static byte[] encrypt(byte[] data, byte[] key) throws Exception{
            SecretKey secretKey = new SecretKeySpec(key, "DES");
            
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] cipherBytes = cipher.doFinal(data);
            return cipherBytes;
        }
        
        
        /*
         * DES 解密
         */
        public static byte[] decrypt(byte[] data, byte[] key) throws Exception{
            SecretKey secretKey = new SecretKeySpec(key, "DES");
            
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] plainBytes = cipher.doFinal(data);
            return plainBytes;
        }
    
        //Test
        public static void main(String[] args) throws Exception {
            byte[] desKey = DESUtil.initKey();
            System.out.println("DES KEY : "+new String(desKey,"UTF-8"));
            String DATA = "12345";
            byte[] desResult = encrypt(DATA.getBytes() , desKey);
            System.out.println(DATA + ">>>DES 加密结果>>>" + new String(desResult,"UTF-8"));
            
            byte[] desPlain = DESUtil.decrypt(desResult, desKey);
            System.out.println(DATA + ">>>DES 解密结果>>>" + new String(desPlain));
        }
        
    }

    打印结果:

    DES KEY : �,�����E
    12345>>>DES 加密结果>>>Y:(H� �
    12345>>>DES 解密结果>>>12345

    欢迎关注微信公众号【Java典籍】,收看更多Java技术干货!

      ▼微信扫一扫下图↓↓↓二维码关注

     

     
  • 相关阅读:
    Leetcode 1489找到最小生成树李关键边和伪关键边
    Leetcode 113 路径总和 II
    hdu 1223 还是畅通工程
    hdu 1087 Super Jumping! Jumping! Jumping!
    hdu 1008 Elevator
    hdu 1037 Keep on Truckin'
    湖工oj 1241 畅通工程
    湖工oj 1162 大武汉局域网
    hdu 2057 A + B Again
    poj 2236 Wireless Network
  • 原文地址:https://www.cnblogs.com/bingyimeiling/p/12769373.html
Copyright © 2011-2022 走看看