Des encryption in Asp.net

Source: Internet
Author: User

 

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

Encrypt des # region encrypt des
Public String encrypt (string ptoencrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
// Put the string in the byte array
// The original utf8 encoding. I changed it to unicode encoding. No.
Byte [] inputbytearray = encoding. Default. getbytes (ptoencrypt );

// Create the key and offset of the encryption object
// Make sure that you enter the English text for the password
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );

CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ())
{
Ret. appendformat ("{0: X2}", B );
}
Ret. tostring ();
Return ret. tostring ();
}
# Endregion

Decryption Method # region Decryption Method
// Ptodecrypt is the string to be decrypted and skey is the key
Public String decrypt (string ptodecrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = new byte [ptodecrypt. Length/2];
For (INT x = 0; x <ptodecrypt. Length/2; X ++)
{
Int I = (convert. toint32 (ptodecrypt. substring (x * 2, 2), 16 ));
Inputbytearray [x] = (byte) I;
}
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
// Create a stringbuild object. createdecrypt uses a stream object. The decrypted text must be converted to a stream object.
Stringbuilder ret = new stringbuilder ();
Return System. Text. encoding. Default. getstring (Ms. toarray ());

}
# Endregion
Related Article

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.