2

I made the following file: asn_format_mykey.txt

asn1=SEQUENCE:rsa_key

[rsa_key]
version=INTEGER:0
modulus=INTEGER:305512047893009
pubExp=INTEGER:78221649299689
privExp=INTEGER:181909
p=INTEGER:17477423
q=INTEGER:17480383
e1=INTEGER:181909
e2=INTEGER:181909
coeff=INTEGER:1611938 

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
size:rsa_none.c:74:

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). – rustyMagnet Mar 30 '20 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) – rustyMagnet Apr 29 '20 at 12:29
5

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 '18 at 23:30
  • small privexpt (d) relative to n is also insecure, but that belongs on crypto.SX where it has already been answered – dave_thompson_085 May 27 '18 at 3:13
  • this is an exercise.So my goal is to make it insecure – 111 May 27 '18 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. – Matt Caswell May 29 '18 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. – Matt Caswell Mar 30 '20 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.