当前位置:网站首页>Quickly understand the commonly used symmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry

Quickly understand the commonly used symmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry

2022-06-22 14:12:00 51CTO

interviewer : Tell me about your common encryption algorithms ?

Encryption algorithms are usually divided into two types : Symmetric encryption and Asymmetric encryption . among , Symmetric encryption algorithm uses the same key for encryption and decryption ; Asymmetric encryption algorithm uses different keys for encryption and decryption , It is divided into public key and private key . Besides , There is also a class called Message digest algorithm , It is an irreversible algorithm for summarizing data .

This time we will learn about symmetric encryption algorithm .

Symmetric encryption algorithm

Symmetric encryption algorithm uses the same key for encryption and decryption , Or use two keys that can be simply calculated from each other . In most symmetric encryption algorithms , The encryption and decryption keys are the same .

It requires both parties to communicate securely before , Agree on a key . The security of symmetric algorithm depends on the key , Leaking the key means that anyone can decrypt the information they send , This is also one of the main disadvantages of symmetric encryption algorithm .

The common symmetric encryption algorithms are :DES Algorithm 、3DES Algorithm 、AES Algorithm .

DES Algorithm

DES Algorithm (Data Encryption Standard) Is a common block encryption algorithm .

interviewer : What is a block encryption algorithm ?

Block encryption algorithm is to divide plaintext into fixed length groups , Each group is encrypted with the same key and algorithm , Output is also a ciphertext of fixed length .

from IBM The company in 1972 Developed in ,1976 In, it was determined as the federal data processing standard by the National Standards Bureau of the federal government of the United States (FIPS), Then it spread widely in the world .

stay DES In the algorithm, , The fixed length of the key is 64 position . Clear text press 64 Bit to group , Ciphertext group is formed by permutation or exchange of the plaintext group and key bit by bit , Then assemble the ciphertext group into ciphertext .

Each eighth bit of the key is set to parity bit , That is the first. 8、16、24、32、40、48、56、64 position , Therefore, the length of the key actually participating in the encryption is 56 position .

We use it Java Write an example :

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;

public class DesUtil {

    private static final String DES = "DES";
    private static final Charset CHARSET = StandardCharsets.UTF_8;

    /** *  encryption  * * @param input  Plaintext  * @param key  secret key  * @return  Ciphertext  * @throws GeneralSecurityException */
    public static String encrypt(String input, String key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(DES);
        SecretKey keySpec = new SecretKeySpec(key.getBytes(CHARSET), DES);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] data = cipher.doFinal(input.getBytes(CHARSET));
        return Base64.getEncoder().encodeToString(data);
    }

    /** *  Decrypt  * * @param input  Ciphertext  * @param key  secret key  * @return  Plaintext  * @throws GeneralSecurityException */
    public static String decrypt(String input, String key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(DES);
        SecretKey keySpec = new SecretKeySpec(key.getBytes(CHARSET), DES);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] data = cipher.doFinal(Base64.getDecoder().decode(input));
        return new String(data, CHARSET);
    }

    public static void main(String[] args) throws GeneralSecurityException {
        String msg = " I like you , Can you be my girl friend? ?";
        String key = "One-More";
        System.out.println(" Before encryption :" + msg);
        String pwd = DesUtil.encrypt(msg, key);
        System.out.println(" After encryption :" + pwd);
        System.out.println(" After decryption :" + DesUtil.decrypt(pwd, key));
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

The operation results are as follows :

 Before encryption : I like you , Can you be my girl friend? ?

     
  • 1.
 After encryption :xxxxxx

     
  • 1.
 After decryption : I like you , Can you be my girl friend? ?

     
  • 1.

DES Now it's not a secure encryption method , Mainly because the key it uses is too short , Easily broken by violence .

3DES Algorithm

3DES Algorithm (Triple Data Encryption Algorithm) yes DES An upgraded version of the algorithm , It is equivalent to three times of plaintext DES encryption .

Because of the enhancement of computer operation ,DES Because the key length of the algorithm is too low, it is easy to be broken by violence ;3DES The algorithm provides a relatively simple method , By adding DES To avoid similar attacks , Instead of designing a new block cipher algorithm .

stay DES In the algorithm, , The fixed length of the key is 192 position . When encrypting and decrypting , The key will be divided into 3 individual 64 A key .

interviewer :3DES What is the process of algorithm encryption and decryption ?

The encryption process is as follows :

  1. Use the first key to encrypt plaintext .
  2. Decrypt the result of the previous step with the second key .
  3. Use the third key to encrypt the result of the previous step .

The decryption process is as follows :

  1. Decrypt plaintext using a third key .
  2. Use the second key to encrypt the result of the previous step .
  3. Decrypt the result of the previous step with the first key .

We use it Java Write an example :

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;

public class TripleDesUtil {

    private static final String DESede = "DESede";
    private static final Charset CHARSET = StandardCharsets.UTF_8;

    /** *  encryption  * * @param input  Plaintext  * @param key  secret key  * @return  Ciphertext  * @throws GeneralSecurityException */
    public static String encrypt(String input, String key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(DESede);
        SecretKey keySpec = new SecretKeySpec(key.getBytes(CHARSET), DESede);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] data = cipher.doFinal(input.getBytes(CHARSET));
        return Base64.getEncoder().encodeToString(data);
    }

    /** *  Decrypt  * * @param input  Ciphertext  * @param key  secret key  * @return  Plaintext  * @throws GeneralSecurityException */
    public static String decrypt(String input, String key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(DESede);
        SecretKey keySpec = new SecretKeySpec(key.getBytes(CHARSET), DESede);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] data = cipher.doFinal(Base64.getDecoder().decode(input));
        return new String(data, CHARSET);
    }

    public static void main(String[] args) throws GeneralSecurityException {
        String msg = " I like you , Can you be my girl friend? ?";
        String key = "One-More12345678One.More";
        System.out.println(" Before encryption :" + msg);
        String pwd = TripleDesUtil.encrypt(msg, key);
        System.out.println(" After encryption :" + pwd);
        System.out.println(" After decryption :" + TripleDesUtil.decrypt(pwd, key));
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

The operation results are as follows :

 Before encryption : I like you , Can you be my girl friend? ?

     
  • 1.
 After encryption :xxxxxx

     
  • 1.
 After decryption : I like you , Can you be my girl friend? ?

     
  • 1.

although 3DES The security of the algorithm has been improved , But because of the use of 3 Time DES Algorithm , Encryption and decryption are slow .

AES Algorithm

AES(Advanced Encryption Standard, Advanced encryption standard ) Mainly to replace DES Encryption algorithm , Although it appears 3DES Encryption method of , But because its encryption time is DES Algorithm 3 More than double , The number of key bits still cannot meet the security requirements .

1997 year 1 month 2 Number , National institute of standards and technology (NIST) Announced the hope to solicit advanced encryption standards , To replace DES. Many cryptographers all over the world have submitted their own algorithms . Through the selection process , Advanced encryption standard was established by the National Institute of standards and technology in 2001 year 11 month 26 Published on FIPS PUB 197, And in 2002 year 5 month 26 Day becomes the effective standard .

This algorithm is a Belgian cryptographer Joan Daemen and Vincent Rijmen Designed by , Combine the names of the two authors , With Rijndael The selection process of Advanced Encryption Standard for name contribution .

interviewer :AES Is the key length of the algorithm fixed ?

AES The key length of the algorithm is fixed , The length of the key can be used 128 position 、192 Bit or 256 position .

AES The algorithm is also a block encryption algorithm , Its grouping length can only be 128 position . The grouped plaintext group and key use several different methods to perform permutation and replacement operations to form a ciphertext group , Then assemble the ciphertext group into ciphertext .

We use it Java Write an example :

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;

public class AesUtil {

    private static final String AES = "AES";
    private static final Charset CHARSET = StandardCharsets.UTF_8;

    /** *  encryption  * * @param input  Plaintext  * @param key  secret key  * @return  Ciphertext  * @throws GeneralSecurityException */
    public static String encrypt(String input, String key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(AES);
        SecretKey keySpec = new SecretKeySpec(key.getBytes(CHARSET), AES);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] data = cipher.doFinal(input.getBytes(CHARSET));
        return Base64.getEncoder().encodeToString(data);
    }

    /** *  Decrypt  * * @param input  Ciphertext  * @param key  secret key  * @return  Plaintext  * @throws GeneralSecurityException */
    public static String decrypt(String input, String key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(AES);
        SecretKey keySpec = new SecretKeySpec(key.getBytes(CHARSET), AES);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] data = cipher.doFinal(Base64.getDecoder().decode(input));
        return new String(data, CHARSET);
    }

    public static void main(String[] args) throws GeneralSecurityException {
        String msg = " I like you , Can you be my girl friend? ?";
        String key = "One-More12345678One.More87654321";
        System.out.println(" Before encryption :" + msg);
        String pwd = AesUtil.encrypt(msg, key);
        System.out.println(" After encryption :" + pwd);
        System.out.println(" After decryption :" + AesUtil.decrypt(pwd, key));
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

The operation results are as follows :

 Before encryption : I like you , Can you be my girl friend? ?

     
  • 1.
 After encryption :xxxxxx

     
  • 1.
 After decryption : I like you , Can you be my girl friend? ?

     
  • 1.

AES The algorithm is the most widely used symmetric encryption algorithm at present .

summary

Symmetric encryption algorithm uses the same key for encryption and decryption , The common symmetric encryption algorithms are :DES Algorithm 、3DES Algorithm 、AES Algorithm .
Due to low security 、 The efficiency of encryption and decryption is low ,DES Algorithm and 3DES Algorithms are not recommended ,AES The algorithm is the most widely used symmetric encryption algorithm at present .

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221232168266.html