Asp.net encryption and decryption skills, asp.net encryption and decryption

Source: Internet
Author: User
Tags hmac

Asp.net encryption and decryption skills, asp.net encryption and decryption

We all know about encryption and decryption. The following describes encryption and decryption in ASP. NET.

I. Data Encryption/encoding algorithm list

Common encryption or encoding algorithms used to ensure security are as follows:

1. Common Key Algorithms

Key algorithms are used to encrypt sensitive data, summaries, signatures, and other information. common key algorithms include:

DES (Data Encryption Standard): Data Encryption Standard, fast, suitable for encrypting a large amount of Data;

3DES (Triple DES): Based on DES, three different keys are used to encrypt a piece of data three times, which is more powerful;

RC2 and RC4: Use a variable-length key to encrypt a large amount of data, which is faster than DES;

IDEA (International Data Encryption Algorithm) International Data Encryption Algorithm, uses a 128-bit key to provide very strong security;

RSA: A Public Key algorithm that supports variable-length keys, and the fast length of files to be encrypted is variable;

DSA (Digital Signature Algorithm): Digital Signature Algorithm, which is a standard DSS (Digital Signature Standard );

AES (Advanced Encryption Standard): Advanced Encryption Standard, which is the next-generation Encryption algorithm Standard with fast speed and high security level. Currently, one of the implementations of the AES Standard is the Rijndael algorithm;

BLOWFISH uses a variable-length key, which can contain up to 448 bits and runs fast;

Other algorithms, such as ElGamal, Deffie-Hellman, and ECC.

2. Unidirectional hashing algorithm

Unidirectional hash functions are generally used to generate message summaries and encrypt keys. Common examples include:

MD5 (Message Digest Algorithm 5): It is a one-way hash Algorithm developed by RSA Data Security Companies. MD5 is widely used, it can be used to compress data blocks of different lengths into a 128-bit value;

SHA (Secure Hash Algorithm) is a relatively new Hash Algorithm that can generate a 160-bit value for any-length data computation;

Message Authentication Code (MAC): Message Authentication Code. It is a one-way function that uses keys and can be used to authenticate files or messages between systems or users. HMAC is an example of this function.

CRC (Cyclic Redundancy Check): Cyclic Redundancy Check code. CRC Check is widely used in various data verification applications due to its simple implementation and high error checking capability. It occupies less system resources and can be implemented using software and hardware. It is a good method for data transmission error detection (CRC is not a strictly hashed algorithm, however, it serves roughly the same purpose as the hash algorithm ).

3. Other data Algorithms

Other data algorithms include some common Encoding algorithms and their conversion from plain text (ASCII, Unicode, etc.), such as Base 64, Quoted Printable, and EBCDIC.

Ii. algorithm. NET implementation

Common encryption and encoding algorithms have been implemented in. NET Framework, providing great convenience for the coding staff. The namespace for implementing these algorithms is System. Security. Cryptography.

The System. Security. Cryptography namespace provides encryption services, including secure data encoding and decoding, and many other operations, such as hash, random number generation, and message authentication.

System. Security. Cryptography is organized as follows:

1. Private Key Encryption

Private Key Encryption is also called symmetric encryption because the same key is used for both encryption and decryption. The private key encryption algorithm is very fast (compared with the public key algorithm), especially suitable for performing encryption and conversion on large data streams.

. NET Framework provides the following classes for implementing the private key encryption algorithm:

DES: DESCryptoServiceProvider
RC2: RC2CryptoServiceProvider
Rijndael (AES): RijndaelManaged
3DES: TripleDESCryptoServiceProvider
2. public key encryption and digital signature

Public key encryption uses a private key that must be kept confidential to unauthorized users and a public key that can be made public to anyone. Data Encrypted with the public key can only be decrypted with the private key, while data signed with the private key can only be verified with the public key. The public key can be used by anyone. The key is used to encrypt the data to be sent to the Private Key Holder. The two keys are unique for communication sessions. Public key encryption algorithms are also called asymmetric algorithms because one key is used to encrypt data and another key is used to decrypt data.

. NET Framework provides the following classes to implement public key encryption algorithms:

DSA: DSACryptoServiceProvider
RSA: RSACryptoServiceProvider

3. Hash Value

The hash algorithm maps binary values of any length to smaller binary values of a fixed length. This smaller binary value is called a hash value. A hash value is a unique and extremely compact numeric representation of a piece of data. If a piece of plain text is hashed and only one letter of the paragraph is modified, the subsequent hash will generate different values. It is impossible to calculate two different inputs with the same hash value. Therefore, the hash value of the data can be used to check the integrity of the data.

. NET Framework provides the following classes for implementing digital signature algorithms:

HMAC: HMACSHA1 (HMAC is a key-based Hash algorithm)
MAC: MACTripleDES
MD5: MD5CryptoServiceProvider
SHA1: SHA1Managed, SHA256Managed, SHA384Managed, SH7747.net12Managed
4. Random Number Generation

Encryption keys must be as random as possible to make the generated keys difficult to reproduce. Therefore, random number generation is an integral part of many encryption operations.

In. in the. NET Framework, RNGCryptoServiceProvider is the implementation of the random number generator algorithm. For data algorithms ,. NET Framework is implemented in other namespaces, such as the Convert class to implement Base 64 encoding, System. text to convert the encoding method.

From the above ,.. NET Framework supports data encryption and encoding, which greatly facilitates developers ,. the data encryption algorithms in the. NET Framework are still incomplete, such as IDEA, BLOWFISH, and other algorithms, such as ElGamal, Deffie-Hellman, and ECC. They do not support other data verification algorithms, for example, CRC and SFV, developers can only transplant early code or find third-party vendor implementations.

The following describes the encryption and decryption methods commonly used in projects.

I. MD5 Encryption Algorithm

[The MD5 Algorithm inherent in the. NET class library is an irreversible Algorithm Without decryption]

In fact, data is encrypted in ASP. Net programming. There are built-in classes in DotNet:

System. web. security. hashPasswordForStoringInConfigFile () public string md5 (string str, int code) {if (code = 16) // 16-bit MD5 encryption (get 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 the required package
Using System. IO;
Using System. Text;
Using System. Security. Cryptography;
(1) MD5 common Encryption

// Obtain the field to be encrypted and convert it to a Byte [] Array
Byte [] data = System. Text. Encoding. Unicode
. GetBytes (TextBox1.Text. ToCharArray ());
// Create an encryption service
System. Security. Cryptography. MD5 md5 = new System. Security. Cryptography. MD5CryptoServiceProvider ();
// Encrypt Byte [] Arrays
Byte [] result = md5.ComputeHash (data );
Label1.Text = "MD5 common encryption:" + System. Text. Encoding. Unicode. GetString (result );
(2) MD5 password encryption [common]

Label1.Text = "MD5 encryption:" + System. Web. Security. FormsAuthentication
. HashPasswordForStoringInConfigFile (TextBox1.Text, "MD5 ");
(3) encryption and decryption of QueryString in ASP. NET [common]

// Encryption
Response. Redirect ("DetailInfo. aspx? Id = "+ Convert. ToBase64String
(System. Text. Encoding. Default. GetBytes ("whaben"). Replace ("+", "% 2B "));
// Decrypt
String ID = System. Text. Encoding. Default. GetString
(Convert. FromBase64String (Request. QueryString ["id"]. ToString (). Replace ("% 2B", "+ ")));
Ii. DES encryption and decryption algorithms [common key algorithms

Simple use:

// -- Import the required package using System. IO; using System. text; using System. security. cryptography; public static string Key = "DKMAB5DE"; // the encryption Key must be 8 bits // the encryption algorithm public static string MD5Encrypt (string pToEncrypt) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte [] inputByteArray = Encoding. default. getBytes (pToEncrypt); des. key = ASCIIEncoding. ASCII. getBytes (Key); des. 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; x ++) {int I = (Convert. toInt32 (pToDecrypt. substring (x * 2, 2), 16); inputByteArray [x] = (byte) I;} des. key = ASCIIEncoding. ASCII. getBytes (Key); des. 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 ());}

Iii. RSA encryption and decryption algorithms [common key algorithms

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); decryptBytes [index] = Convert. toByte (substring, 16);} decryptBytes = RSAProvider. decrypt (decryptBytes, true); return ASCIIEncoding. ASCII. getString (decryptBytes );}

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.