A ruby implementation of the RFC 7516 JSON Web Encryption (JWE) standard.
gem install jwe
This example uses the default alg and enc methods (RSA-OAEP and A128CBC-HS256). It requires an RSA key.
require 'jwe'
key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."
encrypted = JWE.encrypt(payload, key)
puts encrypted
plaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
This example uses a custom enc method:
require 'jwe'
key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."
encrypted = JWE.encrypt(payload, key, enc: 'A192GCM')
puts encrypted
plaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
This example uses the 'dir' alg method. It requires an encryption key of the correct size for the enc method
require 'jwe'
key = SecureRandom.random_bytes(32)
payload = "The quick brown fox jumps over the lazy dog."
encrypted = JWE.encrypt(payload, key, alg: 'dir')
puts encrypted
plaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
This example uses the DEFLATE algorithm on the plaintext to reduce the result size.
require 'jwe'
key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."
encrypted = JWE.encrypt(payload, key, zip: 'DEF')
puts encrypted
plaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
This example sets an extra plaintext custom header.
require 'jwe'
key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."
# In this case we add a copyright line to the headers (it can be anything you like
# just remember it is plaintext).
encrypted = JWE.encrypt(payload, key, copyright: 'This is my stuff! All rights reserved')
puts encrypted
The RFC 7518 JSON Web Algorithms (JWA) spec defines the algorithms for encryption and key management to be supported by a JWE implementation.
Only a subset of these algorithms is implemented in this gem. Striked elements are not available:
Key management:
Encryption:
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.