Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C Windows Phone应用程序中使用密钥创建Aes 256bit加密?_C#_Windows Phone 8_Encryption_Aes - Fatal编程技术网

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

C# 如何在C Windows Phone应用程序中使用密钥创建Aes 256bit加密?,c#,windows-phone-8,encryption,aes,C#,Windows Phone 8,Encryption,Aes,我正在尝试在登录屏幕中使用密钥创建Aes 256bit加密。我需要一个大的加密字符串,因为我正在使用256bit,但它会导致小的加密字符串。我检查了许多示例,但都是针对Windows桌面应用程序的,而不是针对Windows Phone应用程序的。请帮忙 这是我的密码 namespace SampleEncription { public partial class MainPage : PhoneApplicationPage { public MainPage(

我正在尝试在登录屏幕中使用密钥创建Aes 256bit加密。我需要一个大的加密字符串,因为我正在使用256bit,但它会导致小的加密字符串。我检查了许多示例,但都是针对Windows桌面应用程序的,而不是针对Windows Phone应用程序的。请帮忙

这是我的密码

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

    }
}
另一个名为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;

        }
    }
}
而不是

string chars = System.Convert.ToBase64String(encryptedPassword);
这样做

Encoding.UTF8.GetString(encryptedPassword, 0, encryptedPassword.Length);
我认为wp8不允许您使用System.Text.Encoding.Default.GetString,您可以尝试将其默认为UTF8,我假设密码文本都是拉丁字符。

您忘记刷新:

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


在您的实现中,您应该能够将msEncrypt.ToArray移动到加密流的使用范围之下,如果我没有弄错的话。

告诉我们您到目前为止做了什么?我已经更新了代码。请看一看它给出了默认错误:-System.Text.Encoding不包含Defaulttry Encoding.UTF8.GetStringencryptedPassword的定义,0,encryptedPassword.Length;你的字节长度是多少?