加密url發生 Invalid length for a Base-64 char array異常

來源:互聯網
上載者:User
類是轉自:http://forums.asp.net/t/1222829.aspx,但是有些問題,現在把解決方案共用下,很簡單的

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography; 


/**//// <summary>
/// Summary description for Encryption64
/// </summary>
public class Encryption64
{
    const string DESKey = "AQWSEDRF";
    const string DESIV = "HGFEDCBA";

    public static string DESDecrypt(string stringToDecrypt)//Decrypt the content
    {

        byte[] key;
        byte[] IV;

        byte[] inputByteArray;
        try
        {

            key = Convert2ByteArray(DESKey);

            IV = Convert2ByteArray(DESIV);

            int len = stringToDecrypt.Length;

          //  stringToDecrypt =stringToDecrypt.Replace(" ","+");
            inputByteArray = Convert.FromBase64String(stringToDecrypt);


            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            MemoryStream ms = new MemoryStream();

            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);

            cs.FlushFinalBlock();

            Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray());
        }

        catch (System.Exception ex)
        {

            throw ex;
        }
    }

    public static string DESEncrypt(string stringToEncrypt)// Encrypt the content
    {

        byte[] key;
        byte[] IV;

        byte[] inputByteArray;
        try
        {

            key = Convert2ByteArray(DESKey);

            IV = Convert2ByteArray(DESIV);

            inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);

            cs.FlushFinalBlock();

            string value = Convert.ToBase64String(ms.ToArray());
          //  value = value.Replace("+"," ");

            return value;
        }

        catch (System.Exception ex)
        {

            throw ex;
        }

    }

    static byte[] Convert2ByteArray(string strInput)
    {

        int intCounter; char[] arrChar;
        arrChar = strInput.ToCharArray();

        byte[] arrByte = new byte[arrChar.Length];

        for (intCounter = 0; intCounter <= arrByte.Length - 1; intCounter++)
            arrByte[intCounter] = Convert.ToByte(arrChar[intCounter]);

        return arrByte;
    }



}

以上代碼大部分時間運行是正常的,但是加密得出的字串如果包含"+",用Request.QueryString接收,"+"字元會漏掉,
解密的時候就會報Invalid length for a Base-64 char array異常,所以加密後要替換下

            string value = Convert.ToBase64String(ms.ToArray());
            value = value.Replace("+"," ");

解密時就要替換回來            stringToDecrypt =stringToDecrypt.Replace(" ","+");
            inputByteArray = Convert.FromBase64String(stringToDecrypt);

希望對您有協助,弄了好久才發現的。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.