-1

What I am implementing is the client encrypts a value with RSA public KEY, later server side receives and decrypts it with a RSA private key. However due to the client and server side uses different RSA library, the task is not successful.

Then I did a test.

On the client side, I use jsencrypt library.

import JSEncrypt from 'jsencrypt';
var cryptor = new JSEncrypt();
const publicEncrypt = (str:string, publicKey:string = PUBLIC_KEY) =>{
    cryptor.setPublicKey(publicKey);
    return cryptor.encrypt(str)
}
 
let encryptedData = publicEncrypt('Hello World!');
console.log('-------',encryptedData);

the result is

nvSa22jh6pstvLVOHyyAT7Ak7fu9LwOPBMGZqZVnh6ITbNRviwHO1tV1vBgQZxJPlDF0Mwes9vsDsebDZamXLB4/Pm5CsWBWcmRDadR5zCAVForDc2Kg3zr9XFpcLGZqpaz0wK5jf+atJk5H/6N5fKmTGvqyy0aTNOTVIn5c57dW8Gu7mlg2Vbl/zMPwTFn5N7Chwg1RfPWcZgwomqIsGb8pdbZyD4n10ci0pIeSNQOPASFGX1bYbRc7U1O3za90ta7CfRnZ9zes7wnnmSykpBSlnPNzSxIAAGpknAdNCF1ejIPnAv8DIVLRE7elkvqzpX5cWi7cyUxnEq9ImBENFQ==

convert it to base64

window.btoa(encryptedData)

the result is bnZTYTIyamg2cHN0dkxWT0h5eUFUN0FrN2Z1OUx3T1BCTUdacVpWbmg2SVRiTlJ2aXdITzF0VjF2QmdRWnhKUGxERjBNd2VzOXZzRHNlYkRaYW1YTEI0L1BtNUNzV0JXY21SRGFkUjV6Q0FWRm9yRGMyS2czenI5WEZwY0xHWnFwYXowd0s1amYrYXRKazVILzZONWZLbVRHdnF5eTBhVE5PVFZJbjVjNTdkVzhHdTdtbGcyVmJsL3pNUHdURm41TjdDaHdnMVJmUFdjWmd3b21xSXNHYjhwZGJaeUQ0bjEwY2kwcEllU05RT1BBU0ZHWDFiWWJSYzdVMU8zemE5MHRhN0NmUm5aOXplczd3bm5tU3lrcEJTbG5QTnpTeElBQUdwa25BZE5DRjFlaklQbkF2OERJVkxSRTdlbGt2cXpwWDVjV2k3Y3lVeG5FcTlJbUJFTkZRPT0=

Then on the server side I did the same task.

import NodeRSA from 'node-rsa';
const encrypt = new NodeRSA(PUBLIC_KEY);
const encryptedData = encrypt.encrypt('Hello World!', 'base64');

qvNO2Ugh9iY71doH+PAtzToESgKHh04ZI0SS2xai/JwWI7yD9QB93fKcJC/V/sAlBIXhYTzLdmw98iF5mIHXlnIMFAtNpyNlWoIjrQH1kaC3h8BXcbIjqD6ysp6yQ5ZqP8icyf8vDxL0NJ8/qwqGuvfOS7WsWMekH2rGEK9WXk9MQALPe1Dfl71iSxVtJp1KSq7YQHOKFC/zTeEe/X84W5+U41NrRkNDj9jJ9iX12BisWIThnw4BTfTMpewKdaQS+n7DE/EJ4acxDIxybEf2kE7RQrZqQ3ivenJvv/IlqC+4PfPWd5fG2cweGi4k/KgSQ1sOR5c7+EBlSKCaCui9rA==

two different RSA library works the same data with same public key , the encrypted result is different, even on the client the result converted with base64 is different from server's result

any idea?

const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8kn91R25PNbpE+zy/5Tb
qlR4BS9prlvZggheg6sMH6aK47LS4N+gUfwPO28833qz7jFagqUqSQF7ufQrkaeZ
Qgd8gDRfN/Qh5rJTqsJpRXlaFb57orXXFYfarw5p9U2thuHeOyi+2VTVmvETLpVs
f8pqXTJziSIOPd2IDDNmD61v0Yd/hUa/LKq5Iplhfm55cxqEDHI/J0kviSqzS7WC
CeMd+qNTJfElKM9M7lzqC9zuPb5wPrZ+iLYj3HblfWuje4R0jg/XxC+MkmPOMaVQ
3fHiuq51LVKYkgDSrsSIzxjZOs5HITy/i4aN4kzs9G2duYZqxrcYNCcqjZyVMDFr
wwIDAQAB
-----END PUBLIC KEY-----`
user824624
  • 7,077
  • 27
  • 106
  • 183
  • 1
    RSA encryptions are not deterministic, i.e. repeated encryptions of the same message with the same key produce different ciphertexts. This is not a bug, but the expected behavior. – Topaco Aug 31 '23 at 06:26
  • thanks, I will try to compare the encryption of jsencrypt with decryption with node-rsa in the pkcs1 scheme – user824624 Aug 31 '23 at 06:29
  • 1
    Note the result from JSEncrypt is _already_ base64 encoded; you don't need to and shouldn't twice-encode it. Also FYI jsencrypt works in nodejs, as well as node-rsa and node-forge and node's builtin crypto module. – dave_thompson_085 Aug 31 '23 at 09:20

0 Answers0