C # A encapsulated encryption and decryption class

Source: Internet
Author: User
Tags decrypt key string
Encapsulation | encryption | decryption using System;
Using System.Text;
Using System.Globalization;
Using System.Security.Cryptography;
Using System.Windows.Forms;

Namespace Jh.encrypts
{
public class Jhencrypt
{
<summary>
Construction method
</summary>
Public Jhencrypt ()
{
}
<summary>
Encrypt with default key string
</summary>
<param name= "original" > Clear text </param>
<returns> redaction </returns>
public static string Encrypt (string original)
{
Return Encrypt (Original, "Jasonheung");
}
<summary>
Decrypting with the default key
</summary>
<param name= "original" > Redaction </param>
<returns> Clear Text </returns>
public static string Decrypt (string original)
{
Return Decrypt (Original, "Jasonheung", System.Text.Encoding.Default);
}
<summary>
Decrypts with a given key
</summary>
<param name= "original" > Redaction </param>
<param name= "key" > Key </param>
<returns> Clear Text </returns>
public static string Decrypt (string original, String key)
{
Return Decrypt (Original,key,system.text.encoding.default);
}
<summary>
Decrypts using the default key, returning the specified encoding plaintext
</summary>
<param name= "original" > Redaction </param>
<param name= "encoding" > Coding mode </param>
<returns> Clear Text </returns>
public static string Decrypt (String original,encoding Encoding)
{
Return Decrypt (Original, "Jasonheung", encoding);
}
<summary>
Encrypt with a given key
</summary>
<param name= "original" > Original text </param>
<param name= "key" > Key </param>
<param name= "Encoding" > character encoding scheme </param>
<returns> redaction </returns>
public static string Encrypt (string original, String key)
{
byte[] Buff = System.Text.Encoding.Default.GetBytes (original);
Byte[] KB = System.Text.Encoding.Default.GetBytes (key);
Return convert.tobase64string (Encrypt (BUFF,KB));
}

<summary>
Decrypts with a given key
</summary>
<param name= "Encrypted" > Redaction </param>
<param name= "key" > Key </param>
<param name= "Encoding" > character encoding scheme </param>
<returns> Clear Text </returns>
public static string Decrypt (String encrypted, string key,encoding Encoding)
{
byte[] Buff = convert.frombase64string (encrypted);
Byte[] KB = System.Text.Encoding.Default.GetBytes (key);
return encoding. GetString (Decrypt (BUFF,KB));
}
<summary>
Generate MD5 Summary
</summary>
<param name= "original" > Data source </param>
<returns> Summary </returns>
public static byte[] MakeMD5 (byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider ();
byte[] Keyhash = Hashmd5.computehash (original);
HASHMD5 = null;
return keyhash;
}

<summary>
Encrypt with a given key
</summary>
<param name= "original" > Clear text </param>
<param name= "key" > Key </param>
<returns> redaction </returns>
public static byte[] Encrypt (byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider ();
Des. Key = MakeMD5 (key);
Des. Mode = CIPHERMODE.ECB;

Return DES. CreateEncryptor (). TransformFinalBlock (original, 0, original. Length);
}

<summary>
Decrypting data with a given key
</summary>
<param name= "Encrypted" > Redaction </param>
<param name= "key" > Key </param>
<returns> Clear Text </returns>
public static byte[] Decrypt (byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider ();
Des. Key = MakeMD5 (key);
Des. Mode = CIPHERMODE.ECB;

Return DES. CreateDecryptor (). TransformFinalBlock (encrypted, 0, encrypted. Length);
}

<summary>
Encrypt with a given key
</summary>
<param name= "original" > Raw data </param>
<param name= "key" > Key </param>
<returns> redaction </returns>
public static byte[] Encrypt (byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes ("Jasonheung");
Return Encrypt (Original,key);
}

<summary>
Decrypting data with the default key
</summary>
<param name= "Encrypted" > Redaction </param>
<param name= "key" > Key </param>
<returns> Clear Text </returns>
public static byte[] Decrypt (byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes ("Jasonheung");
Return Decrypt (Encrypted,key);
}

}
}





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.