package test;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/*实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
* KeyGenerator 提供对称密钥生成器的功能,支持各种算法
* KeyGenerator keygen =KeyGenerator.getInstance("AES");
* 【1】生成随机;二选一
* keygen.init(128);
* 【2】根据口令生成;二选一
* keygen.init(128, new SecureRandom(password.getBytes()));
* 生成密钥
* SecretKey deskey =keygen.generateKey();
* 保存为byte数组
* byte[] key=deskey.getEncoded();
*/
/*将数组转换成密钥
* byte[] key ={ 117, -1, -107, -53, 125, -94, -67, 102, 55, 66, -90, 81, -70, -96, -95,-83 };
* SecretKey deskey = new SecretKeySpec(key, "AES");
*/
public class EncrypAES
{
private static byte[] StrKey2ByteArrayKey(String key) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(key.getBytes()));
kgen.init(128, new SecureRandom(key.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
//String s=new String(secretKey.getEncoded(),"UTF-8");
//System.out.println(s);
return enCodeFormat;
}
/**
* Encrytor<对字符串加密>
*
* @param param
* src<要加密的byte数组,二进制>
* @param key
*
*
* @return byteArray<二进制>
*/
public static byte[] Encrytor(byte src[], byte[] key)
throws Exception
{
// SecretKey 负责保存对称密钥
SecretKey deskey = new SecretKeySpec(key, "AES");
// Cipher负责完成加密或解密工作,生成Cipher对象,指定其支持的AES,DES算法
Cipher c = Cipher.getInstance("AES");
Security.addProvider(new com.sun.crypto.provider.SunJCE());
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
// 该字节数组负责保存加密的结果, 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
return cipherByte;
}
private static byte[] Encrytor(byte[] src, String key) throws Exception
{
byte[] enCodeFormat = StrKey2ByteArrayKey(key);
return Encrytor(src, enCodeFormat);
}
public static String Encrytor(String str, String key)
throws Exception
{
byte[] src = str.getBytes("utf-8");
return Str.ByteArray2HexStr(Encrytor(src, key));
}
/**
* Encrytor<对字符串加密>
*
* @param param
* str<要加密的字符串>
* @param key
*
*
* @return String
*/
public static String Encrytor(String str, byte[] key)
throws Exception
{
byte[] src = str.getBytes("utf-8");
return Str.ByteArray2HexStr(Encrytor(src, key));
}
/**
* Decryptor<对字符串解密>
*
* @param enstr
* <要解密的字符串Hexstr>
* @param key
*
*
* @return String
*/
public static byte[] Decryptor(byte[] enstr, byte[] key)
throws Exception
{
// SecretKey 负责保存对称密钥
SecretKey deskey = new SecretKeySpec(key, "AES");
// Cipher负责完成加密或解密工作,生成Cipher对象,指定其支持的AES,DES算法
Cipher c = Cipher.getInstance("AES");
Security.addProvider(new com.sun.crypto.provider.SunJCE());
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示解密模式
c.init(Cipher.DECRYPT_MODE, deskey);
// 该字节数组负责保存加密的结果
byte[] cipherByte = c.doFinal(enstr);
return cipherByte;
}
private static byte[] Decryptor(byte[] src, String key) throws Exception
{
byte[] enCodeFormat = StrKey2ByteArrayKey(key);
return Decryptor(src, enCodeFormat);
}
public static String Decryptor(String enstr, String key)
throws Exception
{
byte[] buff = Str.HexStr2ByteArray(enstr);
return new String(Decryptor(buff, key), "utf-8");
}
/**
* Decryptor<对字符串解密>
*
* @param enstr
* <要解密的字符串Hexstr>
* @param key
*
*
* @return String
*/
public static String Decryptor(String enstr, byte[] key)
throws Exception
{
byte[] buff = Str.HexStr2ByteArray(enstr);
Decryptor(buff, key);
return new String(Decryptor(buff, key), "utf-8");
}
//
/**
* main
*/
public static void main(String[] args) throws Exception
{
String msg = "12345678";
System.out.println("字符数组密钥-----------------------------");
byte[] key={107, -76, -125, 126, -73, 67, 41, 16, 94, -28, 86, -115, -38, 125, -58, 126};
String encontent = EncrypAES.Encrytor(msg, key);
String decontent = EncrypAES.Decryptor(encontent, key);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + encontent);
System.out.println("解密后:" + decontent);
System.out.println("字符串密钥-----------------------------");
String keys="123456";
encontent = EncrypAES.Encrytor(msg, keys);
decontent = EncrypAES.Decryptor(encontent, keys);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + encontent);
System.out.println("解密后:" + decontent);
}
}
其中使用的工具类Str见《String工具类(java)》