Symmetric encryption, asymmetric encryption

(A) symmetric encryption (Symmetric Cryptography)
symmetric encryption is the fastest and easiest way encryption, encryption (Encryption) and decryption (Decryption) use the same key (secret key). There are many symmetric encryption algorithms, it is due to the high efficiency, it is widely used encryption protocol in the heart of many of them.

Symmetric encryption is generally used key is relatively small, typically less than 256 bit. Because the larger the key, the stronger the encryption, but the slower the process of encryption and decryption. If you only do this with a 1 bit key, that hackers can use to try to decrypt 0, then 1, then it does not work solution; but if you have 1 MB big key, hackers could never break, but the encryption and decryption process takes a very long time. Size of the key not only to take care of security, but also to take care of efficiency, is a trade-off.

October 2, 2000, the US National Institute of Standards and Technology (NIST-American National Institute of Standards and Technology) chose the Rijndael algorithm as the new Advanced Encryption Standard (AES-Advanced Encryption Standard). .NET contained in the Rijndael algorithm, called the RijndaelManaged class, the following example.

The encryption process:

private string myData = "hello";
        private string myPassword = "OpenSesame";
        private byte[] cipherText;
        private byte[] salt = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0 };


        private void mnuSymmetricEncryption_Click(object sender, RoutedEventArgs e)
        {
            var key = new Rfc2898DeriveBytes(myPassword, salt);
            // Encrypt the data.
            var algorithm = new RijndaelManaged();
            algorithm.Key = key.GetBytes(16);
            algorithm.IV = key.GetBytes(16);
            var sourceBytes = new System.Text.UnicodeEncoding().GetBytes(myData);
            using (var sourceStream = new MemoryStream(sourceBytes))
            using (var destinationStream = new MemoryStream())
            using (var crypto = new CryptoStream(sourceStream, algorithm.CreateEncryptor(), CryptoStreamMode.Read))
            {
                moveBytes(crypto, destinationStream);
                cipherText = destinationStream.ToArray();
            }
            MessageBox.Show(String.Format("Data:{0}{1}Encrypted and Encoded:{2}", myData, Environment.NewLine, Convert.ToBase64String(cipherText)));
        }
        private void moveBytes(Stream source, Stream dest)
        {
            byte[] bytes = new byte[2048];
            var count = source.Read(bytes, 0, bytes.Length);
            while (0 != count)
            {
                dest.Write(bytes, 0, count);
                count = source.Read(bytes, 0, bytes.Length);
            }
        }

Decryption process:

private void mnuSymmetricDecryption_Click(object sender, RoutedEventArgs e)
        {
            if (cipherText == null)
            {
                MessageBox.Show("Encrypt Data First!");
                return;
            }
            var key = new Rfc2898DeriveBytes(myPassword, salt);
            // Try to decrypt, thus showing it can be round-tripped.
            var algorithm = new RijndaelManaged();
            algorithm.Key = key.GetBytes(16);
            algorithm.IV = key.GetBytes(16);
            using (var sourceStream = new MemoryStream(cipherText))
            using (var destinationStream = new MemoryStream())
            using (var crypto = new CryptoStream(sourceStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read))
            {
                moveBytes(crypto, destinationStream);
                var decryptedBytes = destinationStream.ToArray();
                var decryptedMessage = new UnicodeEncoding().GetString(
                decryptedBytes);
                MessageBox.Show(decryptedMessage);
            }
        }

Hand symmetric encryption is a major drawback is the key management and distribution, in other words, how to send your key to decrypt the message needs people is a problem. In the process of sending the key, the key there is a great risk of being intercepted hackers. In reality the usual practice is to symmetric encryption key for asymmetric encryption and then transferred to those who need it.


(Ii) asymmetric encryption (Asymmetric Cryptography)
asymmetric encryption and decryption of data encryption provides a very secure method, which uses a pair of keys, public key (public key) and a private key (private key). Private safe custody only by one party, can not be compromised, and the public key can be distributed to anyone who requests it. This asymmetric encryption using a key to encrypt, and decrypt the other key is required. For example, you request a public key to the bank, the bank will send you a public key, you use the public key to encrypt a message, only the private key holders - banks can decrypt your messages. And symmetric encryption is different, the bank need not be sent over the private network, security is greatly enhanced.

The most commonly used asymmetric encryption algorithm is the RSA algorithm, a Rivest, Shamir, and Adleman invented in 1978, when they were at MIT. .NET also has the RSA algorithm, see the following example:

The encryption process:

private byte[] rsaCipherText;
        private void mnuAsymmetricEncryption_Click(object sender, RoutedEventArgs e)
        {
            var rsa = 1;
            // Encrypt the data.
            var cspParms = new CspParameters(rsa);
            cspParms.Flags = CspProviderFlags.UseMachineKeyStore;
            cspParms.KeyContainerName = "My Keys";
            var algorithm = new RSACryptoServiceProvider(cspParms);
            var sourceBytes = new UnicodeEncoding().GetBytes(myData);
            rsaCipherText = algorithm.Encrypt(sourceBytes, true);
            MessageBox.Show(String.Format("Data: {0}{1}Encrypted and Encoded: {2}",
                myData, Environment.NewLine,
                Convert.ToBase64String(rsaCipherText)));
        }

Decryption process:

private void mnuAsymmetricDecryption_Click(object sender, RoutedEventArgs e)
        {
            if(rsaCipherText==null)
            {
                MessageBox.Show("Encrypt First!");
                return;
            }
            var rsa = 1;
            // decrypt the data.
            var cspParms = new CspParameters(rsa);
            cspParms.Flags = CspProviderFlags.UseMachineKeyStore;
            cspParms.KeyContainerName = "My Keys";
            var algorithm = new RSACryptoServiceProvider(cspParms);
            var unencrypted = algorithm.Decrypt(rsaCipherText, true);
            MessageBox.Show(new UnicodeEncoding().GetString(unencrypted));
        }

Although asymmetric encryption is very secure, but it is very slow and compared to symmetric encryption, so we still have to use symmetric encryption to deliver messages, but symmetric encryption key used we can send out through asymmetric encryption. To explain this process, consider the following example:

(1) Alice needs to do a deal at the bank's website, her browser first generates a random number as the symmetric key.

(2) Alice's browser requests a public key to the bank's website.

(3) the bank will send the public key to Alice.

(4) Alice's browser using the bank's own public key symmetric key encryption.

(5) Alice's browser will send the encrypted symmetric key to the bank.

(6) Bank obtained using the private key to decrypt the symmetric key Alice browser.

(7) Alice and the bank can use a symmetric key to encrypt and decrypt the content of the communication.
Write pictures described here

(C) Summary
(1) symmetric encryption using encryption and decryption keys are the same, so fast, but because of the need to key network transmission, so security is not high.

(2) asymmetric cryptography uses a pair of keys, public and private keys, so that safe, but slow encryption and decryption.

(3) The solution is a symmetric encryption key using an asymmetric encryption public key to encrypt, and then sent, the recipient uses a private key to decrypt the symmetric encryption keys obtained, then the two sides can communicate using symmetric encryption .

Original Address: http://www.cnblogs.com/jfzhu/p/4020928.html

Please indicate the source

Guess you like

Origin blog.csdn.net/WilliamsWayne/article/details/78959522