public final class DesUtils {
private static final String DES = "DES";
private static final String KEY = "4YztMHI7PsT4rLZN"; private DesUtils() {} private static byte[] encrypt(byte[] src, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
return cipher.doFinal(src);
} private static byte[] decrypt(byte[] src, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
return cipher.doFinal(src);
} private static String byte2hex(byte[] b) {
String hs = "";
String temp = "";
for (int n = 0; n < b.length; n++) {
temp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (temp.length() == 1)
hs = hs + "0" + temp;
else
hs = hs + temp;
}
return hs.toUpperCase(); } private static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("length not even");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
} private static String decode(String src, String key) {
String decryptStr = "";
try {
byte[] decrypt = decrypt(hex2byte(src.getBytes()), key.getBytes());
decryptStr = new String(decrypt);
} catch (Exception e) {
e.printStackTrace();
}
return decryptStr;
} private static String encode(String src, String key){
byte[] bytes = null;
String encryptStr = "";
try {
bytes = encrypt(src.getBytes(), key.getBytes());
} catch (Exception ex) {
ex.printStackTrace();
}
if (bytes != null)
encryptStr = byte2hex(bytes);
return encryptStr;
} /**
* 解密
*/
public static String decode(String src) {
return decode(src, KEY);
} /**
* 加密
*/
public static String encode(String src) {
return encode(src, KEY);
}
}

测试

public static void main(String[] args) {
String ss = "uu123@#$";
String encodeSS = encode(ss);
System.out.println(encodeSS);
String decodeSS = decode(encodeSS);
System.out.println(decodeSS);
}
result:
6EA84873A948B299936006D75B7CA819
uu123@#$

最新文章

  1. Jenkins配置MSBuild实现自动部署(MSBuild+SVN/Subversion+FTP+BAT)
  2. KnockoutJS 3.X API 第七章 其他技术(6) 使用&ldquo;fn&rdquo;添加自定义函数
  3. 使用 WinAppDeployCmd 部署Win10 App 到移动设备
  4. windows7-SQLyog 安装图解
  5. for while (list each)的用法
  6. ubuntu12.04 Daemon的简单实现
  7. lucene 3.0.2 搜索
  8. JS 实现新浪微博, QQZone 等的分享
  9. [ionic开源项目教程] - 第15讲 ionic用户个人中心登录注册的实现
  10. C# 时间与时间戳互转
  11. HTML语义化标签(一)
  12. img 的 align 属性
  13. 在Android上仿百度贴吧客户端Loading图标小球
  14. CocoaPods 安装及使用
  15. Markdown语法及编辑器
  16. ajax提交不进入后台报415错误
  17. Java多线程之二(Synchronized)
  18. python入门(二):isinstance、内置函数、常用运算等
  19. 搭建ftp
  20. 从池子里的beta看秋香, 个性迥异

热门文章

  1. 咏南CS开发框架新的界面风格
  2. Python学习-13.Python的输入输出(二)
  3. Spring AOP 自定义注解获取http接口及WebService接口入参和出参
  4. hibernate 中 fetch=FetchType.LAZY 懒加载失败处理
  5. 支付宝PC网站接口对接
  6. JAVA—Filter
  7. MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
  8. console使用技巧
  9. “java.lang.NullPointerException”异常分析
  10. 如何获取token值