Decrypting Encrypted JSON Web Tokens (JWE) with Ruby
There are a number of great standards for encrypting data, and one I interact with quite a lot is JSON Web Encryption.
As mentioned in Why I Actively Discourage Online Tooling like
jwt.io and Online JSON Validators, I like having the option to use offline tools (which I can audit more easily) for common tasks.
jose gem allows us to do this pretty nicely, and it has some really useful utilities for parsing different key formats.
We can create the following script:
require 'jose' # if using a PEM file key = JOSE::JWK.from_pem ARGV # if using a JWK key = JOSE::JWK.from_map JSON.parse(File.read ARGV) token = File.read ARGV puts JOSE::JWE.block_decrypt(key, token).first
This allows us to execute it as such:
# i.e. if using PEMs $ ruby decrypt.rb key.pem jwe.txt The true sign of intelligence is not knowledge but imagination.