将c#解密方法转换为android
本文关键字:android 转换 方法 解密 | 更新日期: 2023-09-27 18:14:50
我试图在JAVA中解密在c#中加密的字符串。在c#中,通过以下方法解密是成功的:
AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider();
aesProvider.BlockSize = 128;
aesProvider.KeySize = 128;
aesProvider.Key = strKey;
aesProvider.IV = strIV;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.Mode = CipherMode.CBC;
ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor(aesProvider.Key, aesProvider.IV);
byte[] DecryptedBytes = cryptoTransform.TransformFinalBlock(EncryptedBytes, 0, EncryptedBytes.Length);
return System.Text.Encoding.UTF8.GetString(DecryptedBytes);
我要做的是将此方法转换为JAVA (Android),这是我尝试过的,但得到一个错误消息("Pad块损坏"):
try
{
byte[] IV = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
byte[] KEY = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
SecretKeySpec key = new SecretKeySpec(KEY, "AES/CBC/PKCS7Padding");
Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
byte[] bytesData = Utilities.Base64Coder.decode(data);
Log.i("bytesData", String.valueOf(bytesData.length));
String strResult = new String(cipher.doFinal(bytesData));
Log.i("decrypted string", strResult);
return strResult;
}
catch (Exception e) {
Log.i("decrypted FAILED", e.getMessage());
return null;
}
10 x:)
对于那些曾经搜索并得到这个问题的人-这段代码工作得很好,问题在于c#和Java之间的键不同。
GregS - 10X提醒我再次检查