Those things about encryption and decryption, signature verification

Do you have many question marks in the face of terms such as MD5, SHA, DES, AES, RSA, etc.? What are these nouns? There are also public key encryption, private key decryption, private key signature, and public key signature verification. What the hell are these? Maybe you haven't heard of these terms in your daily work, but once you want to design an interface for external access, or a system with high security requirements, you will inevitably come into contact with these terms. Therefore, encryption, decryption, and signature verification are concepts that a qualified programmer must master. So is the cryptography related to encryption and decryption really far away from us? In fact, there are many common scenarios in life that actually use the relevant knowledge of cryptography. Let's not think about it too hard. For example, in the passage of "The Brother Sleeping in My Upper Bunk" cheating on the tongue twister, Xiaoyisan represents A. , Xiao Chi Lao represents B, oh my mother represents C, and daddy is D. This tongue twister is actually a kind of cryptography. Interested friends can take a look at this snippet of tongue twister . So in fact, cryptography is closely related to our lives. Next, we will thoroughly understand these concepts in one article.

A battlefield without gunpowder smoke -- a brief introduction to cryptography

A hut may be built without foundations, but never a solid mansion.

The word password has many interpretations. In modern society, if you don’t touch programming, it is generally considered to be the login password we set, or the number we enter when we go to the bank to withdraw money. We store a set of numbers for the party providing the service when we register, and use this set of numbers to prove our identity when we log in later. This number is usually called a password.

What we need to understand is not the password mentioned above, but a kind of "cryptography", which is to convert the information to be transmitted according to certain rules, thereby hiding the content of the information. This method allows confidential information to be passed on through open channels without leakage. Using this method, go through the encryption process. In the encryption process we need to know the following concepts:

  • Original text: Or plaintext, which is the hidden text
  • Encryption method: refers to the law of hiding the original text
  • Ciphertext: or pseudo-text, refers to the text that can be publicly transmitted after processing the original text according to the encryption method.
  • Key: The decisive factor in encryption, it may be numbers, words, or some letters, or a combination of these things

The result of encryption generates ciphertext. If the recipient can read the ciphertext, then the encryption method and key must be told to the recipient. Otherwise, the recipient cannot decrypt the ciphertext and cannot read the original text.

From a historical point of view, cryptography can be roughly divided into two stages: classical cryptography and modern cryptography. The two take the birth of modern information technology as the dividing point, and the cryptography discussed now mostly refers to the latter, which is based on the results of information theory and mathematics.

classical cryptography

Classical cryptography originated thousands of years ago, as early as in ancient Egypt around 1900 BC, with the use of special characters and simple substitution ciphers to protect information. A clay tablet dated to around 1500 BC has been unearthed in the Mesopotamian plain, which records the enciphered recipe for the glazing of ceramics. The ancient Greek period (800 BC - 146 BC) also invented "steganography" to hide information by physical means, such as using milk to write, covering words with wax, etc. Later, in ancient Rome, the Caesar cipher based on substitution encryption appeared, and it is said that Caesar used this method to communicate with his subordinates and was named. Most of these methods use simple mechanical tools to protect secrets, which are undoubtedly very crude and easy to guess today. Strictly speaking, it may be difficult to call it cryptography.

The Caesar cipher is that when the offset is 3, all letters A will be replaced with D, B will become E, and so on.

Caesar Cipher

modern cryptography

The study of modern cryptography came from the need to protect and guess military communications during the First and Second World Wars. In December 1901, Italian engineer Guglielmo Marconi successfully completed the experiment of transatlantic radio communication, which caused a sensation around the world and promoted the arrival of the era of radio communication. Radio greatly improved the ability of long-distance communication, but it had a natural flaw - it was difficult to limit the recipient, which meant that the information you sent could be intercepted, thus giving birth to the development of encryption technology.

Encryption and decryption of radio information also directly contributed to the emergence of modern cryptography and computer technology. In turn, these technological advances have also influenced the development of the times. The telegram (January 16, 1917) that German Foreign Minister Arthur Zimmermann wooed Mexico to form an anti-American military alliance during the First World War was deciphered by the British intelligence agency - Office 40, which directly led to the United States' entry into the war. ; The Enigma cipher machine (the most advanced encryption equipment at the time) used by Germany during World War II was successfully deciphered by the Allies (1939 to 1941), resulting in the defeat of Germany in the Atlantic Battle. It is said that during World War II, the number of people engaged in cryptography research in Britain alone reached 7,000, and their results brought the end of World War II at least one to two years earlier.

What follows is an event that can be called a milestone in the history of cryptography. September 1, 1945, Claude Elwood Shannon completed the landmark internal report "A Mathematical Theory of Cryptography", October 1949, the report Officially published in Bell System Technical Journal with the title of "Communication Theory of Secrecy Systems". This paper links cryptography and information theory together for the first time, providing a mathematical foundation for symmetric cryptography . This also marks the official establishment of modern cryptography. This is also the first milestone event in the history of cryptography.

The second milestone in the history of cryptography is the emergence of DES. The full name of DES is Data Encryption Standard, which is a data encryption standard. It is a block cipher algorithm that uses key encryption. It was used in communication, and then the algorithm was widely spread internationally.

The third milestone event in the history of cryptography is the emergence of public key cryptography, which is widely used in our blockchain, that is asymmetric cryptographic algorithms . In November 1976, Whitfield Diffie and Martin E. Hellman published the paper "New Directions in Cryptography" in IEEE Transactions on Information Theory, discussing the issue of secure communication and signature authentication systems without the need to transmit keys , officially initiated the research on modern public key cryptography system. Before the discovery of public key cryptography, if confidential communication is required, both parties in the communication must negotiate in advance on the encryption and decryption algorithms and the keys to be used, including sending chicken feather letters, which are actually transmitting the keys. But since the advent of public key cryptography, the two parties who need to communicate secretly no longer need to perform key negotiation in advance. Public key cryptography is not confidential in theory, but it is in practice. That is to say, the public key password can be guessed, but it takes a very long time. After guessing it, there is no need to keep the secret secret.

Above we talked about things related to modern cryptography. Basically, there are two commonly used ones, one is symmetric encryption algorithm and the other is asymmetric encryption algorithm . Then we will introduce these two concepts as the main line to introduce the concepts we mentioned in the opening title.

Program implementation

Symmetric encryption algorithm

Symmetric encryption means that the same key is used for encryption and decryption, so it is called symmetric encryption. Symmetric encryption has only one key, which is the private key. The specific algorithms are: DES, 3DES, TDEA, Blowfish, RC5, IDEA. But our common ones are: DES, AES and so on.

So what are the advantages of symmetric encryption? The algorithm is open, the calculation amount is small, the encryption speed is fast, and the encryption efficiency is high. The disadvantage is that the management and distribution of keys is very difficult and not secure enough. Before the data is transmitted, the sender and the receiver must agree on the secret key, and then both parties must save the secret key. If one party's secret key is leaked, the encrypted information will be insecure. In addition, every time each pair of users uses a symmetric encryption algorithm, they need to use a unique key that others do not know, which will cause a huge number of keys owned by both the sender and receiver, and key management will also become a burden on both parties.

The encryption process can be understood as follows:

  • Encryption: original text + secret key = cipher text
  • Decryption: ciphertext - secret key = original text

You can see that both processes use the same key. It is simply represented by the diagram as follows:

Practical drill

Now that we know the relevant knowledge about symmetric encryption algorithms, how do we use Java to implement the encryption and decryption actions of symmetric encryption? Common symmetric encryption algorithms are: DES, AES, etc.

FROM

The DES encryption algorithm is a block cipher that encrypts data in 64-bit groups, its key length is 56 bits, and the same algorithm is used for encryption and decryption. The DES encryption algorithm is to keep the key secret, while the public algorithm includes encryption and decryption algorithms. In this way, only the person who has the same key as the sender can decipher the ciphertext data encrypted by the DES encryption algorithm. Therefore, deciphering the DES encryption algorithm is actually the encoding of the search key. For a key with a length of 56 bits, if an exhaustive method is used to search, the number of operations is 2 to the 56th power.

Next, implement DES encryption with Java

 private final static String DES = "DES";

    public static void main(String[] args) throws Exception {
        String data = "123 456";
        String key = "wang!@#$";
        System.err.println(encrypt(data, key));
        System.err.println(decrypt(encrypt(data, key), key));

    }

    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,key.getBytes());
        return new String(bt);
    }

    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }


    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

After output, you can see that the data is encrypted

5fiw/XhRJ0E=
123 456

Encryption with DES in Java has a special place

  1. The length of the key set must be greater than or equal to 8
  2. If the length of the key set is greater than 8, only the first 8 bytes will be taken as the key

Why, we can see that DESKeySpecthere is the following paragraph when the class is initialized, where var1 is the secret key we passed. You can see that he made an interception. Only the first eight bytes are truncated.

public DESKeySpec(byte[] var1, int var2) throws InvalidKeyException {
    if (var1.length - var2 < 8) {
        throw new InvalidKeyException("Wrong key size");
    } else {
        this.key = new byte[8];
        System.arraycopy(var1, var2, this.key, 0, 8);
    }
}

AES

The AES encryption algorithm is an advanced encryption standard in cryptography. The encryption algorithm adopts a symmetric block cipher system. The minimum supported key length is 128, 192, and 256, and the block length is 128 bits. The algorithm should be easy to implement in various hardware and software. This encryption algorithm is the block encryption standard adopted by the US federal government. The AES standard is used to replace the original DES. It has been analyzed by many parties and is widely used all over the world.

JCE, Java Cryptography Extension, in the early JDK version, due to the restrictions on the export of passwords in the United States, the API involving encryption and decryption functions in Java was restricted from exporting, so the security components in Java were divided into two parts: JCA without encryption function (Java Cryptography Architecture) and JCE (Java Cryptography Extension) with encryption function.

The APIs of JCE are all under the javax.crypto package. The core functions include: encryption and decryption, key generation (symmetric), MAC generation, and key negotiation.

The encryption and decryption functions are provided by the Cipher component, which is also the core component in JCE.

Cipher There are a few things to keep in mind when setting up classes:

  1. When using Cipher, you need to specify the transformation as a parameter
  2. The format of transformation is algorithm/mode/padding, where algorithm is a mandatory item, such as: AES/DES/CBC/PKCS5Padding, see the following table for details
  3. The default mode is ECB, and the default padding is PKCS5Padding
  4. When the block algorithm is combined with the stream encryption mode, the number of bits processed each time needs to be specified after the mode, such as DES/CFB8/NoPadding. If not specified, the default value is used. The default value of SunJCE is 64bits
  5. Cipher has 4 operation modes: ENCRYPT_MODE (encryption), DECRYPT_MODE (decryption), WRAP_MODE (export key), UNWRAP_MODE (import key), a certain operation mode needs to be specified during initialization
Algorithm/Mode/Padding 16 bytes encrypted data length Less than 16 bytes encrypted length
AES/CBC/NoPadding 16 not support
AES/CBC/PKCS5Padding 32 16
AES/CBC/ISO10126Padding 32 16
AES/CFB/NoPadding 16 original data length
AES/CFB/PKCS5Padding 32 16
AES/CFB/ISO10126Padding 32 16
AES/ECB/NoPadding 16 not support
AES/ECB/PKCS5Padding 32 16
AES/ECB/ISO10126Padding 32 16
AES/OFB/NoPadding 16 original data length
AES/OFB/PKCS5Padding 32 16
AES/OFB/ISO10126Padding 32 16
AES/PCBC/NoPadding 16 not support
AES/PCBC/PKCS5Padding 32 16
AES/PCBC/ISO10126Padding 32 16

The secret key can be defined by ourselves, or it can be generated by AES itself. When it is necessary to define it by ourselves, we should pay attention:

  1. According to the AES specification, it can be 16 bytes, 24 bytes and 32 bytes long, corresponding to 128 bits, 192 bits and 256 bits respectively;
  2. In order to facilitate transmission, the encrypted data is generally base64 encoded:
    public static void main(String[] args) throws Exception {
        /*
         * 此处使用AES-128-ECB加密模式,key需要为16位。
         */
        String cKey = "1234567890123456";
        // 需要加密的字串
        String cSrc = "buxuewushu";
        System.out.println(cSrc);
        // 加密
        String enString = Encrypt(cSrc, cKey);
        System.out.println("加密后的字串是:" + enString);

        // 解密
        String DeString = Decrypt(enString, cKey);
        System.out.println("解密后的字串是:" + DeString);
    }

    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

Asymmetric encryption algorithm

In asymmetric encryption algorithm, the same key is not used for encryption and decryption, so it is called asymmetric encryption algorithm. In an asymmetric encryption algorithm, each user has two keys, a public key and a private key. The public key is released to the outside world, everyone can see the public key of everyone, and the private key is kept by themselves. Everyone only knows their own private key and not others. And it is precisely in the asymmetric encryption algorithm that there are concepts of encryption and decryption, signing and verification. Next we explain what these concepts mean.

Encrypt and decrypt

After encryption with the user's public key, only the user's private key can be decrypted. In this case, the public key is used to encrypt the information, ensuring that only a specific person (whose public key is used) can decrypt the information. So this we call encryption and decryption .

Let me take Bank A and Xiao Ming as examples. It is assumed that an asymmetric encryption algorithm is used between the two to ensure the security of information transmission (the meaning of the information and the tampering of the information are not known by the third party). The general process is as follows: First, Xiao Ming sent a message to Bank A "I want to deposit 500 yuan". This piece of information will be encrypted by Xiao Ming according to the public key released by Bank A. After encryption, it will become "XXXXXXX" and sent to Bank A. It was intercepted by a third party in the middle. Since there is no private key of Bank A, it cannot be decrypted, and the meaning of the information cannot be known, nor can it be tampered with in the correct way. So there is no way to get this encrypted information. Finally accepted by Bank A, Bank A decrypts the message with its own private key, decrypts it successfully, reads the content, and executes the operation. Then I learned that the message was sent by Xiao Ming, so I went to get Xiao Ming's public key, encrypted the information "operation succeeded (or failed)" with Xiao Ming's public key, and sent it to Xiao Ming. In the same way, Xiaoming finally unlocked it with his private key and learned the content of the information sent by Zhihu. Others intercepted it because they didn't have Xiao Ming's private key, so it was useless.

Signature and verification

There is also a second case where the public key is used to decrypt the information, making sure to let others know that the information was really published by me and that it is complete and correct. The recipient thus knows that the message really came from someone who has the private key. This is called a digital signature, and the form of the public key is a digital certificate. So this is what we call signature and signature verification .

Continue to take Xiao Ming and Bank A as examples. Bank A has released a patch for the bank client for all users to update. In order to ensure that people download the correct and complete client, Bank A will stamp the program with a digital signature (that is, use Bank A's private key to encrypt the program and then release), you need to install the digital certificate of Bank A (that is, the public key issued by the bank) in your computer, and then download the program, the digital certificate will decrypt the digital signature of the program, the decryption is successful, and the patch can be used . At the same time you can know that this patch is indeed from this bank A, it was released by him and not by anyone else.

Practical drill

The asymmetric encryption algorithm we often use in the development process is the RSA algorithm . Next we use Java to implement the RSA algorithm.

generate key

The first is the part of generating the key. There are many ways to generate the key. Here I introduce three kinds.

  1. Command line: Openssl can be used to generate public and private keys

    	-- 生成公钥和私钥
    	openssl genrsa -out key.pem 1024
    		    -out 指定生成文件,此文件包含公钥和私钥两部分,所以即可以加密,也可以解密
    		    1024 生成密钥的长度
    
    
  2. Use website: The website where the key was generated

  3. Use code: You can specify the length of the generated key, the minimum is 512

        public static KeyPair buildKeyPair() throws NoSuchAlgorithmException {
        final int keySize = 2048;
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);
        keyPairGenerator.initialize(keySize);
        return keyPairGenerator.genKeyPair();
    }
    
    
encryption

With the key, you can perform the encryption operation. Next, we will introduce the encryption operation of RSA. It is very simple as long as you pass in the public key and the data to be encrypted.

--- 加密
public static byte[] encrypt(PublicKey publicKey, String message) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        return cipher.doFinal(message.getBytes(UTF8));
    }
decrypt
--- 解密
public static byte[] decrypt(PrivateKey privateKey, byte [] encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return cipher.doFinal(encrypted);
}
add signature
/**
 * 使用RSA签名
 */
private static String signWithRSA(String content, PrivateKey privateKey) throws Exception {
    Signature signature = Signature.getInstance("SHA1WithRSA");
    signature.initSign(privateKey);
    signature.update(content.getBytes("utf-8"));
    byte[] signed = signature.sign();
    return base64Encode(signed);
}

signature verification
/**
 * 使用RSA验签
 */
private static boolean checkSignWithRSA(String content, PublicKey publicKey,String sign) throws Exception {
    Signature signature = Signature.getInstance("SHA1WithRSA");
    signature.initVerify(publicKey);
    signature.update(content.getBytes("utf-8"));
    return signature.verify(base64Decode(sign));
}

When signing and verifying, you need to pass in a digital signature standard. What we fill here is SHA1WithRSA that it means to use the SHA algorithm to sign and use the RSA algorithm to encrypt.

Algorithm description: After performing the digest calculation for the SHA1 algorithm, it is required to process the calculated digest instead of directly encrypting the RSA algorithm. The data of the SHA1 digest is required to be compressed to 20 bytes. Insert 15 bytes of identification data at the front. So the structure is as follows

30(数据类型结构)21(总长度)30(数据类型)09(长度)06 05 2B 0E 03 02 1A 0500【数据具体类型不清楚-请专家指正】 04 (数据类型) 14 (长度) + SHA1签名数据

Finally, RSA encryption is performed. So what we filled in XXXWithRSA, this XXX represents what digest algorithm is used to sign, as for what the digest algorithm is, there will be a detailed description later.

call the experiment

public static void main(String[] args) throws Exception {
    KeyPair keyPair = buildKeyPair();

    byte[] encryptData = encrypt(keyPair.getPublic(), "不学无数");

    System.out.println(String.format("加密后的数据:%s",base64Encode(encryptData)));

    System.out.println(String.format("解密后的数据:%s",new String(decrypt(keyPair.getPrivate(),encryptData),UTF8)));

    String context = "加签的字符串";

    String sign = signWithRSA(context, keyPair.getPrivate());

    System.out.println(String.format("生成的签名:%s",sign));

    Boolean checkSignWithRSA = checkSignWithRSA(context, keyPair.getPublic(), sign);

    System.out.println(String.format("校验的结果:%s",checkSignWithRSA.toString()));
}

The output is

加密后的数据:Bi8b4eqEp+rNRhDaij8vVlNwKuICbPJfFmyzmEXKuAgEgzMPb8hAmYiGN+rbUKWeZYJKJd0fiOXv
6YrYqd7fdast/m443qQreRLxdQFScwvCvj9g1YnPzbU2Q/jIwqAPopTyPHNNngBmFki+R/6V4DYt
HA5gniaUMYzynHdD+/W+x8ZYmwiuuS63+7wXqL36aLKe0H50wELOpSn45Gvni8u+5zPIoHV7PBiz
trCnQvne5LxFKDprrS3td1/76qyupFd+Ul3hsd+gjbAyN2MlXcAFMrGVaRkopWwc9hP1BsPvS52q
/8jOVdbeyU9BziVhViz1V0TtGW8bfbEnIStc3Q==
解密后的数据:不学无数
生成的签名:wvUXtr2UI0tUXmyMTTUBft8oc1dhvtXSBrFFetI5ZoxMm91TbXRWD31Pgqkg72ADxx9TEOAM3Bm1
kyzfBCZZpoq6Y9SM4+jdJ4sMTVtw0wACPglnPDAGs8sG7nnLhXWNQ1Y4pl4ziY6uLxF1TzQLFTxu
NAS7nyljbG69wrb9R3Sv5t8r1I54rYCVGSVFmTrGf+dSCjxABZv6mH8nygVif7zN1vU1+nSDKcON
Vtrpv0xCQHVBqnHPA6OiDm5GzBQxjD5aQt8mfgv8JJrB52TEa4JPYoC5Zw4JHlL++OvPwMpJgnuG
yg5vnWhxE2ncTzM+/pZ+CnXF2Dqv/JMQOfX6tA==
校验的结果:true

Digest Algorithm

Data digest algorithm is a very important branch of cryptography algorithm. It extracts fingerprint information from all data to realize data signature, data integrity check and other functions. Due to its irreversibility, it is sometimes used to encrypt sensitive information. . Data digest algorithms are also known as hash algorithms or hash algorithms.

The main feature of the message digest algorithm is that the encryption process does not require a key, and the encrypted data cannot be decrypted. Only by inputting the same plaintext data and going through the same message digest algorithm can the same ciphertext be obtained. (The digest can be compared to a fingerprint, and the message digest algorithm is the only position to get the file)

Features

No matter how long the input message is, the length of the calculated message digest is always fixed. In general, as long as the input messages are different, the digested messages produced after digesting them must also be different; but the same input must produce the same output. Only forward information digests can be performed, but no messages can be recovered from the digests, or even no information related to the original information can be found at all (irreversibility).

A good digest algorithm, where no one can find "collisions" or is extremely difficult to find, although "collisions" definitely exist (collisions are different content producing the same digest).

application

Generally, a digest of a message is referred to as the message's fingerprint or digital signature. Digital signatures are a method of guaranteeing the integrity and non-repudiation of information. The integrity of the data means that the message received by the sink must be the information sent by the source, and there is no change in the middle; the non-repudiation of the information means that the source cannot deny the information that has been sent. In fact, the digital signature can also realize the identity identification (authentication) of the source, that is, to determine whether the "source" is the intended communication partner of the source. The digital signature should be unique, that is, the signatures of different messages are different; at the same time, it should be unforgeable, that is, it is impossible to find another message whose signature is the same as the signature of the existing message; it should also be irreversible. , that is, any information about the signed message cannot be recovered from the signature. These characteristics are exactly the characteristics of the message digest algorithm, so the message digest algorithm is suitable as a digital signature algorithm .

What specific message digest algorithms are there?
  • CRC8, CRC16, CRC32: The CRC (Cyclic Redundancy Check, Cyclic Redundancy Check) algorithm has been around for a long time and is widely used, especially in the field of communications. The CRC32 algorithm is the most widely used now, which generates a 4-byte (32 digit) check value, which is generally an 8-digit hexadecimal number, such as FA 12 CD 45, etc. The advantages of the CRC algorithm are its simplicity and speed. Strictly speaking, CRC should be called a data verification algorithm, but its function is similar to the data digest algorithm, so it is also an optional algorithm for testing.
  • MD2, MD4, MD5: This is a very widely used algorithm family, especially MD5 (Message-Digest Algorithm 5, message digest algorithm version 5), which is developed from MD2, MD3, MD4, by Ron Rivest (RSA Company) ) was proposed in 1992 and is currently widely used in data integrity checking, data (message) digests, data encryption, etc. MD2, MD4, and MD5 all generate 16-byte (128-bit) check values, which are generally represented by 32-bit hexadecimal numbers. MD2's algorithm is slower but relatively secure, MD4 is fast but less secure, and MD5 is more secure and faster than MD4.
  • SHA1, SHA256, SHA384, SHA512: SHA (Secure Hash Algorithm) is formulated by the National Institute of Standards and Technology (NIST), a standard organization that specializes in formulating cryptographic algorithms in the United States. The digest lengths of SHA series algorithms are: SHA is 20 bytes (160 bits), SHA256 is 32 bytes (256 bits), SHA384 is 48 bytes (384 bits), and SHA512 is 64 bytes (512 bits), because the length of the data digest it produces is longer and therefore more It is more difficult to collide and therefore safer, and it is the development direction of future data summarization algorithms. Due to the long data digest length of the SHA series algorithm, its operation speed is relatively slow compared with MD5.
  • RIPEMD, PANAMA, TIGER, ADLER32, etc.: RIPEMD was proposed by Hans Dobbertin and others in 1996 based on the analysis of MD4 and MD5 defects. There are 4 standards 128, 160, 256 and 320, and their corresponding output lengths are respectively 16 bytes, 20 bytes, 32 bytes and 40 bytes. TIGER was proposed by Ross in 1995. Tiger claims to be the fastest Hash algorithm, optimized for 64-bit machines.
Practical drill

When using the digest algorithm alone, we usually use the MD5 algorithm, so here we will separately explain the implementation of the MD5 algorithm using Java.


public static String getMD5Str(String str) throws Exception {
    try {
        // 生成一个MD5加密计算摘要
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 计算md5函数
        md.update(str.getBytes());
        // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
        // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
        return new BigInteger(1, md .digest()).toString(16);
    } catch (Exception e) {
        throw new Exception("MD5加密出现错误,"+e.toString());
    }
}

Code addresses involved in the article

refer to

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324042056&siteId=291194637