利用Cipher的核心功能,自己封装了一个加密解密的工具类,可以直接使用。在使用之前需要先下载commons-codec-1.9.jar,并导入项目。

工具类如下:

package com.pcict.util.test;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays; /**
* DESede对称加密算法
*
* @Description
* @author ljz
* @created 2015年7月31日 上午11:30:04
* @version
* @history
* @see
*/
public class DESedeUtils { // 加密模式
public static final int ENCRYPT_MODE = Cipher.ENCRYPT_MODE;
// 解密模式
public static final int DECRYPT_MODE = Cipher.DECRYPT_MODE; private static final String ALGORITHM = "DESede";
private static final Charset UTF8 = Charset.forName("UTF-8"); private Cipher cipher = null;
private int opmode = 0; // 初始化加密或解密
public synchronized boolean init(int mode, String key) {
if (opmode != 0) {
return true;
} if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) {
return false;
} if (key == null || key.isEmpty()) {
return false;
} try {
cipher = Cipher.getInstance(ALGORITHM);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (cipher == null) {
return false;
}
}
Key secKey = getSecKey(key);
if (secKey == null) {
return false;
}
try {
cipher.init(mode, secKey, new SecureRandom());
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
opmode = mode;
return true;
} private static Key getSecKey(String key) {
SecretKey securekey = null;
try {
byte[] material = Arrays.copyOf(
Base64.decodeBase64(key.getBytes(UTF8)), 24);
DESedeKeySpec keySpec = new DESedeKeySpec(material);
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance(ALGORITHM);
securekey = keyFactory.generateSecret(keySpec);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return securekey;
} // 加密
public synchronized String encrypt(String data) {
if (opmode != ENCRYPT_MODE) {
return null;
}
if (data == null) {
return null;
}
byte[] encData = null;
try {
encData = cipher.doFinal(data.getBytes(UTF8));
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (encData == null) {
return null;
}
return new String(Base64.encodeBase64(encData), UTF8);
} // 解密
public synchronized String decrypt(String data) {
if (opmode != DECRYPT_MODE) {
return null;
}
if (data == null) {
return null;
}
byte[] decData = null;
try {
decData = cipher.doFinal(Base64.decodeBase64(data.getBytes(UTF8)));
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (decData == null) {
return null;
}
return new String(decData, UTF8);
} }

测试如下:

package com.pcict.util.test;

public class Test {
public static void main(String[] args) {
DESedeUtils encoder = new DESedeUtils();
String key = "123456";
// 以123456作为加密的密匙,在后面解密的时候也要以该密匙作为解密的密匙
encoder.init(DESedeUtils.ENCRYPT_MODE, key);
String str = encoder.encrypt("1");
System.out.println(str);
DESedeUtils decoder = new DESedeUtils();
// 调用初始化解密
decoder.init(DESedeUtils.DECRYPT_MODE, key);
String str1 = decoder.decrypt(str);
System.out.println(str1);
}
}

附commons-codec-1.9.jar文件:点击下载

最新文章

  1. 【转】[特征选择] An Introduction to Feature Selection 翻译
  2. MyEclipse黑色主题
  3. 小技能——markdown
  4. MySQL与SqlServer中update操作同一个表问题
  5. java之注解Annotation
  6. (实用篇)PHP JSON数组与对象的理解
  7. Java中方法的重载
  8. motor helper
  9. 【转载】Spring Cloud全家桶主要组件及简要介绍
  10. 转一篇 ShaderVariantCollection介绍的比较详细的文章 感谢作者
  11. 公网Ip和私网ip
  12. jquery中使用datepicker限制开始日期小于结束日期
  13. WebSlides - 轻松制作漂亮的 HTML 幻灯片(演讲稿)
  14. Oracle数据库游标,序列,存储过程,存储函数,触发器
  15. Java 获取客户端IP
  16. 【agc019D】Shift and Flip
  17. jsp内置对象pageContext如何在Servlet中获取值
  18. 前端开发学习之——dom ready和window onload的区别
  19. Django日志
  20. linux中ftp配置文件详解

热门文章

  1. poj 3404&&poj1700(贪心)
  2. DotNetCore 微服务上传附件
  3. golang实现mysql数据库备份
  4. 山东BOSS性能压力测试
  5. CodeForces 732E Sockets
  6. CodeForces 723F st-Spanning Tree
  7. msysgit: Unicode font warning
  8. 【BZOJ 2646】【NEERC 2011】flight
  9. 【优先队列+贪心】POJ2431-Expedition
  10. python3-开发进阶Django-form组件中model form组件