/**
*
*/
package com.csair.session.memcache;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* @author Administrator
*
*/
public class TripleDESEncrypt {
private static final String Algorithm = "DESede";
// public static byte[] keyBytes = "HjgoO8cvnRJeFu0Scj8EtNPR".getBytes();
public static byte[] keyBytes = "hello world";//密钥
private static TripleDESEncrypt encrypt = new TripleDESEncrypt();
public static TripleDESEncrypt getInstance()
{
return encrypt;
}
// 24字节的密钥
/*
* @ use DESede algorithm to enc the src
*
* @ keybyte: secretkey 24 byte
*
* @ src:the text needs to be encrypt
*
* @ return the enc result
*/
public byte[] encryptMode(byte[] src) {
try {
System.out.println("密钥:"+new String(keyBytes,"utf-8"));
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//加密
public byte[] encrypt(String src) throws Exception
{
String srcString = encode(encryptMode(src.getBytes("UTF-8")));
return srcString.getBytes("UTF-8");
}
/**
* 解密(currentKey解密失败,使用preKey解密)
* @param src
* @return
* @throws Exception
* @throws Exception
*/
public String decrypt(byte[] src) throws Exception{
byte[] srcBytes = null;
String decryptStr = "";
String srcString = new String(src,"UTF-8");
try {
//keyBytes = CryptoKeyUtil.getCryptoKey(CryptoKeyUtil.CURRENT).getBytes();
srcBytes = decryptMode(decode(srcString),keyBytes);
} catch (Exception e) {
keyBytes = CryptoKeyUtil.getCryptoKey(CryptoKeyUtil.PRE).getBytes();
System.out.println("*****************currentKey解密失败,使用preKey解密******************");
srcBytes = decryptMode(decode(srcString),keyBytes);
e.printStackTrace();
}
decryptStr = new String(srcBytes,"UTF-8");
return decryptStr;
}
public byte[] decryptMode(byte[] src , byte[] keyBytes) throws Exception{
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
}
/*
* @ use DESede algorithm to dec the src
*
* @ keybyte: secretkey 24 byte
*
* @ src:the text needs to be dec
*
* @ return the dec result
*/
public byte[] decryptMode(byte[] src) {
try {
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
private void writeFile(String src) throws Exception {
File file = new File("C://encrypt.txt");
FileWriter fw = new FileWriter(file);
fw.write(src);
fw.close();
}
private String readFile(String path) {
File file = new File(path);
FileReader fr = null;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
char[] cbuf = new char[10240];
try {
fr.read(cbuf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String res = new String(cbuf);
return res;
}
/**
* 编码
*
* @param bstr
* @return String
*/
public String encode(byte[] bstr) {
return new sun.misc.BASE64Encoder().encode(bstr);
}
/**
* 解码
*
* @param str
* @return string
*/
public byte[] decode(String str) {
byte[] bt = null;
try {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
bt = decoder.decodeBuffer(str);
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}
/**
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
TripleDESEncrypt encrypt = new TripleDESEncrypt();
// TODO Auto-generated method stub
Security.addProvider(new com.sun.crypto.provider.SunJCE());
// String keyString ="This is a secret keynews";
// byte[] keyBytes = keyString.getBytes();
// System.out.println("密钥是:"+new String(keyBytes));
String szSrc = "AFE0AFE6-8DBA-E6CB-3240-103AC6F94766";
System.out.println("加密前的字符串:" + szSrc);
byte[] encoded = TripleDESEncrypt.getInstance().encryptMode(szSrc.getBytes("UTF-8"));
String encodedString = encrypt.encode(encoded);
System.out.println("加密后的字符串:" + encodedString);
//encodedString = "EBtjunqwdP67jCPdyAg0rnpGJed/nHTo32EdvpLNSd3+OGgW+BsNow==";
String urlEncodeStr = URLEncoder.encode(encodedString, "UTF-8");
System.out.println("UTF-9编码的字符串:" + urlEncodeStr);
try {
encrypt.writeFile(encodedString);
} catch (Exception e) {
e.printStackTrace();
}
byte[] srcBytes = TripleDESEncrypt.getInstance().decryptMode(encrypt.decode(encodedString));
System.out.println("解密后的字符串:" + (new String(srcBytes,"UTF-8")));
}
}