付费节点推荐
免费节点
节点使用教程
对称密码概述
DES 算法的编程使用
3DES 算法的编程使用
AES 算法的编程使用
对称密码概述 — 对称密码的概念
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 算法的编程使用 — DES 算法基本概念
1.DES:数据加密标准,是对称加密算法领域中的典型算法
2.特点:密钥偏短(56位)、生命周期短
3.JDK实现
1.生成密钥
[cc lang="java"]
//KeyGenerator ,密钥生成器
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
//初始化密钥生成器
keyGen.init(56);
//生成密钥
SecretKey secretKey = keyGen.generateKey();
[/cc]
2.加/解密
[cc lang="java"]
//恢复密钥
SecretKey secretKey = new SecretKeySpec(key, "DES");
//Cipher 完成加密或解密工作
Cipher cipher = Cipher.getInstance("DES");
// 根据密钥,对Cipher 初始化,ENCRYPT_MODE、 DECRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 加密或解密
byte[] cipherByte = cipher.doFinal(data);
[/cc]
[cc lang="java"]
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
/*
* 生成密钥
*/
public static byte[] initKey() throws NoSuchAlgorithmException{
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); //192 256
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[] cipherBytes = cipher.doFinal(data);
return cipherBytes;
}
/*
* AES 解密
*/
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;
}
}
[/cc]
3DES 算法的编程使用 — 3DES 算法基本概念
- 3DES:将密钥长度增至112位或168位,通过增加迭代次数提高安全性
- 缺点:处理速度较慢、密钥计算时间较长、加密效率不高
- JDK实现
[cc lang="java"]
1.生成密钥
KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
keyGen.init(168); //可指定密钥长度为112或168,默认为168
SecretKey secretKey = keyGen.generateKey();
[/cc]
2.加/解密
[cc lang="java"]
SecretKey secretKey = new SecretKeySpec(key, "DESede");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherByte = cipher.doFinal(data);
[/cc]
AES 算法的编程使用
1.AES:高级数据加密标准,能够有效抵御已知的针对DES算法的所有攻击
2.特点:密钥建立时间短、灵敏性好、内存需求低、安全性高
3.JDK实现
1.生成密钥
[cc lang="java"]
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keygen.init(128); //默认128,获得无政策权限后可为192或256
SecretKey secretKey = keyGen.generateKey();
[/cc]
2.加/解密
[cc lang="java"]
SecretKey secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherByte = cipher.doFinal(data);
[/cc]
[cc lang="java"]
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
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/ECB/PKCS5Padding");
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/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainBytes = cipher.doFinal(data);
return plainBytes;
}
}
[/cc]
未经允许不得转载:Bcoder资源网 » java对称密码算法的使用DES/3DES/AES算法
评论前必须登录!
登陆 注册