Java中加密算法介绍及其实现( 四 )


特点:解密加密使用相同秘钥 , 计算量小 , 算法简单 , 加密效率高
对称加密算法实现 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));}