对称密码概述:
1.加密密钥和解密密钥相同,对于大多数对称密码算法,加解密过程互逆
2.加解密通信模型
3.特点:算法公开、计算量小、加密速度快、加密效率高
4.弱点:双方都使用同样密钥,安全性得不到保证
5.分组密码工作模式
(1)ECB:电子密码本 (4)OFB:输出反馈
(2)CBC:密文链接 (5)CTR:计数器
(3)CFB:密文反馈
6.分组密码填充方式
(1)NoPadding
(2)PKCS5Padding
(3)ISO10126Padding
常用对称密码:
(1)DES(Data Encryption Standard)
(2)3DES(Triple DES、DESede)
(3)AES(Advanced Encryption Standard)
DES算法的编程使用:
1.DES:数据加密标准,是对称加密算法领域中的典型算法
2.特点:密钥偏短(56位)、生命周期短
3.JDK实现
算法 秘钥长度 默认秘钥长度
DES 56 56
工作模式:ECB、CBC、PCBC、CTR、CTS、CFB、CFB8-CFB128、OFB、OFB8-OFB128
填充方式:NoPadding、PKCS5Padding、ISO10126Padding
DES算法的工具类DESUtil :
package key.base64;
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);//传入秘钥长度。DES对应一个秘钥长度故可写可不写
SecretKey secretKey=keyGen.generateKey();
return secretKey.getEncoded();
}
/**
* DES加密
*/
public static byte[] encrypt(byte[] data,byte[] key) throws Exception{
//DES算法的工作模式
SecretKey secretKey=new SecretKeySpec(key, "DES");
//Cipher加密工作类
Cipher cipher=Cipher.getInstance("DES");
//告诉cipher的密码操作模式以及秘钥
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;
}
}
3DES的编程使用
1. 3DES:将密钥长度增至112位或168位,通过增加迭代次数提高安全性
2. 缺点:处理速度较慢、密钥计算时间较长、加密效率不高
3. JDK实现
密钥长度:112/168
默认密钥长度:168
工作模式:ECB、CBC、PCBC、CTR、CTS、CFB、CFB8-CFB128、OFB、OFB8-OFB128
填充方式:NoPadding、PKCS5Padding、ISO10126Padding
3DES算法的工具类TripleDESUtil :
package key.base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class TripleDESUtil {
/**
* 生成秘钥
*/
public static byte[] initKey() throws Exception {
KeyGenerator keyGen=KeyGenerator.getInstance("DESede");
keyGen.init(168);//112 168
SecretKey secretKey=keyGen.generateKey();
return secretKey.getEncoded();
}
/**
* 3DES加密
*/
public static byte[] encrypt(byte[] data,byte[] key) throws Exception {
SecretKey secretKey=new SecretKeySpec(key, "DESede");
Cipher cipher=Cipher.getInstance("DESede");
cipher.init(cipher.ENCRYPT_MODE, secretKey);
byte[] cipherBytes=cipher.doFinal(data);
return cipherBytes;
}
/**
* 3DES解密
*/
public static byte[] decrypt(byte[] data,byte[] key) throws Exception{
SecretKey secretKey=new SecretKeySpec(key, "DESede");
Cipher cipher=Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainBytes=cipher.doFinal(data);
return plainBytes;
}
}
AES 算法的编程使用
1.AES:高级数据加密标准,能够有效抵御已知的针对DES算法的所有攻击
2.特点:密钥建立时间短、灵敏性好、内存需求低、安全性高
3.JDK实现
密钥长度:128、192、256
默认密钥长度:128
工作模式:ECB、CBC、PCBC、CTR、CTS、CFB、CFB8-CFB128、OFB、OFB8-OFB128
填充方式:NoPadding、PKCS5Padding、ISO10126Padding
AES算法的工具类AESUtil :
package key.base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
/**
* 生成秘钥
*/
public static byte[] initKey() throws Exception {
KeyGenerator keyGen=KeyGenerator.getInstance("AES");
keyGen.init(256);//192 156
SecretKey secretKey=keyGen.generateKey();
return secretKey.getEncoded();
}
/**
* AES加密
*/
public static byte[] encrypt(byte[] data,byte[] key)throws Exception {
SecretKey secretKey=new SecretKeySpec(key, "AES");
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] resultBytes=cipher.doFinal(data);
return resultBytes;
}
/**
* DES解密
*/
public static byte[] decrypt(byte[] data,byte[] key)throws Exception {
SecretKey secretKey=new SecretKeySpec(key, "AES");
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainBytes=cipher.doFinal(data);
return plainBytes;
}
}
main方法测试:
public class Main{
public static void main(String[] args){
//Test DES
byte[] desKey=DESUtil.initKey();
System.out.println("DES KEY:"+BytesToHex.fromBytesToHex(desKey));
byte[] desResult=DESUtil.encrypt(DATA.getBytes(), desKey);
System.out.println(DATA+">>>DES加密>>>"+BytesToHex.fromBytesToHex(desResult));
byte[] desPlain=DESUtil.decrypt(desResult,desKey);
System.out.println(DATA+">>>des解密>>>"+new String(desPlain));
//Test 3DES
byte[] tripleKey=TripleDESUtil.initKey();
System.out.println("3DES KEY:"+BytesToHex.fromBytesToHex(tripleKey));
byte[] tripleResult=TripleDESUtil.encrypt(DATA.getBytes(), tripleKey);
System.out.println(DATA+">>>3DES加密>>>"+BytesToHex.fromBytesToHex(tripleResult));
byte[] triplePlain=DESUtil.decrypt(desResult,desKey);
System.out.println(DATA+">>>3DES解密>>>"+new String(triplePlain));
//Test AES
byte[] aesKey=AESUtil.initKey();
System.out.println("AES KEY:"+BytesToHex.fromBytesToHex(aesKey));
byte[] aesResult=AESUtil.encrypt(DATA.getBytes(), aesKey);
System.out.println(DATA+">>>AES加密>>>"+BytesToHex.fromBytesToHex(aesResult));
byte[] aesPlain=AESUtil.decrypt(aesResult,aesKey);
System.out.println(DATA+">>>AES解密>>>"+new String(aesPlain));
}
}