package main;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class CryptoUtil {
public static Key DEFAULT_KEY = null;
public static final String DEFAULT_ENCODE_RULES = CryptoUtil.class.getName();
public static final String DES = "DES";
public static void main(String[] args) {
String a = "20161021142843100001yagnpu123";
String key = DEFAULT_ENCODE_RULES + "yangpu1212";
System.out.println(a);
String strEnc = CryptoUtil.encode(key, a);// 加密字符串,返回String的密文
System.out.println(strEnc);
String strDes = CryptoUtil.decode(key, strEnc);// 把String 类型的密文解密
System.out.println(strDes);
}
static {
DEFAULT_KEY = obtainKey(DEFAULT_ENCODE_RULES);
}
/**
* 获得key
**/
public static Key obtainKey(String key) {
if (key == null) {
return DEFAULT_KEY;
}
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(DES);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
generator.init(new SecureRandom(key.getBytes()));
Key key1 = generator.generateKey();
generator = null;
return key1;
}
/**
* 加密<br>
* String明文输入,String密文输出
*/
public static String encode(String str) {
return encode(null, str);
}
/**
* 加密<br>
* String明文输入,String密文输出
*/
public static String encode(String key, String str) {
return byteToString(obtainEncode(key, str.getBytes()));
}
/**
* 解密<br>
* 以String密文输入,String明文输出
*/
public static String decode(String str) {
return decode(null, str);
}
/**
* 解密<br>
* 以String密文输入,String明文输出
*/
public static String decode(String key, String str) {
return new String(obtainDecode(key, stringToByte(str)));
}
/**
* 加密<br>
* 以byte[]明文输入,byte[]密文输出
*/
private static byte[] obtainEncode(String key, byte[] str) {
byte[] byteFina = null;
Cipher cipher;
try {
Key key1 = obtainKey(key);
cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key1);
byteFina = cipher.doFinal(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密<br>
* 以byte[]密文输入,以byte[]明文输出
*/
private static byte[] obtainDecode(String key, byte[] str) {
Cipher cipher;
byte[] byteFina = null;
try {
Key key1 = obtainKey(key);
cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, key1);
byteFina = cipher.doFinal(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 将byte转换成string
*/
public static String byteToString(byte[] b) {
// 转成16进制字符串
StringBuilder sb = new StringBuilder();
String stmp = "";
for (int n = 0; n < b.length; n++) {
// 整数转成十六进制表示
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
sb.append("0" + stmp);
} else {
sb.append(stmp);
}
}
return sb.toString().toUpperCase(); // 转成大写
}
/**
* 将string转换成byte
*/
public static byte[] stringToByte(String str) {
byte[] b = str.getBytes();
if ((b.length % 2) != 0)
throw new IllegalArgumentException(b + ":length is not even numbers");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
}
DES--可逆加密算法-密文可控
最新推荐文章于 2024-03-11 12:22:25 发布