在 Salesforce 中运行以下 Apex 代码,我加密了一个字符串:
public static String getEncryptedData() {
Blob cryptoKey = Crypto.generateAesKey(256);
String dataToEncrypt = 'Test string';
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, Blob.valueOf(dataToEncrypt));
return EncodingUtil.base64Encode(encryptedData);
}
假设这个实现是正确的,我需要在 C# 中解密它。我有以下内容,但看到“填充无效且无法删除”错误。(示例密钥和加密字符串值,当然):
private string Decrypt(string encryptedbase64Password)
{
RijndaelManaged aes256 = new RijndaelManaged();
aes256.KeySize = 256;
aes256.Mode = CipherMode.CBC;
aes256.Padding = PaddingMode.PKCS7;
aes256.BlockSize = 128;
// Salesforce.com stores the first 16 bytes as the IV
// Extract first 16 bytes as IV, the rest as the Key
string keyAndIv = "Ii7oSjjWuhp6J6/hj/wmivqx1h3N2HzJ2ByJOy1n89E="; // sample from SFDC
// hard coded for this example:
encryptedbase64Password = "hRVlbM79aEQi8Tz7JJIL7CEhSxZAJvCh8Ni6ORP1C55+qbJzjDshBYBjyP12/zT2";
byte[] allBytes = Convert.FromBase64String(keyAndIv);
byte[] iv = new byte[16];
Array.Copy(allBytes, 0, iv, 0, iv.Length);
byte[] key = new byte[allBytes.Length - 16];
Array.Copy(allBytes, 16, key, 0, key.Length);
aes256.Key = key;
aes256.IV = iv;
ICryptoTransform decrypto = aes256.CreateDecryptor();
byte[] encryptedbytes = Convert.FromBase64String(encryptedbase64Password);
byte[] decryptedText = decrypto.TransformFinalBlock(encryptedbytes, 0, encryptedbytes.Length);
string result = Convert.ToBase64String(decryptedText);
return result;
}
我究竟做错了什么?