Recently received a lot of friends from the letter said that want to provide des C # code, but I personally think that. NET provides a lot of standard functions, no need to write their own, so I also published only C + + code, if you must be familiar with the encryption process, you can do the whole process yourself, This can refer to my blog in the DES algorithm introduction, and YxyDES2 class code, code annotation is quite clear.
. NET provides a number of standard encryption and decryption functions, and I briefly describe the use of DES,SHA1,RSA's standard functions. If you want to do a network security module, just combine three algorithms to design a model, I believe that can achieve a lot of complex functions.
The example itself is not complicated, I do not do too much explanation, I also learn Linus Torvalds like yelling: "Read the F**ing Code", Haha, a joke, I believe we can certainly understand.
Note: The following example needs to reference a namespace: using System.Security.Cryptography;
A. DES encryption, decryption
I believe that the annotation is quite clear, plus my blog about des articles do quite a lot, so des do not do any explanation, how to call the more do not have to explain it, hehe:
Default key vector
Private byte[] Keys = {0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12};
<summary>
Des encrypted string
</summary>
<param name= "encryptstring" > Strings to be encrypted </param>
<param name= "Encryptkey" > Encryption key required for 8-bit </param>
<returns> encryption successfully returned the encrypted string, failed to return the source string </returns>
public string Encryptdes (string encryptstring, String encryptkey)
{
Try
{
byte[] Rgbkey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));
byte[] Rgbiv = Keys;
byte[] Inputbytearray = Encoding.UTF8.GetBytes (encryptstring);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, Dcsp.createencryptor (Rgbkey, Rgbiv), cryptostreammode.write);
Cstream.write (Inputbytearray, 0, inputbytearray.length);
Cstream.flushfinalblock ();
Return convert.tobase64string (Mstream.toarray ());
}
Catch
{
return encryptstring;
}
}
<summary>
Des decryption string
</summary>
<param name= "decryptstring" > Strings to be decrypted </param>
<param name= "Decryptkey" > Decryption key, required 8-bit, same as encryption key </param>
<returns> decryption successfully returned decrypted string, failed back source string </returns>
public string Decryptdes (string decryptstring, String decryptkey)
{
Try
{
byte[] Rgbkey = Encoding.UTF8.GetBytes (decryptkey.substring (0, 8));
byte[] Rgbiv = Keys;
byte[] Inputbytearray = convert.frombase64string (decryptstring);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, DCSP. CreateDecryptor (Rgbkey, Rgbiv), cryptostreammode.write);
Cstream.write (Inputbytearray, 0, inputbytearray.length);
Cstream.flushfinalblock ();
Return Encoding.UTF8.GetString (Mstream.toarray ());
}
Catch
{
return decryptstring;
}
}