JSEncrypt decrypt on back-end (Bash)

205 views Asked by At

It looks like JSEncrypt adds some random padding to the RSA-encrypted string, and thus I cannot decrypt it with OpenSSL.

My JS code looks like this:

const rsaEncrypt = new window.JSEncrypt();
rsaEncrypt.setKey(
`-----BEGIN PUBLIC KEY-----
MIICITANBgkqhkiG9w0BAQEFAAOCAg4AMIICCQKCAgBNuSrpc+p9fd9I3qSa6+nC
HeNHfagAlCfJisM3eglbNN1yvY8U2DqtrYdPhukxSefo4iFMj65ngt+ZXLaLp6po
4VsGguQtqmQH9HC3I5FQk5YQTdtaLr8cQM8rHta0OEmJgGWfG2YndaFnxshuFgtW
AD9cdXPCh/RmGlhDdmR6xpBeMbvco6YOqyNGfxBRJqiZeINcVWmoEzHmqTlt/L7d
AcBHQA4erxsrfwie9POwgJptYYpwn1mamQ/a2/oDRnttaRayASlN2GRUkUeeI+hZ
zNe4eCrizXyEd7uL0JQ/h7T5qLj/5WNn/cCyi2X9VLllxLdRGauZ77NiB8KKVocb
i6BJM2gX/NszrBDsR/nyP1q0tLNalNE0IXiw0hsv6WamN2YvDfQtKm7Y4giAqhHY
Er7kwxR//OuNuf6Mb5YZVk8c1oZwIT0Vs9q6rlpcPLjpsWN769C+Pu4TP4Pmxh6n
zihPxWSnPLc4VegkpTzN+qix4cFgn6cYBIlvLg11NS0Vb+xKxtQroL67QWq0+LB0
5nS9ao4jxWFM80puWT813ThJ3vt/kIw5a1R9gNEuXW/zOwKQxfddvEO84YprVRRX
0TI2eZcjjBI0P6qJHkn1/B7rjZ7P0afDwCOS4K1vB0RHuP6tPwUI6nC+4R/+emfB
BHapRHdVpO8uotDNoaDhIQIDAQAB
-----END PUBLIC KEY-----`);
const rsaDecrypt = new window.JSEncrypt();
rsaDecrypt.setKey(
`-----BEGIN RSA PRIVATE KEY-----
MIIJJgIBAAKCAgBNuSrpc+p9fd9I3qSa6+nCHeNHfagAlCfJisM3eglbNN1yvY8U
2DqtrYdPhukxSefo4iFMj65ngt+ZXLaLp6po4VsG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BvVRpkC2NwqRrexv+
zMQ3sgQFiD4ecdRAgyJ1+6c4f1eRsG0trDRHI9yL/2sPpjVmaeKKHFZkBR3AYRPv
QvffWJMCmVZ+0KIvaq77WcYyW+/WOjOXPbyVwUClJEdb21x177P3pAS/KWEHGOiu
ECfvHEqwx50cSjKIkgTNeh7VKIZUGo3vhQ5fVCP0NP5+Lknyv8T1CH/xA95iEX5K
oF0CggEANTs0gkWLS4Myg3KlrUVh18aTBJDeu6TSgfOBxefCXcbyII5mYotsY0Sv
hYbW8CanAWu30wZ7y2lTbd4xVuFCja7kjGguIpTRJxlrrhk1oqnJKn1lVWnHydOE
H4XLaQJye3t0sL9OuLmZZAX3mOB7UeZj7gOfvYrHjxJOmXMZw82sBipkey4TWbCY
0FbxqXE2GVxVZaG+avV1/VWxdKVaEzi3t3CnMn2wZO6v6fOQNfr6lqLc5EvkUlRr
m8xrT6BaEyfPPUjjHZrem7wFbYhCWmOgA5cDwl59Ru6wvmBMrjqyFJgT/+UFpXir
PcvzaUdSfwcBj80RU4eKOhoTlkfzDwKCAQB1YH42FTw8uXeg0xz0Hgs2+Id3v1Ft
80iZVkxTxpymTyVW0d09X3n2XJlKHzJA9jUS2kCLdaaAJSqbV3KTqPBM4wWvqLoe
JMGH0oiyiRO4yfyJjYXozdOuyz9AvoX5uDSwXyxqksNGLPlnAYVyLlvVEVef6Npy
o3zooCk/pRbJV8OkzuM2ukQ3OBIiOVEC3fxTzo8DOyZraZ9jP3FMhOXhJK+NVY/i
PuDLgKtaj0jfjtjmkw3j3G317y3tGxRG54VpeN0M7+7dPWF6KHqQjeySt3ELP+mU
8v5+L4GE4zk43Ni4OICC00wsvHQk1kck/NzNFOyeb0Qg/2hXMp13P7TZ
-----END RSA PRIVATE KEY-----`
);
const encryptedKey = rsaEncrypt.encrypt(key);
console.log(rsaDecrypt.decrypt(encryptedKey));

And the command I use to decrypt is this: (this is a command I use currently, but I've tried a million of different commands and none worked!)

openssl rsautl -in "$ENCRYPTED_FILE" -out "decrypted.txt" -inkey "private-key.pem"

I'm also open to using a different library that is compatible to be used with OpenSSL.

0

There are 0 answers