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;
MAC (message authentication code): Message Authentication Code Is a one-way function that uses keys and can be used to authenticate files or messages on the system or between 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 plain text is hashed and only one letter of the paragraph is modified, the subsequent hash will produce 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, and sha512managed
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. The built-in class in DOTNET: system. Web. Security. hashpasswordforstoringinconfigfile ()
Public String MD5 (string STR, int code)
{
If (code = 16) // 16-bit MD5 encryption (take 32-bit encryption 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";
}
easy to use:
// -- import the required package
using system. io;
using system. text;
using system. security. cryptography;
(1) MD5 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 [] 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) 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.
// 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 );
}