将使用AesManaged和CryptoStream的加密算法移植到windows phone 8.1

本文关键字:windows phone 加密算法 AesManaged CryptoStream | 更新日期: 2023-09-27 17:53:32

我正在移植一个5年前的wp7应用程序到wp 8.1,下面的代码无法编译。在8.1运行时中似乎缺少AesManaged和CryptoStream。

是否有一些解决方法?

public static string Encrypt(string Source,string CryptoKey)
{
  AesManaged aes = null;
  MemoryStream memoryStream = null;
  CryptoStream cryptoStream = null;
  //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
  byte[] keyb = LoadKey(CryptoKey);
  aes = new AesManaged();
  aes.Key = keyb;
  aes.IV = keyb;
  //Create Memory and Crypto Streams 
  memoryStream = new MemoryStream();
  cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),  CryptoStreamMode.Write);
  //Encrypt Data 
  byte[] data = Encoding.UTF8.GetBytes(Source);
  cryptoStream.Write(data, 0, data.Length);
  cryptoStream.FlushFinalBlock();
  //Return Base 64 String 
  return Convert.ToBase64String(memoryStream.ToArray());
  return Source;
}

将使用AesManaged和CryptoStream的加密算法移植到windows phone 8.1

这些类没有对应的WinRT。加密代码需要使用新的库重写。

下面是AES

的代码片段
public string AES_Encrypt(string input, string pass)
{
SymmetricKeyAlgorithmProvider SAP = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
CryptographicKey AES;
HashAlgorithmProvider HAP = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
CryptographicHash Hash_AES = HAP.CreateHash();
string encrypted = "";
try
{
    byte[] hash = new byte[32];
    Hash_AES.Append(CryptographicBuffer.CreateFromByteArray(System.Text.Encoding.UTF8.GetBytes(pass)));
    byte[] temp;
    CryptographicBuffer.CopyToByteArray(Hash_AES.GetValueAndReset(), out temp);
    Array.Copy(temp, 0, hash, 0, 16);
    Array.Copy(temp, 0, hash, 15, 16);
    AES = SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(hash));   
    IBuffer Buffer = CryptographicBuffer.CreateFromByteArray(System.Text.Encoding.UTF8.GetBytes(input));
    encrypted = CryptographicBuffer.EncodeToBase64String(CryptographicEngine.Encrypt(AES, Buffer, null));
    return encrypted;
}
catch (Exception ex)
{
    return null;
}
}

public string AES_Decrypt(string input, string pass)
{
SymmetricKeyAlgorithmProvider SAP = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
CryptographicKey AES;
HashAlgorithmProvider HAP = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
CryptographicHash Hash_AES = HAP.CreateHash();
string decrypted = "";
try
{
    byte[] hash = new byte[32];
    Hash_AES.Append(CryptographicBuffer.CreateFromByteArray(System.Text.Encoding.UTF8.GetBytes(pass)));
    byte[] temp;
    CryptographicBuffer.CopyToByteArray(Hash_AES.GetValueAndReset(), out temp);
    Array.Copy(temp, 0, hash, 0, 16);
    Array.Copy(temp, 0, hash, 15, 16);
    AES = SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(hash));   
    IBuffer Buffer = CryptographicBuffer.DecodeFromBase64String(input);
    byte[] Decrypted;
    CryptographicBuffer.CopyToByteArray(CryptographicEngine.Decrypt(AES, Buffer, null), out Decrypted);
    decrypted = System.Text.Encoding.UTF8.GetString(Decrypted, 0, Decrypted.Length);
    return decrypted;
}
catch (Exception ex)
{
    return null;
}
}

来源:如何在地铁做简单的AES加密/解密?