Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello, using jsrsasign to load an existing RSA key pair cannot perform encryption and decryption. Could you please show me. #625

Open
xhc-code opened this issue Aug 4, 2024 · 0 comments

Comments

@xhc-code
Copy link

xhc-code commented Aug 4, 2024

jsrsasign The version is 10.9.0.
The algorithm used for encryption and decryption is RSA/ECB/OAEPWithSHA-256AndMGF1Padding.

The key and data are both encoded using base64. Is it necessary to convert the data into hex and pass it to the decrypt method? The official script did not find any tutorial on encryption and decryption.

Below is a demo, but it doesn't work properly.

// 1.Import jsrsasign library
const { KEYUTIL, KJUR, hextob64u, b64utohex } =  require('jsrsasign');

// 2.Base64 decoding private key
const base64PrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDYtvxVQJfybCiQvomuIjVqU2HQj2AisJgLzKmlVWaS44pysfxCJ/8d4T24btVOA9FKesn05lzYhjRjFVJiLcJXiL0adL5reD2m8sStF1hghVV03FP3PtmSnmfFg1Dte3jKjfTQvDz57+KW2xJxESiBAlFD0Ly+vrQUzubsnVVXFqvd6DojDZjpe5O+++MkBa+OWdN9VWGvr4wHMMU+Tc187E7EF/1fh4j9uYTdNlYUbVYtciPjtYRHvJ04HUc5Lw7XGTx1rUxP9PPCUn/uLHb2NShoEuv35aYLEoohsTjVJ9EKcuMv6tKBe4pMTEfHzBApZt3vKWcYakWWQYPQg22tAgMBAAECggEALNzcqc1gZzgKwcQf/mNoiWVPh3xW23eRUkgBCo/+VwQxwgKkhRGAl6/sNgUDtU3HAtMegcs6hzxpFeXxExAD/lj5RMPMM7VFlg0FuAtwKTMQ+MsvuqxRIDAYrWO82hzIaa4vcxK00MzfLHmXfL6fku1m41IP8LDL2M7oMS104D0Yn2S1hPurkWT9LMSk7Y2JHsJW23aQPNeL6a+0Ua83PbpUtJstElGsHKYHENBA0se2GA4pub2sG9KbO5awIcCCJHttuUQIjHUZbcPHXy9DspprwqNQCUpK7ub+8+nRjxnhLpgx1nD2v16cl2KHkzE+mlA8jfNPuZgq4t+m3C1KIQKBgQD3C/uP7Xck2uCCnbh+/NIvkvsL2Hk8zY+qsbLoGlQXgSMbHQ0Ll8q0G9ITCCoe7HAbcFos0vKzEH14B2v2FpTus3aNvLwikczCZSZyX4PaTmIhFMTL9HSWLpAPTLh5csF+SAe9SGeBS5kM6S4bkkurvcvO+Vm2UMfnMHmt8Jc09QKBgQDgkZe8WTpvqMbB689U8NR2G5JM4kpdikq8r3LrZf0z/m+LpC1GGQzizUvQ4UTeNKPxNZaVFDTilcmM978dnRbt3xw3RD1q3tOQ67T8KRK3YIPTxfj+K1znwqj5185vBX9u2M8vXV/btHjhBGkVqlf/NrL8upoWxS7qzao3NLMi2QKBgCsioXsA+IOr3/WOem9Zz41U+ZaY5ppSPhqMynMyXap49ts+GFAE4fTOwsBl9YzOtchktqoK7yuK3o19pkEiskB3f2TCJotWr8zlgnAH82rDsuiZyr7d4Rlz6d1yBOfXvKdDBm7rXH875u5VLKLNyX6kNhjeeuaVdkIMucJANvadAoGBANvf0UgZuSxwfMKMBsEs12kYj8L1YUh6ybg6tHMktag9u6tmwOk8R7Ccv1mx5oMF2WqnRFuMoEclz2RVgesVzshyRacbCmNj7pObJOYrtOaU+h3UXXlGrb1Bi76r/LQZFu9KGnlOnDd6WUw/zD/pPCVmwhLPBWW8BLbjTYCl+arpAoGAQW7cOxAJE25/rwrpS8wP8hbK9n1cCblbHAEEkWOvRalf1+r0CicFkxA0PmqFfPN9lJshSPChwfTnj+ivDxi4w/gJKgW2skZ+oIXry3WLONRreWmHP4EityDo9sHqGjdzvmHAwLqLXe45M2qM8TgwVaUIaFZE/IssU1ij2QHmGu0=';
const pemPrivateKey = `-----BEGIN PRIVATE KEY-----\n${base64PrivateKey}\n-----END PRIVATE KEY-----`;

// 3. Base64 decoding encrypted data
const encryptedDataB64 = "yq62qSO3ljGgHwSBeTL/4ka50xk5SS6XHizRHux3owwj4ryi4OxNFvVqC1hrApULZJQs6a2ITb6my/FU24gKsrLHEUqMzdWYPl10h9IlNlYvgffMjJS1kdVjeyDKwuFbbxnnjwulnwhlubcwJvvqZUD+yxV0vHmGBJmcj4nG3Jy2c900DxsJXHPqcFxBImPE8v6yeep5MBu3rpHg6nwCvg2+dw3GWLjsDrd5a/D4fUUPDz65yIZ+RYHe3HMfMb8RBlIYrV74Dewty2MhadwcLn5eWaXVPNk8035JSYfyOiF48dYgO8LYta1TbHFQoNlRvODC15+HmOEv1O0pSRiZ4w==";
const encryptedHex = b64utohex(encryptedDataB64);

// 4. Import private key
const rsaKey = KEYUTIL.getKey(pemPrivateKey);

// 5. Decrypt data
const decryptedHex = KJUR.crypto.Cipher.decrypt(encryptedHex, rsaKey, "RSAOAEP");
const decryptedData = hextob64u(decryptedHex);

// 6.Output decrypted data
console.log("decryptedData", decryptedData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant