An invalid length for a base-64 char array error occurred in the encrypted URL.

Source: Internet
Author: User
The class is transferred from: http://forums.asp.net/t/1222829.aspx. However, I have some questions. Now I want to share the answer, which is very simple.

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 Administrative ey =   " Aqwsedrf " ;
Const   String Desiv =   " Hgfedcba " ;

Public   Static   String Desdecrypt ( String Stringtodecrypt) // Decrypt the content
{

Byte [] Key;
Byte [] IV;

Byte [] Inputbytearray;
Try
{

Key = Convert2bytearray (distribuey );

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. utf8; Return Encoding. getstring (Ms. toarray ());
}

Catch (System. Exception ex)
{

ThrowEx;
}
}

Public   Static   String Desencrypt ( String Stringtoencrypt) // Encrypt the content
{

Byte [] Key;
Byte [] IV;

Byte [] Inputbytearray;
Try
{

Key = Convert2bytearray (distribuey );

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)
{

ThrowEx;
}

}

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



}

AboveCodeIt runs normally most of the time, but if the encrypted string contains "+", it is received using request. querystring, and the "+" character will be missed,
During decryption, an invalid length for a base-64 char array exception is reported.

String Value = Convert. tobase64string (Ms. toarray ());
Value = Value. Replace ( " + " , "   " );

Should be replaced when decryptionStringtodecrypt=Stringtodecrypt. Replace (" ","+");
Inputbytearray=Convert. frombase64string (stringtodecrypt );

it takes a long time to discover it, hoping it will be helpful to you...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.