Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

c# - UWP: AES encryption and decryption

I had a simple class to do some basic local encryption for Windows Phone 8. I wanted to use the class again in a new UWP Windows 10 app for the Windows Store. Unfortunately I cannot use the AesManaged class anymore.

I tried to use Windows.Security.Cryptography.Core, but I'm completely stuck. This is the original class I used for Windows Phone 8. I must have found it somewhere on the internet back then.

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;
        }
    }
}

Does someone knows how to translate this for UWP Windows 10 apps?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to read the Documentation about SymmetricAlgorithmProvider and CryptographicEngine.

I have here a little example, how to use these together:

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();
        }
    }
}

When you want to use another Algorithm then AesCbcPkcs7, then you have to change the SymmetricAlgorithmName


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...