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...