用c#加密,用Apex解密
本文关键字:解密 Apex 加密 | 更新日期: 2023-09-27 18:09:33
我想传递一个字段在查询字符串从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()不作为'test'(原始文本)出现。如何解密?
为什么?与SF的所有通信都是通过SSL完成的(例如https://salesforce.stackexchange.com/questions/8273/data-loader-cli-and-encryption)。
如果你绝对必须- Apex有Crypto类,支持多种算法。希望你能在c#库中找到匹配的。
还有EncodingUtil类,如果你需要传递一些二进制数据(如base64)。
创造上述例子的人非常接近。如果他们加密了一个比"Test"更长的字符串,他们可能会意识到这一点——他们只需要在加密的内容前面插入一些填充:
string spadding = "paddingxxxxxxxxx";string source = spadding +"机会0065";
—输出应该是"机会0065"