时间:2017年4月11日星期二
说明:本文部分内容均来自慕课网。@慕课网:教学示例源码:个人学习源码:第一章:对称加密算法DES
1-1 JAVA对称加密算法DES
加密密钥=解密密钥
对称加密算法
初等DES --3DESAESPBEIDEA
算法实现:
代码演示:
package com.myimooc.security.des;import org.apache.commons.codec.binary.Hex;import org.bouncycastle.jce.provider.BouncyCastleProvider;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import java.security.Key;import java.security.NoSuchAlgorithmException;import java.security.Security;/** * DES对称加密演示 * Created by ZC on 2017/4/11. */public class DemoDES { /** 待加密字符串 */ private static String src="imooc security des"; public static void main(String[] args){ jdkDES(); bcDES(); } /** 使用 jdk 实现des加解密 */ public static void jdkDES(){ try { // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); byte[] byteKey = secretKey.getEncoded(); // KEY转换 DESKeySpec desKeySpec = new DESKeySpec(byteKey); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec); // 加密 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk des encrypt:"+ Hex.encodeHexString(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE,convertSecretKey); result = cipher.doFinal(result); System.out.println("jdk des decrypt:"+ new String(result)); } catch (Exception e) { e.printStackTrace(); } } /** 使用BouncyCastle实现DES加解密 */ public static void bcDES(){ try { Security.addProvider(new BouncyCastleProvider()); // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DES","BC"); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); byte[] byteKey = secretKey.getEncoded(); // KEY转换 DESKeySpec desKeySpec = new DESKeySpec(byteKey); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec); // 加密 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk des encrypt:"+ Hex.encodeHexString(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE,convertSecretKey); result = cipher.doFinal(result); System.out.println("jdk des decrypt:"+ new String(result)); } catch (Exception e) { e.printStackTrace(); } }}
流程演示:
第二章:对称加密算法3DES
2-1 JAVA对称加密算法3DES
为什么会有3重DES,DES的劣势
1.违反柯克霍夫原则2.安全问题
3重DES的好处
1.密钥长度增强2.迭代次数提高
算法实现:
代码演示:
package com.myimooc.security.des;import org.apache.commons.codec.binary.Hex;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.DESedeKeySpec;import java.security.Key;import java.security.SecureRandom;/** * 3DES对称加密演示 * Created by ZC on 2017/4/11. */public class Demo3DES { /** 待加密字符串 */ private static String src="imooc security 3des"; public static void main(String[] args){ jdk3des(); } /** 使用jdk实现3重DES加解密 */ private static void jdk3des(){ try { // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); //keyGenerator.init(168); keyGenerator.init(new SecureRandom()); SecretKey secretKey = keyGenerator.generateKey(); byte[] byteKey = secretKey.getEncoded(); // KEY转换 DESedeKeySpec desKeySpec = new DESedeKeySpec(byteKey); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede"); Key convertSecretKey = secretKeyFactory.generateSecret(desKeySpec); // 加密 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk 3des encrypt:"+ Hex.encodeHexString(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE,convertSecretKey); result = cipher.doFinal(result); System.out.println("jdk 3des decrypt:"+ new String(result)); } catch (Exception e) { e.printStackTrace(); } }}
第三章:对称加密算法AES
3-1 JAVA对称加密算法AES
JAVA对称加密算法:AES
AES是目前使用最多的对称加密算法AES的优势之一是至今尚未被破解AES通常用于移动通信系统加密以及基于SSH协议的软件 比如:SSH Clint、secureCRT
AES算法
高级DES替代者
算法实现:
代码演示:
package com.myimooc.security.aes;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import java.security.Key;import java.security.NoSuchAlgorithmException;/** * /** * AES对称加密演示 * Created by ZC on 2017/4/11. */public class DemoAES { /** 待加密字符串 */ private static String src="imooc security aes"; public static void main(String[] args){ jdkAES(); } /** 通过JDK实现AES对称加密 */ public static void jdkAES(){ try { // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey secretKey = keyGenerator.generateKey(); byte[] keyBytes = secretKey.getEncoded(); // KEY 转换 Key key = new SecretKeySpec(keyBytes,"AES"); // 加密 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE,key); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk aes encrypt:"+ Base64.encodeBase64String(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE,key); result=cipher.doFinal(result); System.out.println("jdk aes decrypt:"+ new String(result)); } catch (Exception e) { e.printStackTrace(); } }}
流程演示:
第四章:对称加密算法PBE
4-1 JAVA对称加密算法PBE
PBE:综合性对称加密算法
PBE(Password Based Encyption)基于口令加密对已有算法的包装JDK、BC盐PBEWithMD5AndDES
算法实现:
代码演示:
package com.myimooc.security.pbe;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.PBEKeySpec;import javax.crypto.spec.PBEParameterSpec;import java.security.Key;import java.security.SecureRandom;/** * /** * /** * PBE对称加密演示 * Created by ZC on 2017/4/11. */public class DemoPBE { /** 待加密字符串 */ private static String src="imooc security pbe"; private static final String TOKEN = "imooc"; public static void main(String[] args){ jdkPBE(); } /** 使用JDK实现PBE算法加解密 */ public static void jdkPBE(){ try{ // 初始化盐 SecureRandom random = new SecureRandom(); byte[] salt = random.generateSeed(8); // 口令与密钥 String password = TOKEN; PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); Key key = factory.generateSecret(pbeKeySpec); // 加密 PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt,100); Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES"); cipher.init(Cipher.ENCRYPT_MODE,key,pbeParameterSpec); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk pbe encrypt:"+ Base64.encodeBase64String(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE,key,pbeParameterSpec); result = cipher.doFinal(result); System.out.println("jdk pbe decrypt:"+ Base64.encodeBase64String(result)); }catch (Exception e){ e.printStackTrace(); } }}
流程演示:
第五章:小结
5-1 对称加密算法小结
对称加密算法:回顾
初等DES --3DESAESPBE
加密和解密不在同一方怎么办?
把密钥发给对方