TripleDES加密在通用应用程序c# WP 8.1
本文关键字:WP 应用程序 加密 TripleDES | 更新日期: 2023-09-27 18:03:52
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;
}