The interaction between openssl and java about rsa

Both versions work when self-encrypting and decrypting. However, there is an error in the interaction: openssl encryption and java decryption, the information is appended with garbled characters; java encryption openssl decryption, the information reality is abnormal.
After consulting a large amount of information, the problem cannot be determined. Finally, starting from the garbled code decrypted by java, it is found that the filling method is different.
Taking the RSA_NO_PADDING mode as an example, assuming rsa 1024bit, the last block of plaintext is A, and a difference of n bytes can be added
. The padding method of 128-byte openssl is to add 0 after A, and then n-1 garbled characters (may be meaningful). )
, but java adds n 0s before A,
so I solve the interaction between the two versions in this way: to prevent the difference between the two versions from filling, let's fill it manually. Example: Set to RSA_NO_PADDING
mode, add '\0' after the plaintext before encryption to make the length a multiple of 128, and remove all '\0' at the end after decryption.
Hope it can enlighten someone who encounters the same problem

Guess you like

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