使用C#解密在Salesforce中创建的AES256值 [英] Decrypt AES256 value created in Salesforce using C#
本文介绍了使用C#解密在Salesforce中创建的AES256值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static String getEncryptedData(){
Blob cryptoKey = Crypto.generateAesKey(256);
String dataToEncrypt ='测试字符串';
Blob encryptedData = Crypto.encryptWithManagedIV('AES256',cryptoKey,Blob.valueOf(dataToEncrypt));
返回EncodingUtil.base64Encode(encryptedData);
}
假设这个实现是正确的,我需要在C#中解密它。我有以下,但看到一个填充无效,不能被删除的错误。 (当然,样本密钥和加密的字符串值):
私有字符串解密(string encryptedbase64Password)
{
RijndaelManaged aes256 = new RijndaelManaged();
aes256.KeySize = 256;
aes256.Mode = CipherMode.CBC;
aes256.Padding = PaddingMode.PKCS7;
aes256.BlockSize = 128;
// Salesforce.com将前16个字节存储为IV
//将前16个字节提取为IV,其余作为Key
string keyAndIv =Ii7oSjjWuhp6J6 / hj / wmivqx1h3N2HzJ2ByJOy1n89E =; //从SFDC的样本
//为此示例硬编码:
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 [] decryptptedText = decrypto.TransformFinalBlock(encryptedbytes,0,encryptedbytes.Length);
string result = Convert.ToBase64String(decryptptedText);
返回结果;
}
我做错了什么?
解决方案
用户双头软件在 Salesforce开发人员板看起来好像CryptoStream正确处理填充问题完全解决方案:
string plaintext;
byte [] Key = Convert.FromBase64String(Ii7oSjjWuhp6J6 / hj / wmivqx1h3N2HzJ2ByJOy1n89E =);
string encryptedbase64Password =hRVlbM79aEQi8Tz7JJIL7CEhSxZAJvCh8Ni6ORP1C55 + qbJzjDshBYBjyP12 / zT2;
byte [] IV =新字节[16];
byte [] phase = Convert.FromBase64String(encryptedbase64Password);
Array.Copy相位,0,IV,0,IV.Length);
byte [] cipherText = new byte [phase.Length - 16] ;;
Array.Copy(phase,16,cipherText,0,cipherText .Length);
使用(AesManaged aesAlg = new AesManag ed())
{
aesAlg.KeySize = 256;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
aesAlg.Key = Key;
aesAlg.IV = IV;
//创建一个解密器来执行流转换。
//注意:这是我的原始解决方案与正确的解决方案之间的区别。
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key,aesAlg.IV);
//创建用于解密的流。
使用(MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using(CryptoStream csDecrypt = new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
{
使用(StreamReader srDecrypt = new StreamReader(csDecrypt))
{
//从解密流读取解密的字节,并将它们放在字符串中。
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
Running the following Apex code in Salesforce, I encrypt a string:
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);
}
Assuming this implementation is correct, I need to decrypt it in C#. I have the following, but am seeing a "Padding is invalid and cannot be removed" error. (Sample key & encrypted string value, of course):
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;
}
What am I doing wrong?
解决方案
User 'Doublehead Software" posted a correct solution over on the Salesforce developer boards. It appears as though CryptoStream handles the padding issue correctly. Full solution:
string plaintext;
byte[] Key = Convert.FromBase64String("Ii7oSjjWuhp6J6/hj/wmivqx1h3N2HzJ2ByJOy1n89E=");
string encryptedbase64Password = "hRVlbM79aEQi8Tz7JJIL7CEhSxZAJvCh8Ni6ORP1C55+qbJzjDshBYBjyP12/zT2";
byte[] IV = new byte[16];
byte[] phase = Convert.FromBase64String(encryptedbase64Password);
Array.Copy(phase, 0, IV, 0, IV.Length);
byte[] cipherText = new byte[phase.Length - 16];;
Array.Copy(phase, 16, cipherText, 0, cipherText.Length);
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.KeySize = 256;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decryptor to perform the stream transform.
// NOTE: This is the difference between my original solution and the correct one.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
这篇关于使用C#解密在Salesforce中创建的AES256值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文