使用AES加密,再使用base64编码加密。
具体细节请看注释。
/**
* 加密
*
* @param sSrc 加密的明文
* @param sKey 秘钥
* @param iv 向量 16 bytes
* @return
* @throws Exception
*/
public static String Encrypt(String sSrc, String sKey,String iv) throws Exception {
if (sKey == null) {
System.out.print("Key不能为空null");
return null;
}
if (sKey.length() != 16) {
System.out.print("Key的长度不是16位");
return null;
}
if (iv.length() != 16) {
System.out.print("iv的长度不是16位");
return null;
}
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv1 = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv1);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return new BASE64Encoder().encode(encrypted);
}
解密,如加密相似,唯一不同就是解密的参数是待解密文件。
/**
* 解密
* @param sSrc 接收到的加密过后的字符串(带解密密文)
* @param sKey 秘钥
* @return
* @throws Exception
*/
public static String Decrypt(String sSrc, String sKey,String iv) throws Exception {
try {
if (sKey == null) {
System.out.print("Key不能为空null");
return null;
}
if (sKey.length() != 16) {
System.out.print("Key的长度不是16位");
return null;
}
if (iv.length() != 16) {
System.out.print("iv的长度不是16位");
return null;
}
byte[] byte1 = Base64.decode(sSrc);//先用Base64解码
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
SecretKeySpec key = new SecretKeySpec(sKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
//与加密时不同MODE:Cipher.DECRYPT_MODE
byte[] ret = cipher.doFinal(byte1);
return new String(ret, "utf-8");
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
上述是我集成的方法,把加密(解密)文件、私钥、向量都作为参数进行的解析。
如若您想单独加密解密的话,请继续往下看。
/**
* AES加密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(LENGTH, new SecureRandom(defaultKey.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
return cipher.doFinal(content.getBytes(ENCODE));
}
/**
* AES解密
*
* @param encryptBytes
* @return
* @throws Exception
*/
public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(LENGTH, new SecureRandom(defaultKey.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
/**
* BASE64 加密
*
* @param content
* @return
* @throws Exception
*/
public static String base64Encode(byte[] bytes) {
return new BASE64Encoder().encode(bytes);
}
/**
* BASE64 解密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] base64Decode(String base64Code) throws Exception {
return isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
接下来测试一下:
public static void main(String[] args) throws Exception{
String pwd="123456";
String s = MD5Utils.md5LowerCase(pwd);
//这里使用的是md5加密后的前十六位作为私钥,后十六位作为向量
//私钥
String substring = s.substring(0, 16);
//向量
String substring1 = s.substring(16, 32);
System.out.println("私钥 :"+substring);
System.out.println("向量 :"+substring1);
String mobile="18621764382";
String encrypt1 = Encrypt(mobile, substring,substring1);
System.out.println("加密后的密文:"+encrypt1);
String decrypt1 = Decrypt(encrypt1, substring,substring1);
System.out.println("解密后的明文:"+decrypt1);
}
结果:
私钥 :e10adc3949ba59ab
向量 :be56e057f20f883e
加密后的密文:c85VnIGqV2rxsPT1lNviiA==
解密后的明文:18621764382