0

我想将查询字符串中的一个字段从 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;

    }

在顶点:

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

但这不起作用,decryptedData.toString() 不是“测试”(原文)。我如何解密回来?

4

2 回答 2

0

Why? All communication with SF is done via SSL anyway (https://salesforce.stackexchange.com/questions/8273/data-loader-cli-and-encryption for example).

If you absolutely have to - Apex has Crypto class that supports several algorithms. Hopefully you'll find matching ones in C# libraries.

There's also EncodingUtil class if you need to pass some binary data (as base64 for example).

于 2013-04-16T17:30:10.943 回答
0

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

字符串 spadding = "paddingxxxxxxxxx"; 字符串来源 = spadding + "Opportunity0065";

-- 输出应该是“Opportunity0065”

于 2019-12-21T20:48:16.847 回答