将加密方法从Java转换为C#
本文关键字:转换 Java 加密 方法 | 更新日期: 2023-09-27 18:21:00
我需要使用C#实现一个加密和解密方法对,该方法使用"AES/EBC/PKCS5Padding"。原始代码是用Java编写的。以下是Java中的加密方法:
public static String Encrypt(String plainText, byte[] key2) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
byte[] encryptedTextBytes=null;
byte[] key3 =null;
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key3= sha.digest(key2);
key3 = copyOf(key3, 16);
SecretKeySpec keySpec = new SecretKeySpec(key3, "AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
return new Base64().encode(encryptedTextBytes);
}
这是我在C#中重建它的尝试:
public static string Encrypt_AES(string plainText, byte[] key2)
{
var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] key3 = new byte[16];
sha.TransformFinalBlock(key2, 0, key2.Length);
var tmpkey = sha.Hash;
Array.Copy(tmpkey, key3, 16);
var aes = new System.Security.Cryptography.AesCryptoServiceProvider();
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
aes.Mode = System.Security.Cryptography.CipherMode.ECB;
aes.Key = key3;
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
var encryptor = aes.CreateEncryptor();
byte[] encryptedTextBytes = encryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length);
return Convert.ToBase64String(encryptedTextBytes);
}
在加密某些内容并将其发送到远程服务后,该服务会返回一个错误,表示无法解密消息。所以我认为它有问题。
我还有一个Java中的解密方法示例。我也实现了这种方法,并尝试在本地加密和解密一些文本。当我这样做时,Decrypt_AES方法会向TransformFinalBlock()抛出一个CryptographicException
,说"填充无效,无法删除。"也许我用错了CryptoProvider类?
以下是解密函数的Java和C#版本:Java
public static String Decrypt(String encryptedText, byte[] key2) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
byte[] decryptedTextBytes=null;
byte[] key3 =null;
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key3= sha.digest(key2);
key3 = copyOf(key3, 16);
SecretKeySpec keySpec = new SecretKeySpec(key3, "AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] encryptedTextBytes = new Base64().decode(encryptedText);
decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
return new String(decryptedTextBytes);
}
C#
public static string Decrypt_AES(byte[] key2, string encryptedText)
{
var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] key3 = new byte[16];
sha.TransformFinalBlock(key2, 0, key2.Length);
var tmpkey = sha.Hash;
Array.Copy(tmpkey, key3, 16);
var aes = new System.Security.Cryptography.AesCryptoServiceProvider();
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
aes.Mode = System.Security.Cryptography.CipherMode.ECB;
aes.Key = key3;
var encryptedBytes = Encoding.UTF8.GetBytes(encryptedText);
var decryptor = aes.CreateDecryptor();
var decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return System.Text.Encoding.UTF8.GetString(decryptedBytes);
}
感谢您提前提供的任何提示!
您的解密方法不是Base64解码密文。
var encryptedBytes = Encoding.UTF8.GetBytes(encryptedText);
应该改成类似的东西
var encryptedBytes = Convert.FromBase64String(encryptedText);