对称加密算法
也称为对称密码,是指在加密和解密时使用同一密钥的加密方式。
对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文“原始数据”和“加密密钥“一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。对称加密过程如图:
对称加密算法的特点是算法公开,计算量小,加密速度快,加密效率高。优势在于加解密的高速度和使用长密钥时的难破解性,但是,对称加密算法的安全依赖于密钥,泄漏密钥就意味着任何人都可以对加密的密文进行解密,因此密钥的保护对于加密信息是否安全至关重要。
常用的对称加密算法包括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=
本文暂时没有评论,来添加一个吧(●'◡'●)