用C#加密并使用Apex解密

问题描述 投票:0回答:2

我想将查询字符串中的字段从asp .net传递给Apex。我想加密该字段的值,然后在查询字符串中传递它。我不确定该如何处理,是否有相同的示例代码/链接?基本上我想用C#加密并使用Apex解密。

在C#中

 static string key = "eU5WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=";
 static string IV = "9ehY9gYtfIGlLRgwqg6F2g==";
    static void Main(string[] args) 
    {
        string source = "test";
        string encrypted = EncryptStringToBytes_Aes(source, Convert.FromBase64String(key), Convert.FromBase64String(IV));


        Console.ReadLine();
    }
static string EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        string encrypted;
        // Create an AesManaged object 
        // with the specified key and IV. 
        using (AesManaged aesAlg = new AesManaged())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = Convert.ToBase64String(msEncrypt.ToArray());// ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream. 
        return encrypted;

    }

在Apex中:

string cryptoKey='eU5WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=';
 String det= System.currentPageReference().getParameters().get('Det'); 
 Blob decryptedData = Crypto.decryptWithManagedIV('AES256', EncodingUtil.base64Decode(cryptoKey), EncodingUtil.base64Decode(det));

但是这不起作用,decryptedData.toString()不会作为'test'(原始文本)出现。我该如何解密呢?

c# .net salesforce apex-code
2个回答
0
投票

为什么?无论如何,与SF的所有通信都是通过SSL完成的(例如https://salesforce.stackexchange.com/questions/8273/data-loader-cli-and-encryption)。

如果绝对必须-Apex具有支持多种算法的Crypto类。希望您会在C#库中找到匹配的代码。

如果需要传递一些二进制数据(例如,以base64为例),也有EncodingUtil类。


0
投票

创建上述示例的人非常亲密。如果他们加密了比“测试”更长的字符串,他们可能会意识到这一点-他们只需要在要加密的内容之前插入一些填充即可:

string spadding =“ paddingxxxxxxxxx”;字符串来源=填充+“ Opportunity0065”;

-输出本来是“ Opportunity0065”

© www.soinside.com 2019 - 2024. All rights reserved.