使用AesManaged和CryptoStream将加密alghoritm移植到Windows Phone 8.1

时间:2015-06-05 14:28:29

标签: c# windows-phone-8

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

1 个答案:

答案 0 :(得分: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;
}
}

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