[英]AES 256 decryption from C# net to Java
I am in need to convert below C# code to java , i have tried the possible ways and could not find the correct solution the result of the bot are quite different, In C# we get the correct data but in the Java value result is binary data. 我需要将下面的C#代码转换为Java,我已经尝试了可能的方法,但找不到正确的解决方案。bot的结果大不相同。在C#中,我们获得了正确的数据,但是在Java中,结果是二进制数据。 The Decryption technique used is AES 256 , May i know where i am going wrong in the java code. 使用的解密技术是AES 256,我可以知道我在Java代码中出了什么问题。
public static string AESDecryptText(string input, string key)
{
// Get the bytes of the string
byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
keyBytes = SHA256.Create().ComputeHash(keyBytes);
byte[] bytesDecrypted = AESDecrypt(bytesToBeDecrypted, keyBytes);
string result = Encoding.UTF8.GetString(bytesDecrypted);
return result;
}
private static byte[] AESDecrypt(byte[] bytesToBeDecrypted, byte[] keyBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(keyBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
Java Code for the above C# program 上述C#程序的Java代码
byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
final String KEY = "zszQ1nI8wT8XEcNONtbsbkgGRMLNFrq/X7+FBafQZn8=";
String pin = "pvdDFINyKi1DhgZmxCSvwVIR4TKhEI/momrLpGS3DB8=";
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec pbeKeySpec = new PBEKeySpec(KEY.toCharArray(), salt,
1000, 384);
Key generatedKey = factory.generateSecret(pbeKeySpec);
byte[] extractedKey = new byte[32];
byte[] iv = new byte[16];
System.arraycopy(generatedKey.getEncoded(), 0, extractedKey, 0, 32);
System.arraycopy(generatedKey.getEncoded(), 32, iv, 0, 16);
byte[] decoded = Base64.decodeBase64(pin);
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(extractedKey, "AES"), new IvParameterSpec(iv));
byte[] decValue = c.doFinal(decoded);
System.out.println("Result " + new String(decValue, StandardCharsets.UTF_8));
Be confident that given the same parameters for a standardized algorithm like AES, the results will be exactly the same. 确信给定像AES这样的标准化算法相同的参数,结果将完全相同。 Remember there are quite a few parameters that must all match. 请记住,有很多必须全部匹配的参数。 In this case, it looks like the Java version of your code overrides the padding parameter, while the C# version uses the default. 在这种情况下,代码的Java版本似乎覆盖了padding参数,而C#版本使用了默认值。 Within the debugger, review all the properties and make sure they match. 在调试器中,查看所有属性并确保它们匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.