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