最近用到对文本内容进行加密,于是查了一下常用的加密算法:

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:严格来说不算加密算法,只能说是摘要算法。

以下是Java 实现AES加解密的示例:

加密:

/*
* encrypt
* @param content:
* @param password:
*/
private static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] encodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(encodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}

return null;
}

解密:
/*
* decrypt
* @param content:
* @param password:
*/
private static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] encodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(encodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}

return null;
}

示例:

String content = "test";
String password = "12345678";
//加密
System.out.println("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
//解密
byte[] decryptResult = decrypt(encryptResult,password);
System.out.println("解密后:" + new String(decryptResult));

如果加密后想十六进制显示,可以添加两个函数,二进制与十六进制转换
二进制转换为十六进制:

private static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}

十六进制转换为二进制:
private static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}

byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte)(high * 16 + low);
}
return result;
}

示例:

String content = "test";
String password = "12345678";
//加密
System.out.println("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
String encryptResultStr = parseByte2HexStr(encryptResult);
System.out.println("加密后:" + encryptResultStr);
//解密
byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
byte[] decryptResult = decrypt(decryptFrom,password);
System.out.println("解密后:" + new String(decryptResult));

最新文章

  1. Jquery学习插件之手风琴
  2. word20161215
  3. 让xterm更舒服的设置
  4. 【代码笔记】iOS-提醒时间的选择
  5. 越狱后想禁用Spotlight
  6. uva 11186 Circum Triangle&lt;叉积&gt;
  7. android短信的接收和发送处理
  8. Socket 死连接详解
  9. 利用jink调试程序,时间不准的解决办法
  10. [坑况]——windows升级node最新版本报错【npm install -g n】
  11. sql server 生成数据库字典 sql语句
  12. SQLServer更改用户定义的数据库角色
  13. zabbix自定义监控项
  14. VirtualBox安装Ubuntu14.04
  15. Windows 下安装 Ubuntu 双系统(更新)
  16. 定位JVM内存溢出问题思路总结
  17. spring——事务管理
  18. Looper分析。ThreadLocal有关
  19. Linux的getrlimit与setrlimit系统调用
  20. HTTP API 设计指南(结尾)

热门文章

  1. 10 Vue 学习 shortList页面
  2. python3-----反射实例
  3. prototype for &#39;类名::函数名&#39;does not match any in class&#39;类名&#39;
  4. 快速部署Kubernetes集群管理
  5. 安装Matlab出现Error 1935错误解决方法
  6. MATLAB进行假设检验
  7. Unicode编码下字符串转换
  8. Weekly Contest 113
  9. [51nod] 1289 大鱼吃小鱼 堆栈-模拟
  10. keycode和which