c# - .NET 中的 Salesforce AES 128 解码
问题描述
我正在尝试解码在 Salesforce 中使用 AES 128 加密的字符串,并由供应商通过网络传递给我。
我知道他们如何加密字符串,供应商给了我一个用 Apex 编写的“解密脚本”(见下文),但我无法在 C#(或 VB.NET)中转置它。
加密功能(在 Apex 中):
public static String getCryptedBase64Text(String decryptedText, String encryptionKey)
{
Blob decryptedBlob = Blob.valueOf(decryptedText);
//GENERATE ENCRYPTION KEY
Blob blobKey = Blob.valueOf(encryptionKey);
Blob hashKey = Crypto.generateDigest('MD5', blobKey);
//CRYPT TEXT USING KEY
Blob encryptedBlob1 = Crypto.encryptWithManagedIV('AES128', hashKey, decryptedBlob);
String encryptedString1 = EncodingUtil.base64Encode(encryptedBlob1);
return encryptedString1;
}
解密功能(在 Apex 中):
public static String getPlainDecryptedText(String base64encrypted, String decryptionKey)
{
Blob encryptedBlob = EncodingUtil.base64Decode(base64encrypted);
//GENERATE DECRYPTION KEY
Blob blobKey = Blob.valueOf(decryptionKey);
Blob HashKey = Crypto.generateDigest('MD5', blobKey);
//DECRYPT TEXT USING KEY
Blob decryptedBlob = Crypto.decryptWithManagedIV('AES128', HashKey, encryptedBlob);
String decryptedString = decryptedBlob.toString();
return decryptedString;
}
我有供应商给我的加密密钥,它是一个四位数的字符串,如“abcd”。
知道字符串“decryptedText”和密钥“encryptionKey”,如何使用 C#(或 VB.NET)解密“decryptedText”?
到目前为止,这是我的 C# 代码:
public String Decrypt2(string encryptedbase64text, byte[] Key){
string plaintext;
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.Key = Key;
aesAlg.IV = IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
解决方案
System.Security.Cryptography 您将不得不在那里做一些阅读。使用已经提供的脚本,它应该非常简单。
推荐阅读
- javascript - 如何将一组独特的对象组合成一个对象?
- reactjs - 无法在 React-Redux 组件的 Jest/Enzyme 测试中获取节点
- jsp - 来自 jsp 的 Windows 10 Tomcat 日志记录
- javascript - Javascript 浏览器缩放对象
- javascript - Spring boot,WebSocket不向指定用户发送通知
- sql - 两个varchars之间的比较
- c# - RevenueCat Unity,错误:EntryPointNotFoundException:_RCSetupPurchases
- javascript - 在 React.js 中使用 Async/Await 并返回状态
- ionic3 - ionic 3检查ngfor循环中的数组中是否存在值
- docker - 如何部署具有 docker-compose 的 docker 应用程序?