RSA encryption and decryption asymmetric encryption

RSA encryption and decryption asymmetric encryption
Related introduction
RSA algorithm belongs to asymmetric encryption algorithm, asymmetric encryption algorithm requires two secret keys: public key (publickey) and private key (privatekey). Public key and private key are a pair ,
If the public key encrypts the data, only the corresponding private key can be used to decrypt;
if the private key encrypts the data, then only the corresponding public key can be
used to decrypt the data. Encryption and decryption use two different secret keys This algorithm is called asymmetric encryption algorithm/public key encryption, private key decryption, private key encryption and public key decryption. Points
to note
1. The result of RSA encryption or signature is unreadable binary, and it is often converted to BASE64 when used.
Retransmit the code 2. When RSA encryption, there is a limit to the size of the data to be encrypted, and the maximum is not greater than the length of the key. For example, when a 1024 bit key is used (the key can be generated by Baidu), the maximum data size of 1024/8=128 Bytes can be encrypted. When the data is larger than 128 Bytes, the data needs to be encrypted in blocks (if the data exceeds the limit, the encryption and decryption will fail, and the openssl function will return false), and the encrypted string after the block encryption will be spliced ​​into a string and sent to the client.
In order to ensure that the result of each encryption is different, RSA encryption will splice a random string after the data to be encrypted, and then encrypt it. Different padding methods Padding represent different lengths of this string. After the over-limit data is grouped, random strings will be filled with the length specified by this Padding. For example, if the padding method uses the default OPENSSL_PKCS1_PADDING (it takes up 11 bytes for padding), then the length of the plaintext can only be at most 128-11=117 Bytes.
The receiver also needs to group when decrypting. Divide the encrypted original binary data (for BASE64 data, it needs to be decoded), divide each 128 Bytes into a group, and then decrypt it. After decryption, the random string is discarded according to the length of Padding, and the obtained original string is spliced ​​together to obtain the original message.
3. There is a difference between the default padding and no padding of the openssl_public_encrypt function php. If it is only php and php docking, you do not need to pay attention to this issue. If it is php and c or java, you need to choose no padding and then add the padding yourself
4. You need to add php Open or install the openssl module (win open, linux installation)
parameters send Body/form-data
Web: https://www.cnblogs.com/makalochen/p/10845033.html

AES encryption and decryption symmetric encryption

public function encrypt($data)
{
    
    
    return openssl_encrypt($data, 'AES-128-ECB', 'BankShopAesKeyMini', 0, '');
}
public function decrypt($data)
{
    
    
    return openssl_decrypt($data, 'AES-128-ECB', 'BankShopAesKeyMini', 0, '');
}

Guess you like

Origin blog.csdn.net/weixin_43784997/article/details/115235468