Some examples of cryptographic decryption standard functions in C #--DES,SHA1,RSA

Source: Internet
Author: User
Tags sha1

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;
}
}

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.