package test;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
/**
* Created by raymond on 2015/11/2.
*/
public class DcAesUtil {
private static final String KEY = "amRoZmR1ODRyODRvamZ5Nw==";
// private static final String SKEY = "amRoZmR1ODRyODRvamZ5Nw==amRoZmR1ODRyODRvamZ5Nw==";
/**
* 加密
*
* @param content 需要加密的内容
* @return
*/
public static byte[] encrypt(String content) {
SecureRandom random = null;
try {
random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY.getBytes());
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param content 待解密内容
* @return
*/
public static byte[] decrypt(byte[] content) {
SecureRandom random = null;
try {
random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY.getBytes());
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// kgen.init(128, new SecureRandom(password.getBytes()));
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static String encodeAesBase64(String content) {
byte[] byteArray = DcAesUtil.encrypt(content);
char[] baseArray = Base64EncodeUtil.encode(byteArray);
return new String(baseArray);
}
public static String decoderAesBase64(String content) {
byte[] byteArray = Base64.decodeBase64(content);
byte[] resultArray = DcAesUtil.decrypt(byteArray);
return new String(resultArray);
}
/* public static void main(String[] args) {
//加密
//System.out.println("加密前:"+"dc2015");
System.out.println("加密前:"+"abcd");
// System.out.println("加密后:"+encodeAesBase64("dc2015"));
System.out.println("加密后:"+encodeAesBase64("abcd"));
//System.out.println("解密后:"+decoderAesBase64("wRPOki8ykaeV568CsOfZrg"));
// System.out.println("解密后:"+decoderAesBase64("wRPOki8ykaeV568CsOfZrg"));
}*/
}
最近下载更多
最近浏览更多
qq1176648922 LV6
2022年10月24日
1358849392 LV21
2022年10月21日
1435792946 LV1
2022年5月9日
zzuljh LV9
2021年12月16日
a5366869 LV7
2021年9月20日
itcaizhe LV9
2021年7月26日
1798672867 LV21
2021年7月18日
lris_luanling LV11
2021年5月27日
wwkddjjj LV8
2021年5月9日
zyzyzy1 LV1
2021年5月6日

