C # encryption and decryption

Source: Internet
Author: User

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

Namespace cryptoutil
{
/// <Summary>
/// Summary of cryptoutil.
/// </Summary>
Public class cryptoutil
{

// Randomly select 8 bytes as both the key and initial vector
Byte [] bykey64 = {42, 16, 93,156, 78, 4,218, 32 };
Byte [] iv64 = {55,103,246, 79, 36, 99,167, 3 };
Byte [] bykey192 = {42, 16, 93,156, 78, 4,218, 167, 26,250,155,112, 44, 80, 11,204,119, 35,184,197 };
Byte [] iv192 = {55,103,246, 79, 36, 99,167, 83,184, 7,209, 5, 62, 13,145, 23,200, 58,173, 10,121,222 };

Public cryptoutil ()
{
//
// Todo: add the constructor logic here
//
}

/// <Summary>
/// Standard DES encryption
/// </Summary>
/// <Param name = "str_ SQL"> standard DES encryption </param>
Private string encrypt (string strtext)
{
// Byte [] bykey = {};
// Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
// Bykey = system. Text. encoding. utf8.getbytes (strencrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. utf8.getbytes (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (bykey64, iv64), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Return convert. tobase64string (Ms. toarray ());
}
Catch (exception ex)
{
Return ex. message;
}
}
/// <Summary>
/// Standard des decryption
/// </Summary>
/// <Param name = "str_ SQL"> standard des decryption </param>
Private string decrypt (string strtext)
{
// Byte [] bykey = {};
// Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Byte [] inputbytearray = new byte [strtext. Length];
Try
{
// Bykey = system. Text. encoding. utf8.getbytes (sdecrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (bykey64, iv64), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
System. Text. Encoding encoding = system. Text. encoding. utf8;
Return encoding. getstring (Ms. toarray ());
}
Catch (exception ex)
{
Return ex. message;
}
}

}
}

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.