/***********本人原创,欢迎转载,转载请保留本人信息*************/
作者:wallimn 电邮:
[email protected] 时间:2009-08-11
博客:http://wallimn.iteye.com
网络硬盘:http://wallimn.ys168.com
/***********文章发表请与本人联系,作者保留所有权利*************/
将字符串使用对称加密的方式加一下密,并转化成可显示字符;将加密结果还原为原来的字符串,是可能在很多场合会用到。
下面是一个完成上述功能的工具类,觉得有用可以拿去用。
package com.wallimn.gyz.util;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* 这个类用来给一个较短的字符串加、解密<br/>
* 编码:wallimn 时间:2009-8-12 上午07:43:29<br/>
* 版本:V1.0<br/>
*/
public class KeyUtil {
private static final String EncodeAlgorithm = "DES";
private static KeyUtil instance = null;
public static KeyUtil getInstance() {
if (instance == null) {
instance = new KeyUtil();
if (!instance.init()) {
instance = null;
}
}
return instance;
}
private SecretKey key = null;
private boolean init() {
try {
KeyGenerator keygen = KeyGenerator.getInstance(EncodeAlgorithm);
SecureRandom random = new SecureRandom();
keygen.init(random);
key = keygen.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
return key != null;
}
private Cipher getCipher(int mode) {
try {
Cipher cipher = Cipher.getInstance(EncodeAlgorithm);
cipher.init(mode, key);
return cipher;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Cipher getEncodeCipher() {
return this.getCipher(Cipher.ENCRYPT_MODE);
}
public Cipher getDecodeCipher() {
return this.getCipher(Cipher.DECRYPT_MODE);
}
/**
* 解密,若输入为null或加/解密过程出现异常,则输出为null <br/>
* 作者:wallimn 时间:2009-8-12 上午08:09:44<br/>
* 博客:http://wallimn.iteye.com<br/>
* 参数:<br/>
*
* @param str
* @return
*/
public String decode(String str) {
if(str==null)return null;
Cipher cipher = getDecodeCipher();
StringBuffer sb = new StringBuffer();
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] src = stringToBytes(str);
byte[] outBytes = new byte[outputSize];
int i = 0;
try {
for (; i <= src.length - blockSize; i = i + blockSize) {
int outLength = cipher.update(src, i, blockSize, outBytes);
sb.append(new String(outBytes, 0,outLength));
}
if (i == src.length)
outBytes = cipher.doFinal();
else {
outBytes = cipher.doFinal(src, i, src.length - i);
}
sb.append(new String(outBytes));
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 加密,若输入为null或加/解密过程出现异常,则输出为null <br/>
* 作者:wallimn 时间:2009-8-12 上午08:09:59<br/>
* 博客:http://wallimn.iteye.com<br/>
* 参数:<br/>
*
* @param str
* @return
*/
public String encode(String str) {
if(str==null)return null;
Cipher cipher = getEncodeCipher();
StringBuffer sb = new StringBuffer();
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] src = str.getBytes();
byte[] outBytes = new byte[outputSize];
int i = 0;
try {
for (; i <= src.length - blockSize; i = i + blockSize) {
int outLength = cipher.update(src, i, blockSize, outBytes);
sb.append(bytesToString(outBytes, outLength));
}
if (i == src.length)
outBytes = cipher.doFinal();
else {
outBytes = cipher.doFinal(src, i, src.length - i);
}
sb.append(bytesToString(outBytes));
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String bytesToString(byte[] bs) {
if (bs == null || bs.length == 0)
return "";
return bytesToString(bs, bs.length);
}
private String bytesToString(byte[] bs, int len) {
if (bs == null || bs.length == 0)
return "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++) {
// System.out.println(bs[i]+":"+String.format("%02X", bs[i]));
sb.append(String.format("%02X", bs[i]));
}
return sb.toString();
}
private byte[] stringToBytes(String str) {
if (str == null || str.length() < 2 || str.length() % 2 != 0)
return new byte[0];
int len = str.length();
byte[] bs = new byte[len / 2];
for (int i = 0; i * 2 < len; i++) {
bs[i] = (byte) (Integer.parseInt(str.substring(i * 2, i * 2 + 2),
16) & 0xFF);
// System.out.println(str.substring(i * 2, i * 2 + 2)+":"+bs[i]);
}
return bs;
}
}
使用示例:
public void testCrypt(){
String str = "[email protected],http://wallimn.iteye.com";
String tmp = KeyUtil.getInstance().encode(str);
System.out.println("加密结果:"+ tmp);
System.out.println("解密结果:"+KeyUtil.getInstance().decode(tmp ));
}