【问题标题】:Salesforce AES 128 decode in .NET.NET 中的 Salesforce AES 128 解码
【发布时间】:2020-07-08 15:21:16
【问题描述】:

我正在尝试解码在 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;

}

【问题讨论】:

  • 对不起,但它不允许我使用供应商给我的四位数密钥。
  • encryptWithManagedIV隐式存储密文前的 16 个字节 IV。因此,在 .NET 中解密时,必须在对传递的数据进行 Base64 解码后,显式分离 IV 和密文。是你做的吗?请发布您最近的 C# 代码。
  • 您想从字符串中读取密钥。请参阅链接中的 YD4 解决方案:aes.IV = "this is your IV"; // 你的静脉注射
  • 是的,我已经明确区分了 IV 和密文。我想我把钥匙弄乱了。我正在用我的 C# 代码更新答案。

标签: c# vb.net salesforce aes apex


【解决方案1】:

System.Security.Cryptography 你将不得不在那里做一些阅读。有了已经提供的脚本,它应该非常简单。

【讨论】:

    【解决方案2】:

    最终代码(在 VB.NET 中)是:

    Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim plaintext As String = ""
        Dim Hash_AES As New MD5CryptoServiceProvider
        Dim MD5Pass = Hash_AES.ComputeHash(Encoding.Default.GetBytes(pass))
        Dim Key As Byte() = MD5Pass
        Dim IV = New Byte(15) {}
        Dim phase As Byte() = Convert.FromBase64String(input)
        Array.Copy(phase, 0, IV, 0, IV.Length)
        Dim cipherText As Byte() = New Byte(phase.Length - 16 - 1) {}
        Array.Copy(phase, 16, cipherText, 0, cipherText.Length)
    
        Using aesAlg As AesManaged = New AesManaged()
            aesAlg.KeySize = 128
            aesAlg.Mode = CipherMode.CBC
            aesAlg.Padding = PaddingMode.PKCS7
            aesAlg.Key = Key
            aesAlg.IV = IV
            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
            Using msDecrypt As MemoryStream = New MemoryStream(cipherText)
                Using csDecrypt As CryptoStream = New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
                    Using srDecrypt As StreamReader = New StreamReader(csDecrypt)
                        plaintext = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        End Using
    
        Return plaintext
    End Function
    

    如有必要,Wich 可以轻松转换为 C#。我的错误是没有正确处理使用 MD5 的密钥派生。

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      相关资源
      最近更新 更多