网站首页 > 技术教程 正文
对称加密算法
也称为对称密码,是指在加密和解密时使用同一密钥的加密方式。
对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文“原始数据”和“加密密钥“一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。对称加密过程如图:
对称加密算法的特点是算法公开,计算量小,加密速度快,加密效率高。优势在于加解密的高速度和使用长密钥时的难破解性,但是,对称加密算法的安全依赖于密钥,泄漏密钥就意味着任何人都可以对加密的密文进行解密,因此密钥的保护对于加密信息是否安全至关重要。
常用的对称加密算法包括DES算法,3EDS算法,AES算法。
DES算法
DES算法又被称为美国数据加密标准,是1972年美国IBM公司研制的对称加密算法。在设计上,DES采用了Feistel结构。 DES是一个分组密码算法,明文和密文的长度相同,分组长度为64位。加解密用的都是同一个密钥,密钥长度为64位,其中第8、16、24、32、40、48、56、64位是奇偶校验位,使得密钥中每8位都有奇数个1,因此,有效密钥长度为56位。分组后的明文组和56位的密钥按位替代或交换的方法得到密文组。
DES 加密流程
DES 解密流程
生成DES密钥
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
BASE64Encoder base64Encoder = new BASE64Encoder();
System.out.println("DES_key:" + base64Encoder.encode(secretKey.getEncoded()));
}
DES_key:bUAp9yb4iQs=
public class DESTest {
public static void main(String[] args) throws Exception {
// 密钥
String key = "bUAp9yb4iQs=";
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] bytes = base64Decoder.decodeBuffer(key);
String content = "this is a des test content";
SecretKey secretKey = new SecretKeySpec(bytes, "DES");
//加密
byte[] desStr = encryptDES(secretKey, content);
BASE64Encoder base64Encoder = new BASE64Encoder();
// Base64算法编码
System.out.println(base64Encoder.encode(desStr));
System.out.println(decryptDES(secretKey, desStr));
}
/**
* DES 加密
*/
private static byte[] encryptDES(SecretKey secretKey, String content) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(content.getBytes());
return bytes;
}
/**
* DES 解密
*/
private static String decryptDES(SecretKey secretKey, byte[] source) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(source);
return new String(bytes, "UTF-8");
}
}
AES算法
AES的全称是Advanced Encryption Standard,意思是高级加密标准。它的出现主要是为了取代DES加密算法的,因为我们都知道DES算法的密钥长度是56Bit,因此算法的理论安全强度是2的56次方。但二十世纪中后期正是计算机飞速发展的阶段,元器件制造工艺的进步使得计算机的处理能力越来越强,虽然出现了3DES的加密方法,但由于它的加密时间是DES算法的3倍多,64Bit的分组大小相对较小,所以还是不能满足人们对安全性的要求。于是1997年1月2号,美国国家标准技术研究所宣布希望征集高级加密标准,用以取代DES。AES也得到了全世界很多密码工作者的响应,先后有很多人提交了自己设计的算法。最终有5个候选算法进入最后一轮:Rijndael,Serpent,Twofish,RC6和MARS。最终经过安全性分析、软硬件性能评估等严格的步骤,Rijndael算法获胜。
生成AES密钥
private static String genKeyAES() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
BASE64Encoder base64Encoder = new BASE64Encoder();
// Base64编码
String base64Str = base64Encoder.encode(secretKey.getEncoded());
System.out.println(base64Str);
return base64Str;
}
oBu2LHRwM3MGG11vuW/vYg==
public class AESTest {
public static void main(String[] args) throws Exception {
// 密钥
String key = "oBu2LHRwM3MGG11vuW/vYg==";
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] bytes = base64Decoder.decodeBuffer(key);
String content = "this is a aes test content";
SecretKey secretKey = new SecretKeySpec(bytes, "AES");
//加密
byte[] desStr = encryptAES(secretKey, content);
BASE64Encoder base64Encoder = new BASE64Encoder();
// Base64算法编码
System.out.println(base64Encoder.encode(desStr));
System.out.println(decryptAES(secretKey, desStr));
}
private static String genKeyAES() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
BASE64Encoder base64Encoder = new BASE64Encoder();
// Base64编码
String base64Str = base64Encoder.encode(secretKey.getEncoded());
System.out.println(base64Str);
return base64Str;
}
/**
* AES 加密
*/
private static byte[] encryptAES(SecretKey secretKey, String content) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(content.getBytes());
return bytes;
}
/**
* AES 解密
*/
private static String decryptAES(SecretKey secretKey, byte[] source) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(source);
return new String(bytes, "UTF-8");
}
}
AES的密钥 [Base64算法编码]:
oBu2LHRwM3MGG11vuW/vYg==
AES 算法加密后生成的密文 [Base64算法编码]:
rFAqvd+NdKpwMudMDOoPTC2Vq7dE2Bdni9JqSfANWbc=
猜你喜欢
- 2024-10-06 CAD最新快捷键命令大全 cad2022快捷键命令大全
- 2024-10-06 2-3!国足客场惜败沙特,武磊单刀不进,洛国富世界波+错失绝平
- 2024-10-06 魔兽世界9.2:元素萨是赎罪大厅最好治疗?30赎罪元素斗士队探索
- 2024-10-06 AJ13GIGI、AJ1Low北卡!还有性价比春夏服饰
- 2024-10-06 “快播”复活?红衣教主周鸿祎在下一盘什么棋?
- 2024-10-06 惊天大瓜!白晓荷带子归来,黄周二选一,编剧能否解开情感谜团?
- 2024-10-06 门窗绘图软件CAD快捷键大全! 门窗设计cad教程
- 2024-10-06 摇滚圈惊天大瓜!歌手婚内出轨高达240次,连岳母丧期也不放过!
- 2024-10-06 360发布快视频APP,百亿基金打响生态之战!
- 2024-10-06 Qlik最新版本发布多项创新,点亮你的数据洞察之旅
你 发表评论:
欢迎- 05-1613步震撼淘宝大促闪光裂纹破墙立体字PS制作教程
- 05-16AI教程 | 绘制扁平的萌萌哒图标
- 05-160基础学平面设计所需了解的基础常识汇总
- 05-16自学平面设计需要多长时间?十六年职业设计总监告诉你
- 05-16平面设计都要学习哪些内容?
- 05-16李涛PS教程 高手之路PS教程 合成教程 —制作一个小星球
- 05-16Illustrator实例教程:制作炫酷的漩涡效果
- 05-16Illustrator实例教程:利用混合工具制作一朵炫酷的花
- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)