UWP: crittografia e decrittografia AES

Ho avuto una semplice lezione per fare un po ‘di crittografia locale di base per Windows Phone 8. Volevo usare di nuovo la class in una nuova app UWP per Windows 10 per Windows Store. Purtroppo non posso più usare la class AesManaged.

Ho provato a utilizzare Windows.Security.Cryptography.Core , ma sono completamente bloccato. Questa è la class originale che ho usato per Windows Phone 8. Devo averlo trovato da qualche parte su Internet allora.

 using System.Security.Cryptography; namespace TestGame { public class AesEnDecryption { private string AES_Key = "MYLiSQ864FhDevJOeMs9EVp5RmfC7OuH"; private string AES_IV = "FoL5Tyd9sZclVn5A"; public string AES_encrypt(string Input) { var aes = new AesManaged(); aes.KeySize = 128; aes.BlockSize = 128; aes.Key = Convert.FromBase64String(AES_Key); aes.IV = Encoding.UTF8.GetBytes(AES_IV); var encrypt = aes.CreateEncryptor(aes.Key, aes.IV); byte[] xBuff = null; using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) { byte[] xXml = Encoding.UTF8.GetBytes(Input); cs.Write(xXml, 0, xXml.Length); } xBuff = ms.ToArray(); } string Output = Convert.ToBase64String(xBuff); return Output; } public string AES_decrypt(string Input) { var aes = new AesManaged(); aes.KeySize = 128; aes.BlockSize = 128; aes.Key = Convert.FromBase64String(AES_Key); aes.IV = Encoding.UTF8.GetBytes(AES_IV); var decrypt = aes.CreateDecryptor(); byte[] xBuff = null; using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) { byte[] xXml = Convert.FromBase64String(Input); cs.Write(xXml, 0, xXml.Length); } xBuff = ms.ToArray(); } string Output = Encoding.UTF8.GetString(xBuff, 0, xBuff.Length); return Output; } } } 

Qualcuno sa come tradurre questo per le app UWP di Windows 10?

Grazie!

È necessario leggere la documentazione su SymmetricAlgorithmProvider e CryptographicEngine.

Ho qui un piccolo esempio, come usarli insieme:

 using System; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using Windows.Security.Cryptography; using Windows.Security.Cryptography.Core; using Windows.Storage.Streams; namespace CryptTest { public class AesEnDecryption { // Key with 256 and IV with 16 length private string AES_Key = "Y+3xQDLPWalRKK3U/JuabsJNnuEO91zRiOH5gjgOqck="; private string AES_IV = "15CV1/ZOnVI3rY4wk4INBg=="; private IBuffer m_iv = null; private CryptographicKey m_key; public AesEnDecryption() { IBuffer key = Convert.FromBase64String(AES_Key).AsBuffer(); m_iv = Convert.FromBase64String(AES_IV).AsBuffer(); SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7); m_key = provider.CreateSymmetricKey(key); } public byte[] Encrypt(byte[] input) { IBuffer bufferMsg = CryptographicBuffer.ConvertStringToBinary(Encoding.ASCII.GetString(input), BinaryStringEncoding.Utf8); IBuffer bufferEncrypt = CryptographicEngine.Encrypt(m_key, bufferMsg, m_iv); return bufferEncrypt.ToArray(); } public byte[] Decrypt(byte[] input) { IBuffer bufferDecrypt = CryptographicEngine.Decrypt(m_key, input.AsBuffer(), m_iv); return bufferDecrypt.ToArray(); } } } 

Quando si desidera utilizzare un altro Algorithm quindi AesCbcPkcs7 , è necessario modificare SymmetricAlgorithmName

Codice Cleaner

  public static async Task EncryptAesFileAsync(StorageFile fileForEncryption, string aesKey256, string iv16lenght) { bool success = false; try { //Initialize key IBuffer key = Convert.FromBase64String(aesKey256).AsBuffer(); var m_iv = Convert.FromBase64String(iv16lenght).AsBuffer(); SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7); var m_key = provider.CreateSymmetricKey(key); //secured data IBuffer data = await FileIO.ReadBufferAsync(fileForEncryption); IBuffer SecuredData = CryptographicEngine.Encrypt(m_key, data, m_iv); await FileIO.WriteBufferAsync(fileForEncryption, SecuredData); success = true; } catch (Exception ex) { success = false; DialogHelper.DisplayMessageDebug(ex); } return success; } public static async Task DecryptAesFileAsync(StorageFile EncryptedFile, string aesKey256, string iv16lenght) { bool success = false; try { //Initialize key IBuffer key = Convert.FromBase64String(aesKey256).AsBuffer(); var m_iv = Convert.FromBase64String(iv16lenght).AsBuffer(); SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7); var m_key = provider.CreateSymmetricKey(key); //Unsecured Data IBuffer data = await FileIO.ReadBufferAsync(EncryptedFile); IBuffer UnSecuredData = CryptographicEngine.Decrypt(m_key, data, m_iv); await FileIO.WriteBufferAsync(EncryptedFile, UnSecuredData); success = true; } catch (Exception ex) { success = false; DialogHelper.DisplayMessageDebug(ex); } return success; }