In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "asp.net encryption and decryption skills". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
We all know about encryption and decryption. The following is mainly about encryption and decryption in ASP.NET.
List of data encryption / coding algorithms
Common encryption or coding algorithms for security are as follows:
1. Common key algorithms
Key algorithms are used to encrypt sensitive data, digests, signatures and other information. Common key algorithms include:
DES (Data Encryption Standard): data encryption standard, fast, suitable for encrypting large amounts of data
3DES (Triple DES): based on DES, a piece of data is encrypted three times with three different keys for higher strength.
RC2 and RC4: encrypt large amounts of data with variable length keys, faster than DES
IDEA (International Data Encryption Algorithm) international data encryption algorithm that uses 128bit keys to provide very strong security
RSA: invented by RSA, it is a public key algorithm that supports variable length keys. The fast length of files that need to be encrypted is also 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 high speed and high security level. At present, one implementation of AES standard is Rijndael algorithm.
BLOWFISH, which uses variable-length keys, can be up to 448 bits long and runs very fast
Other algorithms, such as ElGamal, Deffie-Hellman, new elliptic curve algorithm ECC, etc.
2. One-way hashing algorithm
One-way hash functions are generally used to generate message digests, key encryption, and so on. Common ones are:
MD5 (Message Digest Algorithm 5): an one-way hashing algorithm developed by RSA data Security, MD5 is widely used to code blocks of different lengths into a 128-bit value.
SHA (Secure Hash Algorithm) this is a relatively new hash algorithm, which can generate a 160bit value for data operations of arbitrary length.
MAC (Message Authentication Code): message authentication code, an one-way function that uses keys to authenticate files or messages on the system or between users. HMAC (the key hashing method for message authentication) is an example of such a function.
CRC (Cyclic Redundancy Check): cyclic redundancy check code, CRC check is widely used in various data check applications because of its simple implementation and strong error detection ability. It takes up less system resources and can be realized by both software and hardware, so it is a good means to detect errors in data transmission (CRC is not a hash algorithm in the strict sense, but its function is roughly the same as the hash algorithm, so it belongs to this category).
3. Other data algorithms
Other data algorithms include some commonly used coding algorithms and their conversion to plaintext (ASCII, Unicode, etc.), such as Base64, Quoted Printable, EBCDIC and so on.
2. Net implementation of the algorithm.
Common encryption and encoding algorithms have been implemented in the .NET Framework, providing great convenience for coders. The namespace that implements 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. Private key encryption algorithms are very fast (compared with public key algorithms) and are especially suitable for performing encryption transformations on large 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 made public to anyone. Data encrypted with a public key can only be decrypted with a private key, while data signed with a private key can only be verified with a public key. The public key can be used by anyone; the key is used to encrypt data to be sent to the private key holder. Both keys are unique to the communication session. Public key encryption algorithms are also known as asymmetric algorithms because data needs to be encrypted with one key and 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 smaller binary value of a fixed length, which is called a hash value. A hash is the only and extremely compact numerical representation of a piece of data. If you hash a plaintext and change even one letter of the paragraph, the subsequent hash will produce a different value. It is computationally impossible to find two different inputs that hash the same value, so the hash 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 that uses keys)
MAC:MACTripleDES
MD5:MD5CryptoServiceProvider
SHA1:SHA1Managed 、 SHA256Managed 、 SHA384Managed 、 SH7747.net12Managed
4. 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 encryption operations.
In .NET Framework, RNGCryptoServiceProvider is the implementation of random number generator algorithm. For data algorithms,. NET Framework is implemented in other namespaces, such as Convert class to implement Base64 encoding, System.Text to achieve coding conversion and so on.
Judging from the above,. NET Framework still supports data encryption / coding, which is very convenient for developers, but the only drawback is that the data encryption algorithms in .NET Framework are still not complete, such as IDEA, BLOWFISH, other algorithms, such as ElGamal, Deffie-Hellman, ECC, etc., and not enough support for some other data verification algorithms, such as CRC, SFV, etc. Developers can only port from early code or look for third-party implementations.
The following is a brief introduction to the encryption and decryption methods commonly used in the project.
1. MD5 encryption algorithm
[the algorithm included in the .NET class library MD5 is an irreversible algorithm without a decryption algorithm]
Actually encrypt data 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 (take 32-bit encryption 9'25 characters) {return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower (). Substring (8line 16);} if (code==32) / / 32-bit encryption {return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower ();} return "00000000000000000000000000000000";}
Simple to use:
/ /-- Import the required packages
Using System.IO
Using System.Text
Using System.Security.Cryptography
(1) MD5 ordinary encryption
/ / get the fields to be encrypted and convert them to Byte [] array
Byte [] data = System.Text.Encoding.Unicode
.GetBytes (TextBox1.Text.ToCharArray ())
/ / establish 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 [commonly used]
Label1.Text = "MD5 password encryption:" + System.Web.Security.FormsAuthentication
.HashPasswordForStoringInConfigFile (TextBox1.Text, "MD5")
(3) methods of encrypting and decrypting QueryString in ASP.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 required package using System.IO; using System.Text; using System.Security.Cryptography; public static string Key = "DKMAB5DE"; / / encryption key must be 8-bit / / 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 ());}
Algorithms for RSA encryption and decryption [common key algorithms]
Simple to 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) } "asp.net encryption and decryption skills" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.