AES does not encrypt the received plaintext at one time, but block encryption, that is, the plaintext is divided into blocks of equal length, each block size is 128bit, and then each small block is encrypted. Then the problem is coming. Not all original plaintext strings can be equally divided into 18bit. For example, if the original string size is 200bit, then the second block is only 72bit, and the second block needs to be filled to make the size of the second block reach 18bit. Common filling modes are
If there is no padding, the original encrypted string size must be an integer multiple of 128bit;
Assume that the block size is 8 bytes. If the block is missing 8 bytes of n bytes, the original block is filled with n until it is full of 8 bytes. Example: Block {1, 2,3} is five bytes short, so the completed result {1, 2,3,5,5,5} is followed by five 5, Block {1, 2,3,7.
What if it happens to be 8 bytes and PKCS5Padding is selected? Block {1, 2, 3...8} becomes {1, 2, 3...8,8...8}, and the original string is supplemented by eight eights. The reason for this is that for the convenience of decryption, the size of the original block can be calculated only by looking at the last bit.
Like PKCS5Padding, PKCS5Padding only fills 8 bytes, and PKCS7Padding can fill 1~256 bytes. The default filling method of aes in openssl is PKCS7Padding.
AES has many encryption modes, including ECB, CBC, CTR, OCF and CFB, and the most common modes are ECB and CBC.
The simplest encryption mode, each block is encrypted independently, and the encryption between blocks does not affect each other, so it can be parallel and efficient.
Although this encryption is simple, it is not secure. If the plaintext of two blocks is exactly the same, then the encrypted things are exactly the same.
Related functions of openssl:
A new concept, initial vector iv, is introduced in CBC mode. The function of iv is to prevent the same plaintext block from being encrypted into the same content. The principle is that the first plaintext block is XOR-encrypted with the initial vector, the second block is XOR-encrypted with the first ciphertext block, and so on to avoid the same block being encrypted into the same content.
Openssl related functions:
Knock on the blackboard! ! So when docking with a third party, if the other party says they use aes encryption, be sure to ask them three questions:
The function of signature is to let the receiver verify that the data you sent has not been tampered with; The function of encryption is to ensure that data is not stolen.
Principle: You have an original string A to check.
Step 1: select a hash _ a;; Lpriority to hash a gets hash _ a;;
Step 2: encrypt hash_a to obtain the encrypted value encrypt _ a;;
Step 3: Send the original string A and the encrypted encrypt_a to the third party, and the third party will check the signature. The third party decrypts encrypt_a to get a hash value hash_a 1, and then uses the same hash algorithm to hash the original string A to get the hash_a before encryption. If hash_a = hash_a 1, the verification is successful.
Rsa uses private key to encrypt information for signature and public key to decrypt it for verification.
Openssl related functions:
Note: In the two functions, m is the value after the original string has been hashed, and type indicates the algorithm for generating m. For example, NID_sha256 indicates that the original string has been hashed with sha256, and returning 1 indicates that the signature is successful or successful, and-1 indicates failure.
Knock on the blackboard again! ! So, if the third party says rsa authentication, let the other party tell them their own hash algorithm.
Firstly, it is clear that private key encryption is not equal to signature. When encrypting, use the public key to encrypt, and the third party uses your private key to decrypt.
In openssl, the public key encryption function is RSA_public_encrypt and the private key decryption function is RSA_private_decrypt. For details, you can check the official documents yourself.
Rsa also involves the filling method, and you should also ask clearly when docking.
When using public key encryption, you will find that the result of each encryption is different, while when using private key encryption, the result is the same every time. I searched the internet and said it was because of the filling method.
Description of official documents:
So why do you have to sign with a private key and encrypt with a public key instead of signing with a public key and encrypting with a private key?
Give a chestnut: