Android uses RSA encryption and decryption sample code

1、 Public key encryption and private key decryption

1. Data is the data to be encrypted. If it is a string, then GetBytes. Publickey is the public key and privatekey is the private key. Custom key pair test

2. Read the public key from the file

2、 Public key segmented encryption and private key segmented decryption

When the encrypted data is too long, an exception of javax.crypto.illegalblocksizeexception: data must not be longer than 117 bytes will appear. RSA algorithm stipulates that the data encrypted at one time cannot exceed the keylength / 8-11 when generating the key pair. The keylength is generally 1024 bytes, and the encrypted data cannot exceed 117 bytes

Test segment encryption and decryption

3、 Generate key pair

@H_ 301_ 52@

After generating the public key and private key, it is encoded with Base64

other

1、 Android encrypted data cannot be decrypted on the server?

The RSA encryption method of Android is RSA / ECB / nopadding, while the standard JDK is RSA / ECB / pkcs1padding, so the encryption method of the standard JDK should be set during encryption

2、 Base64 encoding. Because different devices have different processing methods for characters, characters may be processed incorrectly, which is not conducive to transmission. Therefore, first encode the data into visible characters by Base64 to reduce errors. The official Base64 class, base64.encode encoding and base64.decode decoding. There will be a line break with this, which needs to be customized

3、 RSA is an asymmetric encryption algorithm. Depending on large number calculation, the encryption speed is slower than DES, and is usually only used to encrypt a small amount of data or keys

4、 Public key encryption is slower than private key encryption block, and public key decryption is slower than private key decryption. The encrypted data is about 1.5 times that before encryption

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>