簡體   English   中英

使用AesManaged和CryptoStream將加密算法alghoritm移植到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;
}
}

來源: 如何在Metro中執行簡單的AES加密/解密?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM