import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class DesEncrypter {
public static final String TRANSFORMATION="DES";
public static final String ENCODING="UTF-8";
public static final int KEY_LEN=16;
private Cipher ecipher=null;
private Cipher dcipher=null;
private SecretKey key = null;
//构造函数,使用系统随机生成的密钥
public DesEncrypter(){
if(key==null){
try {
key=KeyGenerator.getInstance(TRANSFORMATION).generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
init();
}
//构造函数,使用自定义的密钥,此密钥可用于解密
public DesEncrypter(String key){
setSecretKey(key);
}
private void init(){
try {
ecipher=Cipher.getInstance(TRANSFORMATION);
dcipher=Cipher.getInstance(TRANSFORMATION);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
e.printStackTrace();
}
}
//加密函数 str为要加密的字符
@SuppressWarnings("finally")
public String encrypt(String str){
if(str==null){
return null;
}
String encryptedText=null;
try{
byte[] temp=str.getBytes(ENCODING);
byte[] enc=ecipher.doFinal(temp);
encryptedText="";
for(int i=0;i<enc.length;i++){
encryptedText +=byteToHex(enc[i]);
}
}catch(Exception e){
e.printStackTrace();
}finally{
return encryptedText;
}
}
//解密函数,str为需解密的数据
@SuppressWarnings("finally")
public String decrypt(String str){
if(str == null){
return null;
}
String decryptedText=null;
try{
int length=str.length()/2;
byte[] dec = new byte[length];
for(int i=0;i<length;i++){
dec[i]=hexToByte(str.substring(2*i,2*i+2));
}
byte[] temp=dcipher.doFinal(dec);
decryptedText=new String(temp,ENCODING);
}catch(Exception e){
}finally{
return decryptedText;
}
}
private String byteToHex(byte ib){
char[] Digit={
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'
};
char[] ob=new char[2];
ob[0]=Digit[(ib>>>4)&0X0F];
ob[1]=Digit[ib&0X0F];
String s=new String(ob);
return s;
}
private byte hexToByte(String str){
byte ib=0;
char[] c=str.toCharArray();
try{
ib=Byte.decode("0x"+c[0]).byteValue();
ib<<=4;
ib|=Byte.decode("0x"+c[1]).byteValue();
}catch(NumberFormatException nfe){
return 0;
}
return ib;
}
private String bytesToHex(byte[] bytes){
StringBuffer sb=new StringBuffer();
for(int i=0;i<bytes.length;i++){
sb.append(byteToHex(bytes[i]));
}
return sb.toString();
}
public String getSecretKey(){
String skey=null;
if(key!=null){
skey="";
byte[] temp=key.getEncoded();
for(int i=0;i<temp.length;i++){
skey+=byteToHex(temp[i]);
}
}
return skey;
}
public void setSecretKey(String skey){
int length=skey.length();
StringBuffer stringbuffer = new StringBuffer();
stringbuffer.append(bytesToHex(skey.getBytes()));
if (stringbuffer.length() <= KEY_LEN) {
long l1 = KEY_LEN - stringbuffer.length();
for (int k = 0; k < l1; ++k) {
stringbuffer.insert(0, "0");
}
skey = stringbuffer.toString();
}
else {
skey = stringbuffer.substring(stringbuffer.length() - KEY_LEN);
}
length=skey.length();
length/=2;
byte[] bkey=new byte[length];
for(int i=0;i<length;i++){
bkey[i]=hexToByte(skey.substring(2*i, 2*i+2));
}
key=new SecretKeySpec(bkey,TRANSFORMATION);
init();
}
public void setSecretKey1(String skey){
int length=skey.length();
if(length%2!=0){
return;
}
length/=2;
byte[] bkey=new byte[length];
for(int i=0;i<length;i++){
bkey[i]=hexToByte(skey.substring(2*i, 2*i+2));
}
key=new SecretKeySpec(bkey,TRANSFORMATION);
init();
}
}
使用方法,
DesEncrypter des=new DesEncrypter("xumian");
//加密
String enStr=des.encrypt("need to encrypt");
//解密
String deStr=des.decrypt(enStr);
System.out.println(deStr);