我将这段旧代码剪掉,应翻译成使用新的CryptographicEngine。但是我对新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();
}
这是我到目前为止所尝试过的。但它似乎有些问题。我总是得到
Exception = {System.Exception: Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)
at Windows.Security.Cryptography.Core.CryptographicEngine.Decrypt(CryptographicKey key, IBuffer data, IBuffer iv)
at ...
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;
}
答案 0 :(得分:1)
好的,我终于得到了它。如果有人感兴趣,这就是解决方案:
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;
}