通用应用程序 c# WP 8.1 中的 TripleDES 加密

TripleDES encryption in universal app c# WP 8.1

public static string tripleDESEncryptor(string toEncrypt, string keyString)
{

    var crypt = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcb);
    IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);
    IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(keyString, BinaryStringEncoding.Utf8);
    CryptographicKey key = crypt.CreateSymmetricKey(keyBuffer);            
    var sigBuffer = CryptographicEngine.Sign(key, buffer);  // throws exception here
    string signature = CryptographicBuffer.EncodeToBase64String(sigBuffer);
    return signature;
}

这会抛出 InvalidCastException,我似乎无法弄清楚为什么或哪里做错了。

keyString 的长度为 24。

如有任何帮助,我们将不胜感激。

感谢您的帮助,这对我很有效 -

public static string tripleDESEncryptor(string toEncrypt, string keyString)
{
   var crypt = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcbPkcs7);

            IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);
            IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(keyString, BinaryStringEncoding.Utf8);

            CryptographicKey key = crypt.CreateSymmetricKey(keyBuffer);

            IBuffer signed = CryptographicEngine.Encrypt(key, buffer, null);

            string signature = CryptographicBuffer.EncodeToBase64String(signed);
            return signature;
}