) DES, RSA, MD5, SHA, random generation encryption and decryption, rsa encryption and decryption

Source: Internet
Author: User
Tags hmac

1. List of data encryption / encoding algorithms

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

1. Commonly used key algorithms

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

DES (Data EncryptionStandard): data encryption standard, which is faster and suitable for the occasion of encrypting a large amount of data;

3DES (TripleDES): Based on DES, a piece of data is encrypted three times with three different keys, with higher strength;

RC2 and RC4: Encrypt large amounts of data with variable-length keys, which is faster than DES;

IDEA (International Data Encryption Algorithm) international data encryption algorithm, using 128-bit key to provide very strong security;

RSA: Invented by RSA, it is a public key algorithm that supports variable-length keys. The length of the file 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, which is the next generation encryption algorithm standard with high speed and high security level. One implementation of the current AES standard is the Rijndael algorithm;

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

Other algorithms, such as ElGamal, Deffie-Hellman, new elliptic curve algorithm ECC, etc.

2. One-way hash algorithm

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

MD5 (Message Digest Algorithm5): It is a one-way hash algorithm developed by RSA Data Security. MD5 is widely used and can be used to cryptographically calculate data blocks of different lengths into a 128-bit value

SHA (Secure HashAlgorithm) This is a newer hash algorithm that can generate a 160-bit value on any length of data;

MAC (Message AuthenticationCode): Message authentication code is a one-way function that uses a key. You can use them to authenticate files or messages on the system or between users. HMAC (Key Hashing Method for Message Authentication) is an example of such a function.

CRC (Cyclic RedundancyCheck): Cyclic Redundancy Check Code. Due to its simple implementation and strong error detection capability, CRC check is widely used in various data check applications. It occupies less system resources and can be implemented with hardware and software. It is a good method for data transmission error detection (CRC is not a hash algorithm in the strict sense, but its role is roughly the same as the hash algorithm, so it is Such).

3. Other data algorithms

Other data algorithms include some commonly used encoding algorithms and their conversion with plain text (ASCII, Unicode, etc.), such as Base 64, QuotedPrintable, EBCDIC, etc.

Second, the .NET implementation of the algorithm

Common encryption and encoding algorithms have been implemented in the .NET Framework, providing great convenience for coders. 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 hashing, 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 to the public key algorithm), and is particularly suitable for performing encryption conversions on larger data streams.

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

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 secret from unauthorized users and a public key that can be disclosed 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 data to be sent to the holder of the private key. Both keys are unique to the communication session. The public key encryption algorithm is also called an asymmetric algorithm because the data needs to be encrypted with one key and the data needs to be decrypted with another key.

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

DSA: DSACryptoServiceProvider
RSA: RSACryptoServiceProvider
3. Hash value

The hash algorithm maps a binary value of any length to a fixed-length smaller binary value. This small binary value is called a hash value. A hash value is a unique and extremely compact numerical representation of a piece of data. If you hash a paragraph of plain text and even change only one letter of the paragraph, subsequent hashes will produce different values. It is computationally impossible to find two different inputs hashed to the same value, so the hash value of the data can verify the integrity of the data.

The .NET Framework provides the following classes that implement digital signature algorithms:

HMAC: HMACSHA1 (HMAC is a hash algorithm using keys)
MAC: MACTripleDES
MD5: MD5CryptoServiceProvider
SHA1: SHA1Managed, SHA256Managed, SHA384Managed, SH7747.net12Managed
4. Random number generation

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

In the .NET Framework, RNCCryptoServiceProvider is the implementation of the random number generator algorithm. For the data algorithm, the .NET Framework is implemented in other namespaces, such as the Convert class to implement Base64 encoding, and the System.Text to implement encoding conversion.

From the above point of view, .NET Framework still supports data encryption / encoding better, which greatly facilitates developers, but the disadvantage is that the data encryption algorithm in .NET Framework is still not complete, such as IDEA, BLOWFISH, and other algorithms, such as ElGamal, Deffie-Hellman, ECC, etc., are not enough to support some other data verification algorithms, such as CRC, SFV, etc., developers can only transplant from early code or find the implementation of third-party vendors.

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

1. MD5 encryption algorithm

[The algorithm MD5 in the .NET class library is an irreversible algorithm without decryption algorithm]

Actually encrypt data in ASP.Net programming. There are classes in DotNet:

System.Web.Security.HashPasswordForStoringInConfigFile ()
public string md5 (string str, int code)
{
if (code == 16) // 16-bit MD5 encryption (take 9 to 25 characters of 32-bit encryption)
{
returnSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5")
.ToLower (). Substring (8,16);
}
if (code == 32) // 32-bit encryption
{
returnSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5")
.ToLower ();
}
return "00000000000000000000000000000000";
}
Simple use:

// --- import the required packages
using System.IO;
using System.Text;
using System.Security.Cryptography;
(1) MD5 ordinary encryption

// Get the field to be encrypted and convert it to Byte [] array
byte [] data = System.Text.Encoding.Unicode
.GetBytes (TextBox1.Text.ToCharArray ());
// Establish encryption service
System.Security.Cryptography.MD5 md5 = newSystem.Security.Cryptography.MD5CryptoServiceProvider ();
// Encrypt Byte [] array
byte [] result = md5.ComputeHash (data);
Label1.Text = "MD5 ordinary encryption:" + System.Text.Encoding.Unicode.GetString (result);
(2) MD5 password encryption [commonly used]

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

// Encrypt
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 ("+", "+")));
2. DES encryption and decryption algorithm [common key algorithm]

Simple use:

// --- import the required packages
using System.IO;
using System.Text;
using System.Security.Cryptography;
public static string Key = "DKMAB5DE"; // The encryption key must be 8 digits
//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 ());
}
3. RSA encryption and decryption algorithm [common key algorithm]

Simple use:

// --- import the required packages
using System.Text;
using System.Security.Cryptography;
//Encryption Algorithm  
public string RSAEncrypt (string encryptString)
{
CspParameters csp = new CspParameters ();
csp.KeyContainerName = "whaben";
RSACryptoServiceProvider RSAProvider = newRSACryptoServiceProvider (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 = newRSACryptoServiceProvider (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.