特点:解密加密使用相同秘钥 , 计算量小 , 算法简单 , 加密效率高
对称加密算法实现 DES加密 (不安全)
Jdk实现DES
public static void jdkDES(String str) throws Exception {//系统自动生成keybyte[] key = KeyGenerator.getInstance("DES").generateKey().getEncoded();//转换秘钥DESKeySpec desKeySpec = new DESKeySpec(key);SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec);//加密//加密模式 DESCipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");//初始化加密工具cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);byte[] enBytes = cipher.doFinal(str.getBytes());//展示System.out.println("Jdk DES Encrypt:"+ Hex.toHexString(enBytes));//解密cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);byte[] deBytes = cipher.doFinal(enBytes);System.out.println("Jdk DES Decrypt:"+new String(deBytes));}
BC实现DES
public static void BCDES(String str) throws Exception {//向JDK中添加算法Security.addProvider(new BouncyCastleProvider());//系统自动生成keybyte[] key = KeyGenerator.getInstance("DES").generateKey().getEncoded();//转换秘钥DESKeySpec desKeySpec = new DESKeySpec(key);SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec);//加密//加密模式 DESCipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");//初始化加密工具cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);byte[] enBytes = cipher.doFinal(str.getBytes());//展示System.out.println("Jdk DES Encrypt:"+ Hex.toHexString(enBytes));//解密cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);byte[] deBytes = cipher.doFinal(enBytes);System.out.println("Jdk DES Decrypt:"+new String(deBytes));}
(三重DES)
JDK实现
public static voidjdkTripleDES(String str) throws Exception{//系统自动生成keyKeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");//依据算法自定义Key长度 init(168);keyGenerator.init(new SecureRandom());byte[] key = keyGenerator.generateKey().getEncoded();//转换秘钥DESedeKeySpec desKeySpec = new DESedeKeySpec(key);SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede");Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec);//加密//加密模式 DESCipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");//初始化加密工具cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);byte[] enBytes = cipher.doFinal(str.getBytes());//展示System.out.println("Jdk TripleDES Encrypt:"+ Hex.toHexString(enBytes));//解密cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);byte[] deBytes = cipher.doFinal(enBytes);System.out.println("Jdk TripleDES Decrypt:"+new String(deBytes));}
BC+JDK实现
public static voidBCTripleDES(String str) throws Exception{Security.addProvider(new BouncyCastleProvider());//系统自动生成keyKeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");//依据算法自定义Key长度 init(168);keyGenerator.init(new SecureRandom());byte[] key = keyGenerator.generateKey().getEncoded();//转换秘钥DESedeKeySpec desKeySpec = new DESedeKeySpec(key);SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede");Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec);//加密//加密模式 DESCipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");//初始化加密工具cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);byte[] enBytes = cipher.doFinal(str.getBytes());//展示System.out.println("BC+Jdk TripleDES Encrypt:"+ Hex.toHexString(enBytes));//解密cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);byte[] deBytes = cipher.doFinal(enBytes);System.out.println("BC+Jdk TripleDES Decrypt:"+new String(deBytes));}
- java学习笔记2--面向对象编程
- java中静态块的作用,调用顺序和使用方法
- 一 微信公众平台开发JAVA验证来自微信服务器的消息
- 4 零基础学java---方法method
- 海贼王在中国哪年上映
- 1、改变参考文献在正文和末尾中的颜色
- 快速调整毕业论文格式:Word中给表格设置不同磅值的内外框
- 受较强冷空气影响中东部地区将迎降温降雨 广东广西局地有暴雨到大暴雨
- Redis 中的 流水线与事务 流水线
- 基础概念:书中的域、final关键字