Two symmetric encryption classes-Asp.net, C #

Source: Internet
Author: User

The following two types of encryption have been copied from my blog. If you see the same, please include them;

I. Des encryptionAlgorithm:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. configuration;
Using system. IO;
Using system. Security;
Using system. Security. cryptography;
Namespace spacename
{
Public static class encryptionextensions
{
# Region encryption string extension method
/// <Summary>
/// Encrypted string
/// </Summary>
/// <Param name = "svalue"> string </param>
/// <Returns> </returns>
Public static string encodestring (this string svalue)
{
String strencoded = string. empty;
// String strencoded = "";
// Use the memory stream and DES algorithm for encryption
Memorystream MS = new memorystream ();
Des Cr = new descryptoserviceprovider ();
// When using the DES algorithm for encryption, ensure that the defined string is encoded by default (To Make It writable as long as possible) and cannot exceed 8 bytes (a Chinese character occupies 2 bytes)
Byte [] Key = system. Text. encoding. Default. getbytes ("19812155 ");
Byte [] IV = system. Text. encoding. Default. getbytes ("18712515 ");
// Create an encrypted stream and write the text to be encrypted using the default encoding to minimize the encryption result by 34
// Encode the encrypted data with base64 and store the data in the string fields of the database. The reason is that the encrypted data is non-character binary data and data will be lost if it is forcibly converted to any regular encoded string. 35 // The final result length is math. ceiling (default encoding length of the text to be encrypted/8) * 8*1.5 for example, after 8 bytes of raw data are encrypted, 8 bytes are converted to base64 and then 12 bytes
Cryptostream ECs = new cryptostream (MS, Cr. createencryptor (Key, IV), cryptostreammode. Write );
Streamwriter Sw = new streamwriter (ECS, system. Text. encoding. Default );
Sw. Write (svalue );
Sw. Flush ();
ECS. flushfinalblock ();
Strencoded = convert. tobase64string (Ms. getbuffer (), 0, (INT) ms. Length );
Sw. Close ();
Return strencoded;
}
# Endregion
# Region string decryption Extension Method
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "svalue"> string </param>
/// <Returns> </returns>
Public static string decodestring (this string svalue)
{
String strdecoded = string. empty;
// String strdecoded = "";
Byte [] Key = system. Text. encoding. Default. getbytes ("19812155 ");
Byte [] IV = system. Text. encoding. Default. getbytes ("18712515 ");
// Restores the data to be decrypted from base64 encoding.
Des Cr = new descryptoserviceprovider ();
Memorystream MS = new memorystream (convert. frombase64string (svalue ));
// Create a decrypted data stream and read the original text in the same write stream encoding format as the encrypted data stream
Cryptostream DCS = new cryptostream (MS, Cr. createdecryptor (Key, IV), cryptostreammode. Read );
Streamreader sr = new streamreader (DCs, system. Text. encoding. Default );
Strdecoded = Sr. readtoend ();
Sr. Close ();
Return strdecoded;
}

# Endregion
}
}

 

 

 

II:

Using system;
Using system. IO;
Using system. Security. cryptography;

Using system. text;

Namespace datacrypto
{
/// <Summary>
/// Symmetric encryption algorithm
/// </Summary>
Public class extends ricmethod
{

Private encryption ricalgorithm mobjcryptoservice;
Private string key;
/// <Summary>
/// Constructor of symmetric encryption
/// </Summary>
Public parameter ricmethod ()
{
Mobjcryptoservice = new rijndaelmanaged ();
Key = "Guz (% & hj7x89h $ yubi0456ftmat5 & fvhufcy76 * H % (hilj $ lhj! Y6 & (* jkp87jh7 ";
}
/// <Summary>
/// Obtain the key
/// </Summary>
/// <Returns> key </returns>
Private byte [] getlegalkey ()
{
String stemp = key;
Mobjcryptoservice. generatekey ();
Byte [] byttemp = mobjcryptoservice. Key;
Int keylength = byttemp. length;
If (stemp. length> keylength)
Stemp = stemp. substring (0, keylength );
Else if (stemp. Length <keylength)
Stemp = stemp. padright (keylength ,'');
Return asciiencoding. ASCII. getbytes (stemp );
}
/// <Summary>
/// Obtain the initial vector IV
/// </Summary>
/// <Returns> initial test vector IV </returns>
Private byte [] getlegaliv ()
{
String stemp = "e4ghj * ghg7! Rnifb & 95guy86gfghub # er57hbh (U % g6hj ($ jhwk7 &! Hg4ui % $ hjk ";
Mobjcryptoservice. generateiv ();
Byte [] byttemp = mobjcryptoservice. IV;
Int ivlength = byttemp. length;
If (stemp. length> ivlength)
Stemp = stemp. substring (0, ivlength );
Else if (stemp. Length <ivlength)
Stemp = stemp. padright (ivlength ,'');
Return asciiencoding. ASCII. getbytes (stemp );
}
/// <Summary>
/// Encryption Method
/// </Summary>
/// <Param name = "Source"> string to be encrypted </param>
/// <Returns> encrypted string </returns>
Public String encrypto (string source)
{
Byte [] bytin = utf8encoding. utf8.getbytes (source );
Memorystream MS = new memorystream ();
Mobjcryptoservice. Key = getlegalkey ();
Mobjcryptoservice. IV = getlegaliv ();
Icryptotransform encrypto = mobjcryptoservice. createencryptor ();
Cryptostream cs = new cryptostream (MS, encrypto, cryptostreammode. Write );
CS. Write (bytin, 0, bytin. Length );
CS. flushfinalblock ();
Ms. Close ();
Byte [] bytout = Ms. toarray ();
Return convert. tobase64string (bytout );
}
/// <Summary>
/// Decryption Method
/// </Summary>
/// <Param name = "Source"> string to be decrypted </param>
/// <Returns> decrypted string </returns>
Public String decrypto (string source)
{
Byte [] bytin = convert. frombase64string (source );
Memorystream MS = new memorystream (bytin, 0, bytin. Length );
Mobjcryptoservice. Key = getlegalkey ();
Mobjcryptoservice. IV = getlegaliv ();
Icryptotransform encrypto = mobjcryptoservice. createdecryptor ();
Cryptostream cs = new cryptostream (MS, encrypto, cryptostreammode. Read );
Streamreader sr = new streamreader (CS );
Return Sr. readtoend ();
}
}
}

 

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.