C# 实现 JAVA AES加密解密[原创]

时间:2022-02-19 00:22:31

以下是网上普遍能收到的JAVA AES加密解密方法。

因为里面用到了KeyGenerator 和 SecureRandom,但是.NET 里面没有这2个类。无法使用安全随机数生成KEY。

我们在接收JAVA发送的AES加密字符串后,在.NET没有对应的KeyGenerator 和 SecureRandom去生成AES 的 KEY值,导致无法直接解密。

 import java.security.SecureRandom;
import java.util.Base64; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; public class javaaes {
public static void main(String[] args) throws Exception{
byte[] a = AesEncrypt("123456".getBytes(),"abcd1234");
System.out.println(new String(Base64.getEncoder().encodeToString(a)));
}
//AES加密
public static byte[] AesEncrypt(byte[] byteContent, String password) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom); SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();//AES加密实际的Key值
//如果直接enCodeFormat=password.getBytes(),那.NET直接就可以解密 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(byteContent);
}
//AES解密
public static byte[] AesDecrypt(byte[] byteContent, String password) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.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(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(byteContent);
} }

第一种方法是,在JAVA中将AES的密钥直接生成出实际的key值,在.NET中用这个实际的key去解密。

     public static void main(String[] args) throws Exception{
String password = "1234567890";//AES的密钥
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom); SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();//AES加密实际的Key值
System.out.println(new String(Base64.getEncoder().encodeToString(enCodeFormat)));
}
         /// <summary>
/// AES解密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DeAES(byte[] content, string key)
{
using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
{
aesProvider.Key = Convert.FromBase64String(key);
aesProvider.Mode = CipherMode.ECB;
aesProvider.Padding = PaddingMode.PKCS7;
using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
{
byte[] inputBuffers = content;
byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, , inputBuffers.Length);
aesProvider.Clear();
return Encoding.UTF8.GetString(results);
}
}
}

第二种方法是,下载IKVM,解压缩后在VS的项目中引用bin目录下的IKVM.Runtime.dll、IKVM.OpenJDK.Core.dll、IKVM.OpenJDK.Security.dll

 using java.security;
using javax.crypto;
using System.Security.Cryptography;
using System.Text;
using System; public class Program
{
public static void Main()
{
byte[] a = Convert.FromBase64String("W0fSdgixanavXMiDdPXa/Q==");
string result = DeAES(a, "abcd1234");
Console.WriteLine(result);
Console.Read();
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DeAES(byte[] content, string key)
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(Encoding.ASCII.GetBytes(key));
kgen.init(, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded(); using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
{
aesProvider.Key = enCodeFormat;
aesProvider.Mode = CipherMode.ECB;
aesProvider.Padding = PaddingMode.PKCS7;
using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
{
byte[] inputBuffers = content;
byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, , inputBuffers.Length);
aesProvider.Clear();
return Encoding.UTF8.GetString(results);
}
}
} }

C# 实现 JAVA AES加密解密[原创]

C# 实现 JAVA AES加密解密[原创]

下载地址:https://github.com/zhu-xb/AES-Cryptography