The data is encrypted for security reasons in the project, and RSA is used to encrypt it.
Summarized as follows:
Features: Public key encryption private key decryption, private key signature public key authentication, public key is generated through the private key.
Advantages: Asymmetric encryption, high security;
Disadvantage: Encryption and decryption efficiency is low, the cost of performance is therefore not suitable for large amounts of data to encrypt.
Problem:
One: Length problem
RSA limits the encryption and decryption content according to the different private key lengths, generally, the content of the encryption long team = Private key long team/8, but there is also a problem here is the alignment also occupies a certain space, the general alignment is occupied by 11 bytes, so the final plaintext length =1024 ( Assuming a 1024 key length is used/8-11 = 117, the redaction length cannot exceed 117, and the decryption length is 128.
What if the length of the plaintext exceeds the limit? In general, we will solve this problem by means of piecewise encryption and decryption.
Here you can use this library Https://github.com/Blankj/AndroidUtilCode
Second: Decryption Note:
To correctly decrypt the content you need to pay attention to a few things:
1: The key is correct, the encryption and decryption key must be a pair.
2: If the use of segmented encryption, also must be segmented decryption.
3: Key encoding format, cipher code format, alignment is what.
If the key is Base64 encoded then the same way is used to decode the base64 before decrypting it.
Only the encryption and decryption are fully symmetric to get the data correctly.