I made the following file: asn_format_mykey.txt



I run the following commands to get key.pem (private key) and pkey.pem (public key)

openssl asn1parse -genconf "asn_format_mykey.txt" -out "key.der"
openssl rsa -inform der -in "key.der" -outform pem > "key.pem"
openssl rsa -inform der -in "key.der" -outform pem -pubout>"pkey.pem"

Then, I try to encrypt a small message using -raw with openssl rsautl

echo 'aaaa'| openssl rsautl -encrypt -pubin -inkey "pkey.pem" -raw -out "message.encrypted"

and it returns:

RSA operation error 140063665198744:error:0406B07A:rsa
routines:RSA_padding_add_none:data too small for key

Any idea how to fix this?

  • 1
    This is a great question. You showed me a new way to generate a custom RSA Private Key (for my own academic purposes). Mar 30, 2020 at 9:20
  • your parameters (for the Private Key) look wrong. I guess you know that? e1=INTEGER:17477422 e2=INTEGER:17480382 coeff=INTEGER:305512012935204 ? It looks like a cut and paste error ( as the Private Expo was correctly set to 181909) Apr 29, 2020 at 12:29

1 Answer 1


Your modulus is unusually small and insecure for RSA, but I guess you know that!

The modulus in decimal is 305512047893009 and in hex is 0115DC91160DA11, which is a number that is exactly 7 bytes long.

You have passed the -raw flag, which means you are not using any standard padding scheme, meaning you are responsible for making sure the input message is properly formatted for RSA encryption. In practice, this means your input message must also be exactly 7 bytes long and smaller in value than the modulus, so you will need to create a binary file and use that as the input.

Using a hex editor, I created one with the bytes 01146161616161, saved that in a file named message.plain, then I used the same command as you, but with -in message.plain added, and it successfully encrypted the message:

openssl rsautl -encrypt -pubin -inkey "pkey.pem" -raw -in "message.plain" -out "message.encrypted"

Note: This is all ok as an exercise, but, for any real work, you should not invent your own padding scheme - use one of the existing ones.

  • thank you for the answer. Two things, I don't want to encrypt a binary and the other why the message has to be exactly 7 bytes? In rsa the message space are the numbers smaller than the modulus.
    – 111
    May 26, 2018 at 23:30
  • small privexpt (d) relative to n is also insecure, but that belongs on crypto.SX where it has already been answered May 27, 2018 at 3:13
  • this is an exercise.So my goal is to make it insecure
    – 111
    May 27, 2018 at 17:46
  • 1
    If you don't want to encrypt a binary then don't try to encrypt using the "-raw" flag! This is very low level stuff: using "-raw" implies that you will take responsibility for ensuring everything is formatted correctly and this may imply working with binary data. Using PKCS1 padding would avoid this, but you would need a larger modulus for that. May 29, 2018 at 8:52
  • 1
    The number you encrypt must always be less than the modulus. In the example in this question this means the first byte must be less than 11 (or equal to 11 with the second byte less than 5D, and so on). The exact number will depend on the specific modulus you are using - so it could be larger or smaller than that. If you're using the exact same modulus as here it shouldn't need to be 00, as long as its less that 11. Mar 30, 2020 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .