C # string DES encryption/decryption and MD5 encryption)

Source: Internet
Author: User
Using system;
Using system. IO;
Using system. Windows. forms;
Using system. Collections. Generic;
Using system. text;
Using system. Security. cryptography;

Namespace mydes
{
/// <Summary>
/// DES encryption/decryption and MD5 Encryption
/// </Summary>
Public class my_des
{
# Region private attributes

/// <Summary>
/// MD5 encrypted string
/// </Summary>
Private string md5str = NULL;
/// <Summary>
/// DES encrypted string
/// </Summary>
Private string encryptstr = NULL;

/// <Summary>
/// Des decrypted string
/// </Summary>
Private string decryptstr = NULL;

/// <Summary>
/// Des key
/// </Summary>
Private string mypolicey = NULL;

/// <Summary>
/// Returned string
/// </Summary>
Private string mydesstr = NULL;

/// <Summary>
/// Error message
/// </Summary>
Private string message = NULL;

# Endregion

# Region Public attributes

/// <Summary>
/// MD5 encrypted string
/// </Summary>
Public String md5str
{
Get {return md5str ;}
Set {md5str = value ;}
}
/// <Summary>
/// DES encrypted string
/// </Summary>
Public String encryptstr
{
Get {return encryptstr ;}
Set {encryptstr = value ;}
}

///


// The string decrypted by des
///
Public String decryptstr
{< BR> get {return decryptstr ;}
set {decryptstr = value ;}< BR >}

/// <Summary>
/// Des key
/// </Summary>
Public String mypolicey
{
Get {return mypolicey ;}
Set {mypolicey = value ;}
}

/// <Summary>
/// Returned string
/// </Summary>
Public String mydesstr
{
Get {return mydesstr ;}
Set {mydesstr = value ;}
}
/// <Summary>
/// Error message
/// </Summary>
Public String message
{
Get {return message ;}
Set {message = value ;}
}
# Endregion

# Region Public method
///


// execute DES encryption
///
Public void desencrypt ()
{< br> try
{< br> byte [] mystr_e = encoding. utf8.getbytes (this. encryptstr);
byte [] mykey_e = encoding. utf8.getbytes (this. mypolicey);

Descryptoserviceprovider mydes_e = new descryptoserviceprovider ();
Mydes_e.key = mykey_e;
Mydes_e.iv = mykey_e;

Memorystream mymem_e = new memorystream ();

cryptostream mycry_e = new cryptostream (mymem_e, mydes_e.createencryptor (), cryptostreammode. write);
mycry_e.write (mystr_e, 0, mystr_e.length);
mycry_e.flushfinalblock ();
mycry_e.close ();

This. mydesstr = convert. tobase64string (mymem_e.toarray ();
}< br> catch (Exception error)
{< br> This. message = "DES encryption error:" + error. message;
}< BR >}

///


// execute des decryption
///
Public void desdecrypt ()
{< br> try
{< br> byte [] mystr_d = convert. frombase64string (this. decryptstr);
byte [] mykey_d = encoding. utf8.getbytes (this. mypolicey);

Descryptoserviceprovider mydes_d = new descryptoserviceprovider ();
Mydes_d.key = mykey_d;
Mydes_d.iv = mykey_d;

Memorystream mymem_d = new memorystream ();

cryptostream mycry_d = new cryptostream (mymem_d, mydes_d.createdecryptor (), cryptostreammode. write);
mycry_d.write (mystr_d, 0, mystr_d.length);
mycry_d.flushfinalblock ();
mycry_d.close ();

This. mydesstr = encoding. utf8.getstring (mymem_d.toarray ());
}
Catch (Exception error)
{
This. Message = "des decryption error:" + error. message;
}
}

/// <Summary>
/// Perform MD5 Encryption
/// </Summary>
Public void md5jiami ()
{
Md5cryptoserviceprovider mymd5 = new md5cryptoserviceprovider ();
Try
{
Byte [] mymd5_str = mymd5.computehash (encoding. utf8.getbytes (this. md5str ));
This. mydesstr = encoding. utf8.getstring (mymd5_str );
}
Catch (Exception error)
{
This. Message = "MD5 encryption error:" + error. message;
}

}

# Endregion
}
}

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.