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. 生成密钥

d77850b9875319beeead2dc9dbdeca97.png

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

2. 加/解密

a3ddcc356cd8c39822a06587ae51596e.png

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

觉得内容还不错的话,给我点个“在看”呗

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值