asp.net encryption and decryption techniques _ practical skills

Source: Internet
Author: User
Tags decrypt hmac md5 md5 encryption

For encryption and decryption, we all know that. The following is mainly about encryption and decryption in asp.net.

A list of data encryption/coding algorithms

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

1. Common Key algorithm

The key algorithm is used to encrypt sensitive data, abstract, signature and other information, commonly used key algorithms include:

DES (encryption Standard): Data Encryption Standard, faster, suitable for encryption of large amounts of data on the occasion;

3DES (Triple des): is based on DES, a piece of data with three different keys for three times encryption, higher intensity;

RC2 and RC4: Encrypt large amounts of data with variable-length keys, 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;

DSA (Digital Signature algorithm): Digital Signature Algorithm, is a standard DSS (digital signature standard);

AES (Advanced encryption Standard): Advanced Encryption Standard, is the next generation of encryption algorithm standard, fast, high security level, the current AES standard one implementation is Rijndael algorithm;

BLOWFISH, it uses the variable length key, the length can reach 448 bits, the operation speed is very fast;

Other algorithms, such as ElGamal, Deffie-hellman, new elliptic curve algorithm ECC and so on.

2, one-way hashing algorithm

One-way hash functions are generally used to generate message digest, key encryption, etc., common are:

MD5 (Message Digest algorithm 5): is a one-way hashing algorithm developed by RSA Data security Company, MD5 is widely used, can be used to the different length of the block of data into a 128-bit value;

SHA (Secure Hash algorithm) This is a new hashing algorithm that can generate a 160-bit value for any length of data operation;

MAC (Message authentication code): Messaging authentication codes, a one-way function that uses keys to authenticate files or messages between a system or a user. HMAC (the key hashing method for message authentication) is an example of this function.

CRC (Cyclic redundancy check): Cyclic redundancy check code, CRC checksum due to the implementation of simple, error detection ability, is widely used in a variety of data validation applications. The use of less system resources, hardware and software can be achieved, is a data transmission error detection of a good means (CRC is not strictly a hashing algorithm, but its role and hashing algorithm is roughly the same, so attributed to this category).

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.

Second, the algorithm of. NET implementation

Common encryption and coding algorithms have been implemented in the. NET framework, providing great convenience for coders, and the namespaces for implementing 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 are organized in the following manner:

1, private key encryption

Private key cryptography is 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 larger data streams.

The. NET Framework provides the following classes that implement the private key encryption algorithm:

Des:descryptoserviceprovider
Rc2:rc2cryptoserviceprovider
Rijndael (AES): RijndaelManaged
3des:tripledescryptoserviceprovider
2, public key encryption and digital signature

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, while data signed with the private key can only be authenticated with the public key. The public key can be used by anyone, and the key is used to encrypt data to be sent to the private key holder. Two keys are unique to a communication session. Public-key cryptography is also known as an asymmetric algorithm because of the need to encrypt data with one key and another key to decrypt the data.

The. NET Framework provides the following classes that implement the public Key cryptography algorithm:

Dsa:dsacryptoserviceprovider
Rsa:rsacryptoserviceprovider

3, hash (hash) value

The hash algorithm maps the binary value of any length to a smaller binary value of a fixed length, 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 computationally impossible, so the hash value of the data can verify the integrity of the data.

The. NET Framework provides the following classes that implement the digital Signature algorithm:

HMAC:HMACSHA1 (HMAC is a Hash algorithm that uses a key)
Mac:mactripledes
Md5:md5cryptoserviceprovider
Sha1:sha1managed, SHA256Managed, sha384managed, sh7747.net12managed
4. Random number generation

The encryption key needs to be as random as possible so that the generated key is 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, which implements Base 64 encoding, Syste M.text to achieve the conversion of the encoding method.

In view of the above, the. NET Framework is better for data encryption/coding or support, and greatly facilitates developers, but the drawback is that the data encryption algorithms in the. NET Framework are still not complete enough, such as idea, BLOWFISH, and other algorithms, such as ElGamal, Deffie-hellman, ECC, for some other data validation algorithm support is not enough, such as CRC, SFV, developers can only go from the early code to do porting or looking for third-party vendors to achieve.

The following is a brief introduction to the commonly used encryption and decryption methods in the project

First, MD5 encryption algorithm

[. NET class library with the algorithm MD5 is an irreversible algorithm does not decrypt the algorithm]

Actually encrypt the data in asp.net programming. There are dotnet classes in the box:

System.Web.Security.HashPasswordForStoringInConfigFile () public 
string MD5 (string Str,int code) 
{ 
if ( CODE==16)///16-bit MD5 encryption (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 to use:

--Import the 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 Services
System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();
Encrypted byte[] Array
Byte[] result = Md5.computehash (data);
Label1.Text = "MD5 General 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 [commonly used]

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", "+"));
second, DES Encryption and decryption algorithm [Common key algorithm

Simple to 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 a 8-bit//encryption algorithm public static string Md5encrypt (String ptoencrypt) {Descrypt 
Oserviceprovider 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 Descryptoserviceprovi 
Der (); 
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 ());  }

RSA encryption and decryption algorithm [Common key algorithm

Simple to use:

--Import the required package using System.Text; 
Using System.Security.Cryptography; 
Cryptographic 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 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.