Open Gamecock opened 6 years ago
Good morning @Gamecock, before I give anymore thought to your exp/matissa question...
Why not just use the public key PEM? Why store the exp/mantissa manually? The PEM can be stored as a file on any file system and could go into any database as a string.
Second, the public key is intended to be public. It serves no purpose to encrypt and decrypt anything under that key. You want to encrypt with the public key and decrypt with the private key. Looks like maybe your script is doing this properly but the console logging "Now decrypt with public key" is incorrect?
Third, one way hashing algorithms are preferred for password storage: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
Lastly, I suspect that your issues related to the length of the encrypted data have something to do with RSA encryption padding. There are some options in forge related to this. https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Padding_schemes
The documentation shows how to use various padding schemes: https://github.com/digitalbazaar/forge#rsa
@mattcollier Thanks for the quick reply. Let me answer your questions.
1) Why not PEM format: Because the server I am connecting to provides key in this format.
2) When you do not want to send a private key it's customary to encrypt with a public key and decrypt with a private key.
3) Why not one way hash. They
// encrypt data with a public key (defaults to RSAES PKCS#1 v1.5)
var encrypted = publicKey.encrypt(bytes);
I'd be happy if it was just a logging issue. Do you see something there I could fix?
One tip I picked up from this article is that you should use echo -n 'password'
in your shell script, otherwise you're getting an extraneous newline: https://crypto.stackexchange.com/a/12623
I found this comment that indicate that openssl supports OAEP but defaults to PKCS#1 v1.5: (as of Feb 2016) https://crypto.stackexchange.com/questions/20279/openssl-rsa-same-plaintext-but-different-ciphertext#comment75886_20280
http://www-cs-students.stanford.edu/~tjw/jsbn/ This worked instead.
I assume this is operator error. I create a RSA key in open ssl
openssl genrsa -out mykey.pem 2048
, and then test with this script.It works as expected and outputs the mantissa and exponent.
Now I take that mantissa and exponent and put into a JSON object. In production I'll get them from the server in this format. Then I rebuild the key and encrypt:
build a key and encrypt "password", and print out the key in PEM format.
Output: