Simple C #. Net string encryption and decryption program

Source: Internet
Author: User

Yesterday, the South Korean headquarters conducted a security review on the website, and we saw that many link strings were not encrypted. I criticized them.

To encrypt and decrypt the previously used stringProgramI used it out and shared it by the way. I hope to help my friends who need it:

EncryptionAlgorithmWhen 3DES is used, the key has been hashed by MD5, And the strength should meet general requirements,CodeVery few, very convenient:

Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Security. cryptography;

Namespace Nop. Security
{
Public   Class Encrypt
{
Public   Static   String Decryptstring ( String Strtext, String Key)
{
Byte [] Buffer =   New Md5cryptoserviceprovider (). computehash (encoding. ASCII. getbytes (key ));
Tripledescryptoserviceprovider
=   New Tripledescryptoserviceprovider ();
Provider. Key
= Buffer;
Provider. Mode
= Ciphermode. ECB;
Byte [] Inputbuffer = Convert. frombase64string (strtext );
Return Encoding. ASCII. getstring (provider. createdecryptor (). transformfinalblock (inputbuffer, 0 , Inputbuffer. Length ));
}

Public   Static   String Decryptutf8string ( String Strtext, String Key)
{
Byte [] Buffer =   New Md5cryptoserviceprovider (). computehash (encoding. utf8.getbytes (key ));
Tripledescryptoserviceprovider
=   New Tripledescryptoserviceprovider ();
Provider. Key
= Buffer;
Provider. Mode
= Ciphermode. ECB;
Byte [] Inputbuffer = Convert. frombase64string (strtext );
Return Encoding. utf8.getstring (provider. createdecryptor (). transformfinalblock (inputbuffer, 0 , Inputbuffer. Length ));
}

Public   Static   String Encryptstring ( String Strtext, String Key)
{
Byte [] Buffer =   New Md5cryptoserviceprovider (). computehash (encoding. ASCII. getbytes (key ));
Tripledescryptoserviceprovider
=   New Tripledescryptoserviceprovider ();
Provider. Key
= Buffer;
Provider. Mode
= Ciphermode. ECB;
Byte [] Bytes = Encoding. ASCII. getbytes (strtext );
String Str = Convert. tobase64string (provider. createencryptor (). transformfinalblock (bytes, 0 , Bytes. Length ));
Provider
=   Null ;
Return STR;
}

Public   Static   String Encryptutf8string ( String Strtext, String Key)
{
Byte [] Buffer =   New Md5cryptoserviceprovider (). computehash (encoding. utf8.getbytes (key ));
Tripledescryptoserviceprovider
=   New Tripledescryptoserviceprovider ();
Provider. Key
= Buffer;
Provider. Mode
= Ciphermode. ECB;
Byte [] Bytes = Encoding. utf8.getbytes (strtext );
String Str = Convert. tobase64string (provider. createencryptor (). transformfinalblock (bytes, 0 , Bytes. Length ));
Provider
=   Null ;
Return STR;
}
}

} 

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.