最近用到对文本内容进行加密,于是查了一下常用的加密算法:
DES(Data Encryption Standard):对称算法,数据加密标准,速度较快,适用于加密大量数据的场合;
3DES(Triple DES):是基于DES的对称算法,对一块数据用三个不同的密钥进行三次加密,强度更高;
RC2和 RC4:对称算法,用变长密钥对大量数据进行加密,比 DES 快;
IDEA(International Data Encryption Algorithm)国际数据加密算法,使用 128 位密钥提供非常强的安全性;
RSA:由 RSA 公司发明,是一个支持变长密钥的公共密钥算法,需要加密的文件块的长度也是可变的,非对称算法;
DSA(Digital Signature Algorithm):数字签名算法,是一种标准的 DSS(数字签名标准),严格来说不算加密算法;
AES(Advanced Encryption Standard):高级加密标准,对称算法,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法;
BLOWFISH,它使用变长的密钥,长度可达448位,运行速度很快;
MD5:严格来说不算加密算法,只能说是摘要算法。
1. /*
2. * encrypt
3. * @param content:
4. * @param password:
5. */
6. private static byte[] encrypt(String content, String password) {
7. try {
8. KeyGenerator kgen = KeyGenerator.getInstance("AES");
9. kgen.init(128, new SecureRandom(password.getBytes()));
10. SecretKey secretKey = kgen.generateKey();
11. byte[] encodeFormat = secretKey.getEncoded();
12. SecretKeySpec key = new SecretKeySpec(encodeFormat, "AES");
13. Cipher cipher = Cipher.getInstance("AES");
14. byte[] byteContent = content.getBytes("utf-8");
15. cipher.init(Cipher.ENCRYPT_MODE, key);
16. byte[] result = cipher.doFinal(byteContent);
17. return result;
18. } catch (NoSuchAlgorithmException e) {
19. e.printStackTrace();
20. } catch (NoSuchPaddingException e) {
21. e.printStackTrace();
22. } catch (InvalidKeyException e) {
23. e.printStackTrace();
24. } catch (UnsupportedEncodingException e) {
25. e.printStackTrace();
26. } catch (IllegalBlockSizeException e) {
27. e.printStackTrace();
28. } catch (BadPaddingException e) {
29. e.printStackTrace();
30. }
31.
32. return null;
33. }
解密:
[java] view plain copy
1. /*
2. * decrypt
3. * @param content:
4. * @param password:
5. */
6. private static byte[] decrypt(byte[] content, String password) {
7. try {
8. KeyGenerator kgen = KeyGenerator.getInstance("AES");
9. kgen.init(128, new SecureRandom(password.getBytes()));
10. SecretKey secretKey = kgen.generateKey();
11. byte[] encodeFormat = secretKey.getEncoded();
12. SecretKeySpec key = new SecretKeySpec(encodeFormat, "AES");
13. Cipher cipher = Cipher.getInstance("AES");
14. cipher.init(Cipher.DECRYPT_MODE, key);
15. byte[] result = cipher.doFinal(content);
16. return result;
17. } catch (NoSuchAlgorithmException e) {
18. e.printStackTrace();
19. } catch (NoSuchPaddingException e) {
20. e.printStackTrace();
21. } catch (InvalidKeyException e) {
22. e.printStackTrace();
23. } catch (IllegalBlockSizeException e) {
24. e.printStackTrace();
25. } catch (BadPaddingException e) {
26. e.printStackTrace();
27. }
28.
29. return null;
30. }
示例:
[java] view plain copy
1. String content = "test";
2. String password = "12345678";
3. //加密
4. System.out.println("加密前:" + content);
5. byte[] encryptResult = encrypt(content, password);
6. //解密
7. byte[] decryptResult = decrypt(encryptResult,password);
8. System.out.println("解密后:" + new String(decryptResult));
输出:
[java] view plain copy
1. 加密前:test
2. 解密后:test
如果加密后想十六进制显示,可以添加两个函数,二进制与十六进制转换
二进制转换为十六进制:
[java] view plain copy
1. private static String parseByte2HexStr(byte buf[]) {
2. StringBuffer sb = new StringBuffer();
3. for (int i = 0; i < buf.length; i++) {
4. String hex = Integer.toHexString(buf[i] & 0xFF);
5. if (hex.length() == 1) {
6. hex = '0' + hex;
7. }
8. sb.append(hex.toUpperCase());
9. }
10. return sb.toString();
11. }
十六进制转换为二进制:
[java] view plain copy
1. private static byte[] parseHexStr2Byte(String hexStr) {
2. if (hexStr.length() < 1) {
3. return null;
4. }
5.
6. byte[] result = new byte[hexStr.length() / 2];
7. for (int i = 0; i < hexStr.length() / 2; i++) {
8. int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
9. int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
10. result[i] = (byte)(high * 16 + low);
11. }
12. return result;
13. }
示例:
[java] view plain copy
1. String content = "test";
2. String password = "12345678";
3. //加密
4. System.out.println("加密前:" + content);
5. byte[] encryptResult = encrypt(content, password);
6. String encryptResultStr = parseByte2HexStr(encryptResult);
7. System.out.println("加密后:" + encryptResultStr);
8. //解密
9. byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
10. byte[] decryptResult = decrypt(decryptFrom,password);
11. System.out.println("解密后:" + new String(decryptResult));
输出:
[java] view plain copy
1. 加密前:test
2. 加密后:73C58BAFE578C59366D8C995CD0B9D6D
3. 解密后:test