A string encrypted in Ruby gives: "BadPaddingException" when decrypted in Java

I am using the following Ruby code to encrypt a string

require 'openssl'
require 'base64'

public_key = OpenSSL::PKey::RSA.new(File.read('public_key'))

Base64.encode64(public_key.public_encrypt('Some random string that I want to encrypt.'))

I need to pass the ciphertext in the API request, and at the end of the API I get the following error (after reading the API logs):

javax.crypto.BadPaddingException: Decryption error

Now I am faced with numerous questions in Stackoverflow, but given my Ruby code, I am mistaken that the ciphertext cannot be decrypted at the end of the API. Obviously, Java is used at the end of the API.

I am completely zero when it comes to encryption, but through encryption and decryption in Ruby I made sure that the implementation is correct in Ruby. The problem occurs when text encrypted through the Ruby language is decrypted through Java at the end of the API.

+4
source share
3

Java Ruby, . , , ruby ​​openssl Java. Java , , :

, , , Java. , , , BadPadding.

, base64 , Java. , . base64 , . BadPaddingException. , Java Base64URL Base64? Java base64url, , . , , base64, , base64url encoder ( , ).

+3

openJdk, BadPaddingException " (un) ". , : , , , - , . , , , - - .

+2

'PKCS1 padding 11 .

, 11

For 1024-bit RSA keys, the encryption size is usually 117 and the decryption buffer size is 128.

+1
source

Source: https://habr.com/ru/post/1688911/


All Articles