Last active
May 5, 2023 01:03
-
-
Save jooeycheng/0d5fbd039b6ec586c450b7486be5e049 to your computer and use it in GitHub Desktop.
Ruby OpenSSL::PKey::RSA generate RSA Public Key from Modulus and Exponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [A] OpenSSL::PKey::RSA has undocumented `e=' and `n=' methods | |
exponent = "10001" | |
modulus = "9201EBD5DC974FDE613A85AFF2728627FD2C227F18CF1C864FBBA3781908BB7BD72C818FC37D0B70EF8708705C623DF4A9427A051B3C8205631716AAAC3FCB76114D91036E0CAEFA454254D135A1A197C1706A55171D26A2CC3E9371B86A725458E82AB82C848AB03F4F0AF3127E7B2857C3B131D52B02F9A408F4635DA7121B5B4A53CEDE687D213F696D3116EB682A4CEFE6EDFC54D25B7C57D345F990BB5D8D0C92033639FAC27AD232D9D474896668572F494065BC7747FF4B809FE3084A5E947F72E59309EDEAA5F2D81027429BF4827FB62006F763AFB2153C4A959E579390679FFD7ADE1DFE627955628DC6F2669A321626D699A094FFF98243A7C105" | |
rsa = OpenSSL::PKey::RSA.new | |
e = exponent.to_i(16) | |
n = modulus.to_i(16) | |
rsa.e = OpenSSL::BN.new(e) | |
rsa.n = OpenSSL::BN.new(n) | |
rsa.public_encrypt("something") | |
# [B] simplified with `.tap' | |
rsa = OpenSSL::PKey::RSA.new.tap do |rsa| | |
rsa.e = OpenSSL::BN.new(exponent) | |
rsa.n = OpenSSL::BN.new(modulus) | |
end | |
rsa.public_encrypt("something") |
Someone please god answer the above!!!!!
If anyone is still looking for the way to convert modulus and exponent in Base64 encoding to OpenSSL::PKey::RSA
: https://gist.github.com/trojkac/a78d5af6c62cc743dad6fbd7e337701b
If you're here trying to figure out how to do this on Ubuntu 22 / OpenSSL 3: https://gist.github.com/WilliamNHarvey/0e37f84a86e66f9acb7ac8c68b0f996b
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to convert e and n from the json, which are in different encoding, "e":"AQAB","n":"tVKUtcx_n9rt5afY_2WFNvU6PlF...." to the format you have in the example above? Thanks