简体   繁体   English

如何在C#Windows Phone应用程序中使用密钥创建Aes 256位加密?

[英]How to create Aes 256bit Encryption with key in C# Windows Phone application?

I'm trying to create Aes 256bit Encryption with key in login screen. 我正在尝试使用登录屏幕中的密钥创建Aes 256位加密。 I need a large encrypted string as i'm using 256bit But it result in small encrypted string.I have checked many samples But all are for Windows desktop application not for windows Phone application. 我需要一个较大的加密字符串,因为我使用的是256位,但是它导致加密的字符串很小。 Please help regarding this. 请对此提供帮助。

This is my code 这是我的代码

namespace SampleEncription
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            byte[] encryptedPassword;

            // Create a new instance of the RijndaelManaged
            // class.  This generates a new key and initialization
            // vector (IV).
            using (var algorithm = new AesManaged())
            {
                algorithm.KeySize = 256;
                algorithm.BlockSize = 128;

                // Encrypt the string to an array of bytes.
                encryptedPassword = Cryptology.EncryptStringToBytes("Password", algorithm.Key, algorithm.IV);

                //string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString());

                string chars = System.Convert.ToBase64String(encryptedPassword);

                Debug.WriteLine(chars);
            }
        }

    }
}

one another class named cryptology: 另一类称为密码学:

namespace SampleEncription
{
    class Cryptology
    {
        private const string Salt = "603deb1015ca71be2b73aef0857d7781";
        private const int SizeOfBuffer = 1024 * 8;

        internal static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }
            if (key == null || key.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            if (iv == null || iv.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }

            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (var rijAlg = new AesManaged())
            {
                rijAlg.Key = key;
                rijAlg.IV = iv;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream.
            return encrypted;

        }
    }
}

instead of 代替

string chars = System.Convert.ToBase64String(encryptedPassword);

do this 做这个

Encoding.UTF8.GetString(encryptedPassword, 0, encryptedPassword.Length);

I think wp8 doesn't allow you to use System.Text.Encoding.Default.GetString, you can try to default it to UTF8, which i assume that the cipher text are all in latin characters.. 我认为wp8不允许您使用System.Text.Encoding.Default.GetString,您可以尝试将其默认设置为UTF8,我假设密文全部使用拉丁字符。

You forgot to flush :) 你忘了冲洗:)

You are calling encrypted = msEncrypt.ToArray(); 您正在调用encrypted = msEncrypt.ToArray(); before closing and therefore flushing the CryptoStream . 在关闭并因此刷新CryptoStream As the final block needs to be padded, not all bytes will have been written. 由于需要填充最后一个块,因此并非所有字节都会被写入。 If you use a block cipher mode of encryption or an authenticated cipher, it is always required to flush. 如果使用加密的块密码模式或经过身份验证的密码,则始终需要刷新。 Only stream cipher modes of encryption may not require you to flush the stream as each bit can be encryption separately. 仅加密流密码模式可能不需要您刷新流,因为每个位都可以单独加密。

In your implementation, you should be able to just move msEncrypt.ToArray() below the using scope of the CryptoStream , if I'm not mistaken. 在实现中,如果我没有记错的话,您应该可以将msEncrypt.ToArray()移到CryptoStreamusing范围之下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM