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 hidden or 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're here trying to figure out how to do this on Ubuntu 22 / OpenSSL 3: https://gist.github.com/WilliamNHarvey/0e37f84a86e66f9acb7ac8c68b0f996b