A reversible des and tripledes Encryption Class

Source: Internet
Author: User

 

Using System;
Using System. Security;
Using System. Security. cryptography;
Using System. IO;
Using System. text;
Namespace Encryptdownmoon
{
  /**/ ///   <Summary>
  /// Description of encryptsqlconn.
  ///   </Summary>
  Public   Class Encryptsqlconn
{
Public Encryptsqlconn ()
{
If (MCSP = Null )
{
MCSP = Setenc ();
}
}
/**/ ///   <Summary>
/// Set the encryption mode. The value 0 indicates des, and the value 1 indicates tripledes.
///   </Summary>
///   <Returns> </returns>
Public Encryptsqlconn ( Int Enmethod)
{
If (Enmethod = 0 )
{
Lngenmethod = 0 ;
}
Else
{
Lngenmethod = 1 ;
}
If (MCSP = Null )
{
MCSP = Setenc ();
}

}
Method # Region Method

Private Symmetricalgorithm MCSP;
Private   Int M_lngenmethod = 0 ;
/**/ ///   <Summary>
/// Encryption method. The value 0 indicates des, and the value 1 indicates tripledes.
///   </Summary>
Public   Int Lngenmethod
{
Get
{
Return M_lngenmethod;
}
Set
{
M_lngenmethod = Value;
}
}
Private Symmetricalgorithm setenc ()
{
If (Lngenmethod = 0 )
{
Return   New Descryptoserviceprovider ();
}
Else
Return   New Tripledescryptoserviceprovider ();
}
// Test key value
Private String Genkeyvalue
{
Get
{
MCSP. generatekey ();
Return Convert. tobase64string (MCSP. Key );
}
}
// Test IV Value
Private   String Genivvalue
{
Get
{
MCSP. generateiv ();
Return Convert. tobase64string (MCSP. IV );
}
}
/**/ ///   <Summary>
/// Encrypted string
///   </Summary>
///   <Param name = "value"> </param>
///   <Returns> </returns>
Public   String Encryptstring ( String Value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
CT = MCSP. createencryptor (MCSP. Key, MCSP. IV );
// Ct = MCSP. createencryptor (genkeyvalue, genivvalue );
Byt = Encoding. utf8.getbytes (value );
MS =   New Memorystream ();
CS =   New Cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0 , Byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return Convert. tobase64string (Ms. toarray ());
}

/**/ ///   <Summary>
/// Decryption string
///   </Summary>
///   <Param name = "value"> </param>
///   <Returns> </returns>
Public   String Decryptstring ( String Value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
CT = MCSP. createdecryptor (MCSP. Key, MCSP. IV );
// Ct = MCSP. createdecryptor (genkeyvalue, genivvalue );
Byt = Convert. frombase64string (value );
MS =   New Memorystream ();
CS =   New Cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0 , Byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return Encoding. utf8.getstring (Ms. toarray ());
}
# Endregion
 
}
}

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.