DES, RSA, MD5, SHA, randomly generated encryption and decryption

Source: Internet
Author: User
Tags decrypt hmac

I. List of data encryption/coding algorithms   common cryptographic or coding algorithms used to ensure security are as follows:  1, common key algorithm   key algorithm is used to encrypt sensitive data, digest, signature and other information, common key algorithm includes:  des (Data Encryption Standard): Data encryption standards, fast, suitable for encrypting large amounts of data;  3des (Triple des): DES, a piece of data with three different keys for three times encryption, higher intensity; &NBSP;RC2 and RC4: Using variable-length key to encrypt large amounts of data, faster than DES,  idea (International Data Encryption algorithm) International encryption algorithm, using 128-bit key to provide very strong security;  RSA: Invented by RSA, is a public key algorithm that supports variable-length keys, and the fast length of files that need to be encrypted is also variable; &NBSP;DSA (digital Signature algorithm): Digitally signed algorithm, is a standard DSS (digital signature standard);  aes (Advanced encryption): High Encryption Standard, is the next generation of cryptographic algorithm standard, fast, high security level, the current implementation of the AES standard is Rijndael algorithm;  blowfish, It uses a variable-length key that can be up to 448 bits long and runs fast;  other algorithms such as ElGamal, Deffie-hellman, and ECC, a new elliptic curve algorithm.  2, one-way hash algorithm   One-way hash function is generally used to generate message digest, key encryption, etc., Common is: &NBSP;MD5 (message Digest algorithm 5): RSA Data Security Company developed a one-way hashing algorithm, MD5 is widely used, it can be used to set different lengths of data blocks into a 128-bit numeric value;  sha (Secure Hash algorithm) This is a new hashing algorithm that can generate a 160-bit value for any length of data operation. &NBSP;MAC (Message authentication code): Messaging authentication code, which is a one-way function that uses keys to authenticate files or messages between the system or the user. An HMAC (key hashing method for message authentication) is an example of this function. &NBSP;CRC (Cyclic redundancy check): Cyclic redundancy check code, CRC is widely used in various data verification applications because of its simple implementation and high error detection capability. Less system resources, hardware and software can be achieved, is a data transmission error detection is a good means (CRC is not strictly a hashing algorithm, but its function and hash algorithm is roughly the same, so attributed to this class).  3, other data algorithms   Other data algorithms include some common coding algorithms and their conversion to plaintext (ASCII, Unicode, etc.), such as Base 64, Quoted Printable, EBCDIC, etc.  , the. NET implementation of the algorithm   common encryption and encoding algorithms have been implemented in the. NET Framework, providing great convenience for coders, The namespaces that implement these algorithms are: System.Security.Cryptography. The  system.security.cryptography namespace provides cryptographic services, including secure data encoding and decoding, and many other operations, such as hashing, random number generation, and message authentication.  system.security.cryptography is organized as follows: &NBSP;1, private key encryption   Private key encryption, also known as symmetric encryption, because the same key is used both for encryption and for decryption. Private key encryption algorithms are very fast (compared to public-key algorithms) and are especially useful for performing cryptographic transformations on large data streams. The  .net Framework provides the following classes that implement a private key encryption algorithm:  DES:DESCryptoServiceProviderRC2:RC2CryptoServiceProviderRijndael (AES): Rijndaelmanaged3des:tripledescryptoserviceprovider2 , public key cryptography and digital signatures   public key cryptography uses a private key that must be kept secret from unauthorized users and a public key that can be exposed to anyone. Data encrypted with the public key can only be decrypted with the private key, and the data signed with the private key can only be verified with the public key. The public key can be used by anyone, and the key is used to encrypt the data to be sent to the private key holder. Two keys are unique to a communication session. Public-key cryptography, also known as asymmetric algorithms, is due to the need to encrypt data with one key and to decrypt the data with another key. The  .net Framework provides the following classes that implement a public key cryptography algorithm:  dsa:dsacryptoserviceproviderrsa:rsacryptoserviceprovider3, hash value &NBSP; The hashing algorithm maps a binary value of any length to a small, fixed-length binary value, which is called a hash value. A hash value is a unique and extremely compact numeric representation of a piece of data. If you hash a clear text and even change only one letter of the paragraph, subsequent hashes will produce different values. To find two different inputs that hash the same value, it is not possible to compute, so the hash value of the data can verify the integrity of the data. The  .net Framework provides the following classes for implementing a digital Signature algorithm: &NBSP;HMAC:HMACSHA1 (HMAC is a Hash that uses a key Algorithm) MAC:MACTripleDESMD5:MD5CryptoServiceProviderSHA1:SHA1Managed, sha256managed, sha384managed, SH7747.NET12MANAGED4, random number generation   encryption keys need to be as random as possible to make the generated keys difficult to reproduce, so random number generation is an integral part of many cryptographic operations.   in the. NET Framework, RNGCryptoServiceProvider is the implementation of the random number generator algorithm, and for data algorithms, the. NET Framework is implemented in other namespaces, such as the Convert class implementing Base 64 encoding , System.Text to achieve the conversion of the encoding method, and so on.   from the above, the. NET Framework is better for data encryption/encoding or support, greatly facilitated by developers, but the drawback is that the data encryption algorithms in the. NET Framework are still not complete enough, such as idea, BLOWFISH, other algorithms, such as ElGamal, Deffie-hellman, ECC, etc., for some other data validation algorithm support is not enough, such as CRC, SFV, etc., developers can only go from the early code to transplant or look for the implementation of third-party vendors.   The following is a brief introduction to the common methods of encryption and decryption in the project   A, MD5 encryption algorithm  [. NET class library of the algorithm MD5 is an irreversible algorithm does not decrypt the algorithm]  in fact, the data is encrypted in ASP. Classes in dotnet:  system.web.security.hashpasswordforstoringinconfigfile () public string MD5 (string Str,int code) {if (code==16)//16-bit MD5 encryption(Take 32-bit encrypted 9~25 characters) {return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower (). Substring (8,16);} if (code==32)//32-bit encryption {return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5") ). ToLower ();} return "00000000000000000000000000000000";} Simple use:  //--Import required package using system.io;using system.text;using System.Security.Cryptography; (1) MD5 General encryption  // Get the field to encrypt and convert to byte[] array byte[] data = System.Text.Encoding.Unicode.GetBytes (TextBox1.Text.ToCharArray ());// Establish cryptographic service System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();//Encryption byte[ ] array byte[] result = Md5.computehash (data); Label1.Text = "MD5 normal encryption:" + System.Text.Encoding.Unicode.GetString (Result);(2) MD5 password encryption [Common] label1.text = " MD5 Password Encryption: "+ System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (TextBox1.Text," MD5 ");(3) Asp. The method of encrypting and decrypting querystring in net [Common] //encryption Response.Redirect ("detailinfo.aspx?id=" + convert.tobase64string(System.Text.Encoding.Default.GetBytes ("Whaben")). Replace ("+", "+"));//Decrypt string ID = System.Text.Encoding.Default.GetString (convert.frombase64string ( request.querystring["id"]. ToString (). Replace ("+", "+")), DES Encryption and decryption algorithm [Common key algorithm]  simple use:  //--Import required package using system.io;using system.text;using System.security.cryptography;public static string key = "DKMAB5DE";//The encryption key must be a 8-bit//cryptographic algorithm public static string Md5encrypt ( String ptoencrypt) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte[] Inputbytearray = Encoding.Default.GetBytes (ptoencrypt);d es. Key = ASCIIEncoding.ASCII.GetBytes (key);d ES.IV = ASCIIEncoding.ASCII.GetBytes (key); 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);} Ret. ToString (); return ret. ToString ();} Decryption algorithm public static STRing Md5decrypt (string ptodecrypt) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte[] Inputbytearray = new Byte[ptodecrypt.length/2];for (int x = 0; x < PTODECRYPT.LENGTH/2; + +) {int i = (Convert.ToInt32 (ptodecrypt.s Ubstring (x * 2, 2), +)); Inputbytearray[x] = (byte) i;} Des. Key = ASCIIEncoding.ASCII.GetBytes (key);d ES.IV = ASCIIEncoding.ASCII.GetBytes (key); MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); Cs. Write (Inputbytearray, 0, inputbytearray.length); Cs. FlushFinalBlock (); StringBuilder ret = new StringBuilder (); return System.Text.Encoding.ASCII.GetString (Ms. ToArray ());} Third, RSA encryption and decryption algorithm [Common key algorithm]  simple use:  //--Import the required package using system.text;using system.security.cryptography;// Encryption algorithm public string Rsaencrypt (string encryptstring) {CspParameters CSP = new CspParameters (); CSP. KeyContainerName = "Whaben"; RSACryptoServiceProvider Rsaprovider = new RSACryptoServiceProvider (CSP); byte[] Encryptbytes = RSAprovider.encrypt (ASCIIEncoding.ASCII.GetBytes (encryptstring), true); string str = ""; foreach (Byte b in encryptbytes) { str = str + string. Format ("{0:x2}", b);} return str;} Decryption algorithm public string Rsadecrypt (string decryptstring) {CspParameters CSP = new CspParameters (); CSP. KeyContainerName = "Whaben"; RSACryptoServiceProvider Rsaprovider = new RSACryptoServiceProvider (CSP); int length = (DECRYPTSTRING.LENGTH/2); byte[] Decryptbytes = new Byte[length];for (int index = 0; index < length; index++) {String substring = decryptstring.substring (Index * 2, 2);d Ecryptbytes[index] = convert.tobyte (substring, 16);} Decryptbytes = Rsaprovider.decrypt (Decryptbytes, True); return ASCIIEncoding.ASCII.GetString (decryptbytes);}

DES, RSA, MD5, SHA, randomly generated encryption and decryption

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.