填充无效,解密字符串

时间:2016-07-12 23:33:39

标签: c# .net encryption

我有一个网页,AES加密有效负载,以及一个公钥,用于发送站点的查询字符串(这是客户端的请求,而不是我的选择),其中有效负载将被解密并对其进行操作。

这是网页:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>

</body>

</html>

<script src="Scripts/CryptoJS/core.js"></script>
<script src="Scripts/CryptoJS/cipher-core.js"></script>
<script src="Scripts/CryptoJS/aes.js"></script>
<script src="Scripts/CryptoJS/enc-utf16.js"></script>
<script src="Scripts/CryptoJS/enc-base64.js"></script>

<script>
    var payload = "you can do this Rick!";
    var keyvalue = '1234567890ABCDEF';

    var key = CryptoJS.enc.Utf8.parse(keyvalue);
    var iv = CryptoJS.enc.Utf8.parse(keyvalue);


    var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(payload), key,
       {
           keySize: 128 ,
           iv: iv,
           mode: CryptoJS.mode.CBC,
           padding: CryptoJS.pad.Pkcs7
       });

    window.location = "Home/To?encrptedPayload=" + encrypted + "&ivPublicKeyNonEncrypted=" + keyvalue;

</script>

这是接收呼叫的控制器:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Workbench2.Controllers
{
    public class HomeController : Controller
    {
        byte[] Key = Encoding.UTF8.GetBytes("1234567890ABCDEF");

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult To(string encrptedPayload, string ivPublicKeyNonEncrypted)
        {
            string result = "";

            var bIv = Encoding.UTF8.GetBytes(ivPublicKeyNonEncrypted);

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.KeySize = 128;
                rijAlg.Key = Key;
                rijAlg.IV = bIv;
                rijAlg.Padding=PaddingMode.PKCS7;
                rijAlg.Mode=CipherMode.CBC;

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

                // Create the streams used for decryption.
                var bPayload = Encoding.UTF8.GetBytes(encrptedPayload);
                using (MemoryStream msDecrypt = new MemoryStream(bPayload))
                {
                    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.
                            result = srDecrypt.ReadToEnd(); // THIS LINE THROWS ERROR                            }
                    }
                }

            }

            return View(result);
        }               
    }
}

我收到消息“填充无效且无法删除”。当以下行执行时:

result = srDecrypt.ReadToEnd();

1 个答案:

答案 0 :(得分:1)

加密数据是二进制,除非您以某种方式对其进行编码,否则无法以字符串形式传递它 - see e.g. this。如果需要以字符串形式移动它,请使用base64之类的编码。