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

porting encryption alghoritm using AesManaged and CryptoStream to windows phone 8.1

我正在将一个 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;
}

这些 类 没有对应的 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;
}
}

来源:How to do simple AES encryption/decryption in Metro?