C # Implementation of DES algorithm

Source: Internet
Author: User
Tags decrypt

Des is the abbreviation for the data Encryption standard. It is a method of encrypting 64-bit data with a 56-bit key.


public class Encryptutility
{
#region DES
<summary>
DES encryption
</summary>
<param name= "Code" > Encrypted String </param>
<param name= "key" > Key </param>
<returns></returns>
public static string Desencrypt (string code, string key)
{
String IV = Stringutility.reverse (key);
Return Desencrypt (Code, key, IV);
}

<summary>
DES encryption
</summary>
<param name= "Code" > Encrypted String </param>
<param name= "key" > Key </param>
<param name= "IV" > Initialization vector </param>
<returns></returns>
public static string Desencrypt (string code, string key, String IV)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = Encoding.Default.GetBytes (code);
Des. Key = ASCIIEncoding.ASCII.GetBytes (key);
DES.IV = ASCIIEncoding.ASCII.GetBytes (IV);
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);
}
Ms. Dispose ();
Cs. Dispose ();
Ret. ToString ();
return ret. ToString ();
}


<summary>
Des decryption
</summary>
<param name= "Code" > Decrypt String </param>
<param name= "key" > Key </param>
<param name= "Key" ></param>
<returns></returns>
public static string Desdecrypt (string code, string key)
{
String IV = Stringutility.reverse (key);
Return Desdecrypt (Code, key, IV);
}


<summary>
Des decryption
</summary>
<param name= "Code" > Decrypt String </param>
<param name= "key" > Key </param>
<param name= "IV" > Initialization vector </param>
<returns></returns>
public static string Desdecrypt (string code, string key, String IV)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = new Byte[code. LENGTH/2];
for (int x = 0; x < code. LENGTH/2; X + +)
{
int i = (Convert.ToInt32 (code. Substring (x * 2, 2), 16));
INPUTBYTEARRAY[X] = (byte) i;
}
Des. Key = ASCIIEncoding.ASCII.GetBytes (key);
DES.IV = ASCIIEncoding.ASCII.GetBytes (IV);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
Cs. Dispose ();
StringBuilder ret = new StringBuilder ();
Return System.Text.Encoding.Default.GetString (Ms. ToArray ());
}
#endregion
}

C # Implementation of DES algorithm

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.