简体   繁体   English

使用 AES 256 加密和解密查询字符串值

[英]Encrypt & Decrypt querystring values using AES 256

I am using the following code to Encrypt/Decrypt a querystring and pass it from one page to another.我正在使用以下代码加密/解密查询字符串并将其从一个页面传递到另一个页面。 The resulting output is missing a '+' (see at the bottom of the question).结果输出缺少“+”(参见问题底部)。 What can I do to make sure the '+' comes thru as I am already using urlencode/urldecode?当我已经在使用 urlencode/urldecode 时,我该怎么做才能确保“+”通过?

//Encryption page //加密页面

    protected void Page_Load(object sender, EventArgs e)
    {
        string text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";            
        Response.Write("256:" + Decrypt256(Encrypt256(text)));
        Response.Write(string.Format("<br/><a href=\"decrypt.aspx?p={0}\">{0}</a>", HttpUtility.UrlEncode(Encrypt256(text))));            
    }


    private const string AesIV256 = @"!QAZ2WSX#EDC4RFV";
    private const string AesKey256 = @"5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<";


    private string Encrypt256(string text)
    {            
        // AesCryptoServiceProvider
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();            
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.IV = Encoding.UTF8.GetBytes(AesIV256);
        aes.Key = Encoding.UTF8.GetBytes(AesKey256);
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        // Convert string to byte array
        byte[] src = Encoding.Unicode.GetBytes(text);

        // encryption
        using (ICryptoTransform encrypt = aes.CreateEncryptor())
        {
            byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

            // Convert byte array to Base64 strings
            return Convert.ToBase64String(dest);
        }
    }

    /// <summary>
    /// AES decryption
    /// </summary>
    private string Decrypt256(string text)
    {            
        // AesCryptoServiceProvider
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.IV = Encoding.UTF8.GetBytes(AesIV256);
        aes.Key = Encoding.UTF8.GetBytes(AesKey256);
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        // Convert Base64 strings to byte array
        byte[] src = System.Convert.FromBase64String(text);

        // decryption
        using (ICryptoTransform decrypt = aes.CreateDecryptor())
        {
            byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
            return Encoding.Unicode.GetString(dest);
        }
    }

Decryption page (I use this page to print out what I the decrypted string from the first page and then compare it what I get in the URL:解密页面(我使用此页面从第一页打印出我解密的字符串,然后比较我在 URL 中得到的内容:

     protected void Page_Load(object sender, EventArgs e)
    {
        string urlValue = HttpUtility.UrlDecode(Request.QueryString["p"].Trim());
     Decrypt256(Encoding.ASCII.GetString(s2));            

        Response.Write(urlValue + "<br /><br />");     
        Response.Write("AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko+9KGtRh3UcQJtzkfSw==");            

    }

The end result is the following two lines (the first line is the output from the URL).最终结果是以下两行(第一行是 URL 的输出)。 They almost match except the first URL (the encoded/decoded querystring result) is missing the '+' symbol.除了第一个 URL(编码/解码的查询字符串结果)缺少“+”符号外,它们几乎匹配。 Any idea how to avoid this?知道如何避免这种情况吗?

AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko 9KGtRh3UcQJtzkfSw== AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko 9KGtRh3UcQJtzkfSw==

AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko+9KGtRh3UcQJtzkfSw== AUwsHc8j/llULnuwVnspNwolBUAhl5GFqC6iOrUN5euZFrOgFVypqTGfhAaooLxa0Fko+9KGtRh3UcQJtzkfSw==

ok fixed it, simply removed the urldecode method.好的修复它,只需删除 urldecode 方法。 The decode appears to happen automatically.解码似乎是自动发生的。

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

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