本文介绍了将AesManaged转换为新的通用应用程序CryptographicEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已删除了此旧代码,应将其翻译为使用新的CryptographicEngine。但是我对新API的可能性不知所措。
I've this old code snipped that should be translated to use the new CryptographicEngine. But I'm overwhelmed by the possibilities of the new API.
有人可以帮助我吗?
private AesManaged GetAes(string textkey)
{
var aes = new AesManaged();
aes.IV = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
var key = System.Text.Encoding.UTF8.GetBytes(textkey);
aes.Key = key;
return aes;
}
private string DecryptValue(string input, string textkey)
{
var bytes = Convert.FromBase64String(input);
var decryptedString = new StringBuilder();
var aes = GetAes(textkey);
var decryptor = aes.CreateDecryptor();
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
while (!srDecrypt.EndOfStream)
{
decryptedString.Append(srDecrypt.ReadLine());
}
}
}
}
return decryptedString.ToString();
}
这是我到目前为止尝试过的。但是似乎有些问题。我总是得到
This is what I've tried so far. But there seems to be something wrong with it. I'm always getting
Exception = {System.Exception:数据错误(循环冗余校验)。 (来自HRESULT的异常:0x80070017)
在Windows.Security.Cryptography.Core.CryptographicEngine.Decrypt(CryptographicKey键,IBuffer数据,IBuffer iv)
在...
private string DecryptValue(string input, string textkey)
{
// Load the alghorithm providers
var symmetricKeyProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
// Create the symmetric key that is used to encrypt the string from random keystring
var cryptoKey = symmetricKeyProvider.CreateSymmetricKey(CryptographicBuffer.DecodeFromBase64String(textkey));
// Decode the input Base64 string
var buffer = CryptographicBuffer.DecodeFromBase64String(input);
// Declare new byte array
byte[] dectryptedBytes;
// Decrypt the IBuffer back to byte array
CryptographicBuffer.CopyToByteArray(CryptographicEngine.Decrypt(cryptoKey, buffer, null), out dectryptedBytes);
// Get string back from the byte array
var decryptedString = Encoding.UTF8.GetString(dectryptedBytes, 0, dectryptedBytes.Length);
// Return plain text
return decryptedString;
}
推荐答案
好,我终于明白了它。如果有人对此感兴趣,可以采用以下解决方案:
Ok, I finally I got it. If someone is interested, here's the solution:
private string DecryptValue(string input, string textkey)
{
// Declare the static initialization vector
var iv = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// Convert the properties to required buffers
var pwBuffer = CryptographicBuffer.ConvertStringToBinary(textkey, BinaryStringEncoding.Utf8);
var saltBuffer = CryptographicBuffer.CreateFromByteArray(iv);
var buffer = CryptographicBuffer.DecodeFromBase64String(input);
// Load the alghorithm providers
var symmetricKeyProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC");
// Create the symmetric key that is used to encrypt the string from IV
var cryptoKey = symmetricKeyProvider.CreateSymmetricKey(pwBuffer);
// Decrypt the IBuffer back to byte array
var resultBuffer = CryptographicEngine.Decrypt(cryptoKey, buffer, saltBuffer);
// Get string back from the byte array
var decryptedString = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, resultBuffer);
// Return plain text
return decryptedString;
}
这篇关于将AesManaged转换为新的通用应用程序CryptographicEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!