繁体   English   中英

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

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

我正在尝试使用登录屏幕中的密钥创建Aes 256位加密。 我需要一个较大的加密字符串,因为我使用的是256位,但是它导致加密的字符串很小。 请对此提供帮助。

这是我的代码

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

    }
}

另一类称为密码学:

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;

        }
    }
}

代替

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

做这个

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

我认为wp8不允许您使用System.Text.Encoding.Default.GetString,您可以尝试将其默认设置为UTF8,我假设密文全部使用拉丁字符。

你忘了冲洗:)

您正在调用encrypted = msEncrypt.ToArray(); 在关闭并因此刷新CryptoStream 由于需要填充最后一个块,因此并非所有字节都会被写入。 如果使用加密的块密码模式或经过身份验证的密码,则始终需要刷新。 仅加密流密码模式可能不需要您刷新流,因为每个位都可以单独加密。

在实现中,如果我没有记错的话,您应该可以将msEncrypt.ToArray()移到CryptoStreamusing范围之下。

暂无
暂无

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

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