Wednesday, 13 March 2024

openssl RSA key generation, encrypt dedcrypt, base 64 encoded data

 https://travistidwell.com/jsencrypt/

genereatekey:

openssl genrsa -out rsa_1024_priv.pem 1024
  • This generates a private key, which you can see by doing the following...
cat rsa_1024_priv.pem #if you are on mac you can | pbcopy to copy to the clipboard
  • You can then copy and paste this in the Private Key section of the demo page.
  • Next, you can then get the public key by executing the following command.
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem


https://stackoverflow.com/questions/42300795/openssl-decrypting-with-a-private-key


For encryption:

openssl rsautl -encrypt -in /path/to/your/file -out /path/to/your/encrypted -pubin -inkey /path/to/your/public_key.pem

For decryption:

openssl rsautl -decrypt -in /path/to/your/encrypted -out /path/where/you/want/your/decrypted.txt -inkey /path/to/your/private_key.pem

Note: If you have this decryption error: RSA_EAY_PRIVATE_DECRYPT:data greater than mod len try this command before decrypt your file:

cat yourEncryptedFile| base64 --decode > yourEncryptedRawFile

https://stackoverflow.com/questions/23205592/openssl-data-greater-than-mod-len
Asymmetric RSA keys can encrypt/decrypt only data of limited length i.e. RSAES-PKCS1-v1_5 encryption scheme defined in RFC3447 can operate on messages of length up to k - 11 octets (k is the octet length of the RSA modulus) so if you are using 2048-bit RSA key then maximum length of the plain data to be encrypted is 245 bytes.


No comments:

Post a Comment