C # Use des to encrypt and decrypt strings

Source: Internet
Author: User

/// <Summary>
/// Use des to encrypt the string
/// </Summary>
/// <Param name = "stringtoencrypt"> string to be encrypted </param>
/// <Param name = "strkey"> 8-character encryption key </param>
/// <Returns> </returns>
Public static string encryptstringbydes (string stringtoencrypt, string key)
{
If (string. isnullorempty (key) | encoding. utf8.getbytes (key). length! = 8)
Key = "$ ap1bi $ .";
Byte [] Key = {};
Byte [] IV = {0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
Try
{
Key = encoding. utf8.getbytes (key );
Using (descryptoserviceprovider odescrypto = new descryptoserviceprovider ())
{
Byte [] inputbytearray = encoding. utf8.getbytes (stringtoencrypt );
Memorystream omemorystream = new memorystream ();
Cryptostream ocryptostream = new cryptostream (omemorystream,
Odescrypto. createencryptor (Key, IV), cryptostreammode. Write );
Ocryptostream. Write (inputbytearray, 0, inputbytearray. Length );
Ocryptostream. flushfinalblock ();
Return convert. tobase64string (omemorystream. toarray ());
}
}
Catch (exception ex)
{
 
Throw ex;
}
}
 
/// <Summary>
/// Use des to decrypt the string
/// </Summary>
/// <Param name = "stringtodecrypt"> string to be decrypted </param>
/// <Param name = "strkey"> 8-bit decryption key </param>
/// <Returns> </returns>
Public static string decryptstringbydes (string stringtodecrypt, string key)
{
If (string. isnullorempty (key) | encoding. utf8.getbytes (key). length! = 8)
Key = "$ apa1i $ .";
Byte [] Key = {};
Byte [] IV = {0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
Stringtodecrypt = stringtodecrypt. Replace ("", "+ ");
Byte [] inputbytearray = new byte [stringtodecrypt. Length];
 
 
Try
{
Key = encoding. utf8.getbytes (key );
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (stringtodecrypt );
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 = encoding. utf8;
Return encoding. getstring (Ms. toarray ());
}
Catch (exception ex)
{
 
Throw ex;
}
}

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.