Windows Phone8 c# 中的加密解密

本文关键字:加密解密 Phone8 Windows | 更新日期: 2024-11-07 07:30:54

我正在尝试使用AES算法加密我的数据。我从MSDN站点加密解密中获得了功能。我正在做的是使用以下方法
加密数据并将其存储为字符串 byte[] encrypted = EncryptStringToBytes_Aes(response, myAes.Key, myAes.IV); string saveresponse = Convert.ToBase64String(encrypted);

然后我将其保存在隔离存储设置中

settings.Add(merchantId, saveresponse);

但是我面临的问题是,当用户在某个时候到达我的页面时,我首先在 IsolatedStorageSettings 对象中检查是否存在数据,我将该数据传递给解密并进一步处理。我用来解密的步骤如下

byte[] temp = Convert.FromBase64String(response);

response = DecryptStringFromBytes_Aes(temp, myAes.Key, myAes.IV);

但是上面的行给了我错误"值不能为空。参数名称:输入缓冲区"

找不到我出错的地方。你们能否让我知道应该采取哪些步骤来启动和运行它。

这是加密代码

 static byte[] EncryptStringToBytes_Aes(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 AesManaged object 
        // with the specified key and IV. 
        using (AesManaged aesAlg = new AesManaged())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;
            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

  return encrypted;
}

这是解密代码

    static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        try
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");
            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;
            // Create an AesManaged object 
            // with the specified key and IV. 
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return plaintext;
        }
        catch (Exception ex) {
            return "Error";
        }
    }

在按钮 1 上单击我调用加密方法

using (AesManaged myAes = new AesManaged())
                                    {
byte[] encrypted = EncryptStringToBytes_Aes(response, myAes.Key, myAes.IV);
string saveresponse = Convert.ToBase64String(encrypted);
}

在按钮 2 上我调用解密方法

using (AesManaged myAes = new AesManaged())
                                        {
    byte[] temp= Convert.FromBase64String(response)
    response = DecryptStringFromBytes_Aes(temp, myAes.Key, myAes.IV);
    }

Windows Phone8 c# 中的加密解密

问题是using (AesManaged myAes = new AesManaged()){}阻止它做什么,它会生成新的密钥和用于加密和解密的 IV。因此,在解密时,密钥和IV不匹配,因此会产生错误。只需删除 using 块并在全局级别声明 Aesmanaged myAes 对象,问题就解决了。所以最终代码看起来像

AesManaged myAes = new AesManaged();

在按钮上一键加密;

byte[] 加密 = EncryptStringToBytes_Aes(response, myAes.Key, myAes.IV);字符串保存响应 = Convert.ToBase64String(加密);

和按钮2单击以解密

byte[] temp = Convert.FromBase64String(response);

response = DecryptStringFromBytes_Aes(temp, myAes.Key, myAes.IV);

就是这样,快乐的编码。