AES加密方式在win和linux 下面加密结果值不同
关于值为什么不同,原因是底层调用了Java 代码 native()那个方法。所以在不同的系统上加密后的值也就不同了。
解决办法:
下面例子本人亲测可以正常使用:
package com.example.bwjf.demo.util;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
public class AESutil
{
private static final Logger log = LoggerFactory.getLogger(AESutil.class);
public static String encrypt(String content, String secureKey)
{
try
{
if ((StringUtils.isEmpty(content)) ||
(StringUtils.isEmpty(secureKey))) {
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secureKey.getBytes());
kgen.init(128, secureRandom);
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(1, key);
byte[] result = cipher.doFinal(byteContent);
return encodeBASE64(result);
}
catch (Exception e)
{
log.error("��������.", e);
}
return null;
}
public static String decrypt(String content, String secureKey)
{
try
{
if ((StringUtils.isEmpty(content)) || (StringUtils.isEmpty(secureKey))) {
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secureKey.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, key);
byte[] base64Dec = Base64.decode(content);
byte[] result = cipher.doFinal(base64Dec);
return new String(result);
}
catch (Exception e)
{
log.warn("��������,����������:{}", e);
}
return null;
}
public static String encodeBASE64(byte[] content)
throws Exception
{
if ((content == null) || (content.length == 0)) {
return null;
}
try
{
return Base64.encode(content);
}
catch (Exception e)
{
log.error("Base64 encode error.", e);
}
return null;
}
}
或者
package com.example.bwjf.demo.util;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.IOException;
import java.io.PrintStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AESutil2
{
public static String Encrypt(String sSrc, String sKey)
{
try
{
if (sKey == null)
{
System.out.print("Key����null");
return null;
}
if (sKey.length() != 16)
{
System.out.print("Key��������16��");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return encryptBASE64(encrypted);
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
return null;
}
public static String Decrypt(String sSrc, String sKey)
{
try
{
if (sKey == null)
{
System.out.print("Key����null");
return null;
}
if (sKey.length() != 16)
{
System.out.print("Key��������16��");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, skeySpec);
byte[] encrypted1 = decryptBASE64(sSrc);
try
{
byte[] original = cipher.doFinal(encrypted1);
return new String(original, "utf-8");
}
catch (Exception e)
{
System.out.println(e.toString());
return null;
}
return null;
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
public static byte[] decryptBASE64(String key)
throws IOException
{
return new BASE64Decoder().decodeBuffer(key);
}
public static String encryptBASE64(byte[] key)
{
return new BASE64Encoder().encodeBuffer(key);
}
public static String encrypt(String content, String secureKey)
{
try
{
if ((StringUtils.isEmpty(content)) || (StringUtils.isEmpty(secureKey))) {
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secureKey.getBytes());
kgen.init(128, secureRandom);
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(1, key);
byte[] result = cipher.doFinal(byteContent);
return encodeBASE64(result);
}
catch (Exception e)
{
System.err.print("��������." + e);
}
return null;
}
public static String decrypt(String content, String secureKey)
{
try
{
if ((StringUtils.isEmpty(content)) || (StringUtils.isEmpty(secureKey))) {
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secureKey.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, key);
byte[] base64Dec = Base64.decode(content);
byte[] result = cipher.doFinal(base64Dec);
return new String(result);
}
catch (Exception e)
{
System.err.println("��������,����������:{}" + e);
}
return null;
}
public static String encodeBASE64(byte[] content)
throws Exception
{
if ((content == null) || (content.length == 0)) {
return null;
}
try
{
return Base64.encode(content);
}
catch (Exception e)
{
System.err.println("Base64 encode error." + e);
}
return null;
}
}