Created
October 23, 2018 00:25
-
-
Save HCLarsen/1fcf3297d34e71d65fba5289c1eab80a to your computer and use it in GitHub Desktop.
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
require 'jose' | |
message = "Jodie Whittaker is the greatest Doctor ever!" | |
# A128GCM Symmetric Key Encryption/Decryption | |
puts "-----Symmetric Key Encryption/Decryption-----" | |
secret = 'some128bitsecret' | |
jwk = JOSE::JWK.from_oct(secret) | |
encrypted_a128gcmkw = JOSE::JWE.block_encrypt(jwk, message, { "alg" => "A128GCMKW", "enc" => "A128GCM" }).compact | |
puts "encrypted JWE: #{encrypted_a128gcmkw}" | |
decrypted = JOSE::JWE.block_decrypt(jwk, encrypted_a128gcmkw).first | |
puts "Decrypted message: #{decrypted}" | |
#RSA Assymetric Key Encryption/Decryption | |
puts "-----Assymmetric Key Encryption/Decryption-----" | |
private_key = JOSE::JWK.generate_key([:rsa, 4096]) | |
public_key = JOSE::JWK.to_public(private_key) | |
encrypted_rsaoaep = JOSE::JWE.block_encrypt(public_key, message, { "alg" => "RSA-OAEP", "enc" => "A128GCM" }).compact | |
puts "encrypted JWE: #{encrypted_rsaoaep}" | |
decrypted = JOSE::JWE.block_decrypt(private_key, encrypted_rsaoaep).first | |
puts "Decrypted message: #{decrypted}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment