import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
public class EncryptUtil {
public static void main(String[] args) {
String sql = "";
System.out.println(decrypt(sql));
}
private static final String ALGORITHM = "DESede";
static final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x28,
0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74,
(byte) 0x98, 0x30, 0x40, 0x36, (byte) 0xE2};
public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"};
public static String encrypt(String info) {
try {
byte[] newInfo = encrypt(info.getBytes());
return new BASE64Encoder().encode(newInfo).replaceAll("\r\n", "").replaceAll("\n", "");
} catch (Exception ex) {
return null;
}
}
public static String decrypt(String info) {
try {
byte[] newInfo = decrypt(new BASE64Decoder().decodeBuffer(info));
if (newInfo != null)
return new String(newInfo);
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
public static byte[] encrypt(byte[] info) {
return cryptanalyze(Cipher.ENCRYPT_MODE, info);
}
public static byte[] decrypt(byte[] info) {
return cryptanalyze(Cipher.DECRYPT_MODE, info);
}
private static byte[] cryptanalyze(int mode, byte[] info) {
try {
SecretKey deskey = new SecretKeySpec(keyBytes, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode, deskey);
byte[] newInfo = cipher.doFinal(info);
return newInfo;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private static MessageDigest digest = null;
public synchronized static String hash(String data) {
if (data == null || data == "") {
return null;
}
final String algorithm = "MD5";
if (digest == null) {
try {
digest = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException nsae) {
throw new RuntimeException("不支持该加密算法:" + algorithm);
}
}
try {
digest.update(data.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
}
return encodeHex(digest.digest());
}
private static String encodeHex(byte[] bytes) {
StringBuffer hex = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
hex.append("0");
}
hex.append(Integer.toString((int) bytes[i] & 0xff, 16));
}
return hex.toString();
}
private static Random randGen = new Random();
private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
public static final String randomString(int length) {
if (length < 1) {
return null;
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
}
return new String(randBuffer);
}
}