Skip to content

Instantly share code, notes, and snippets.

@HCLarsen
Created October 23, 2018 00:25
Show Gist options
  • Save HCLarsen/1fcf3297d34e71d65fba5289c1eab80a to your computer and use it in GitHub Desktop.
Save HCLarsen/1fcf3297d34e71d65fba5289c1eab80a to your computer and use it in GitHub Desktop.
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