Encryption algorithm of security

Source: Internet
Author: User
Tags crypt decrypt md5 encryption asymmetric encryption

Information security is more and more important today, the system design and development process of the necessary encryption technology is essential, based on the actual experience of the project to summarize the actual project in the safe use scenario.

The system internal password uses MD5 encryption, the password is not reversible, cannot use any means to let anyone obtain the user's password, if the security level is higher some can MD5 or mixes the encryption multiple times.

The security of special fields using symmetric encryption algorithm, commonly used Des/aes, for today's computer development speed,des encryption is relatively easy to crack, so it is recommended to use AES for encryption and decryption.

. NET DES encryption algorithm implementation

/// <summary>        ///Get the key/// </summary>        Private Static stringKey {Get{return @"Ads{}[]19123456789123456789a},ds"; } }        /// <summary>        ///Get Vector/// </summary>        Private Static stringIV {Get{return @"123456789{}, '; s"; } }        /// <summary>        ///AES Encryption/// </summary>        /// <param name= "Plainstr" >PlainText String</param>        /// <returns>Ciphertext</returns>         Public Static stringAesencrypt (stringplainstr) {            byte[] Bkey =Encoding.UTF8.GetBytes (Key); byte[] BIV =Encoding.UTF8.GetBytes (IV); byte[] ByteArray =Encoding.UTF8.GetBytes (PLAINSTR); stringEncrypt =NULL; Rijndael AES=rijndael.create (); Try            {                using(MemoryStream mstream =NewMemoryStream ()) {                    using(CryptoStream cstream =NewCryptoStream (Mstream, AES. CreateEncryptor (Bkey, BIV), CryptoStreamMode.Write)) {Cstream.write (ByteArray, 0, bytearray.length);                        Cstream.flushfinalblock (); Encrypt=convert.tobase64string (Mstream.toarray ()); }                }            }            Catch{} aes.            Clear (); returnencrypt; }        /// <summary>        ///AES Encryption/// </summary>        /// <param name= "Plainstr" >PlainText String</param>        /// <param name= "Returnnull" >whether to return Null,false when encryption fails returns String.Empty</param>        /// <returns>Ciphertext</returns>         Public Static stringAesencrypt (stringPLAINSTR,BOOLreturnnull) {            stringEncrypt =Aesencrypt (PLAINSTR); returnReturnnull? Encrypt: (encrypt = =NULL?String.Empty:encrypt); }        /// <summary>        ///AES Decryption/// </summary>        /// <param name= "Encryptstr" >Ciphertext String</param>        /// <returns>plaintext</returns>         Public Static stringAesdecrypt (stringencryptstr) {            byte[] Bkey =Encoding.UTF8.GetBytes (Key); byte[] BIV =Encoding.UTF8.GetBytes (IV); byte[] ByteArray =convert.frombase64string (ENCRYPTSTR); stringDecrypt =NULL; Rijndael AES=rijndael.create (); Try            {                using(MemoryStream mstream =NewMemoryStream ()) {                    using(CryptoStream cstream =NewCryptoStream (Mstream, AES. CreateDecryptor (Bkey, BIV), CryptoStreamMode.Write)) {Cstream.write (ByteArray, 0, bytearray.length);                        Cstream.flushfinalblock (); Decrypt=Encoding.UTF8.GetString (Mstream.toarray ()); }                }            }            Catch{} aes.            Clear (); returnDecrypt; }        /// <summary>        ///AES Decryption/// </summary>        /// <param name= "Encryptstr" >Ciphertext String</param>        /// <param name= "Returnnull" >whether to return Null,false when decryption fails returns String.Empty</param>        /// <returns>plaintext</returns>         Public Static stringAesdecrypt (stringENCRYPTSTR,BOOLreturnnull) {            stringDecrypt =Aesdecrypt (ENCRYPTSTR); returnReturnnull? Decrypt: (Decrypt = =NULL?String.Empty:decrypt); }

Implementation of asymmetric encryption. NET:

The first step: according to the above-mentioned principle, we need to generate the public key (the originator needs) and the private key (receiver needs), the implementation code is as follows://the methods for making public and private keys are as follows:RSACryptoServiceProvider crypt =NewRSACryptoServiceProvider ();stringPublicKey = Crypt. Toxmlstring (false);//Public KeystringPrivatekey = Crypt. Toxmlstring (true);//private KeyCrypt. Clear (); Second step: The sender uses the public key to encrypt the plaintext, the implementation code is as follows: StreamReader SR=NewStreamReader (Server.MapPath ("a.txt"), Utf8encoding.utf8);stringReadpublickey = Sr. ReadToEnd ();//an XML string that contains RSA key information. Sr. Close (); UTF8Encoding Enc=Newutf8encoding ();byte[] bytes =Enc. GetBytes (TextBox1.Text.Trim ()); RSACryptoServiceProvider Crypt=NewRSACryptoServiceProvider (); crypt. Fromxmlstring (readpublickey); bytes= Crypt. Encrypt (Bytes,false);stringEncryttext =convert.tobase64string (bytes);stringABB =Server.URLEncode (Encryttext); Response.Write ("The ciphertext is:"+ABB); The third step: The receiver uses the private key to decrypt the ciphertext, the implementation code is as follows: StreamReader SR=NewStreamReader (Server.MapPath ("B.txt"), Utf8encoding.utf8);stringReadprivatekey =Sr. ReadToEnd (); Sr. Close (); RSACryptoServiceProvider Crypt=NewRSACryptoServiceProvider (); UTF8Encoding Enc=Newutf8encoding ();byte[] bytes =convert.frombase64string (@Server. UrlDecode (TextBox1.Text.Trim)); crypt. Fromxmlstring (Readprivatekey);byte[] Decryptbyte = crypt. Decrypt (Bytes,false);stringDecrypttext =Enc. GetString (Decryptbyte); Response.Write ("Clear Text is:"+ Decrypttext);

Simple implementation:

Some of the external interfaces of the system display some data, in order to secure the data, the data provided by the interface is AES encrypted, then the client receives and then decrypts the operation. If you're still using DES encryption, make a quick replacement.

Follow-up hope to study and apply:

1. DDoS Protection techniques

2. Cross-site request forgery

3. XSS attack

4. File Upload Vulnerability

5. Information Garbage Filter

Encryption algorithm of security

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.