Skip to content

Instantly share code, notes, and snippets.

@snt
Last active February 20, 2023 09:58
Show Gist options
  • Save snt/bf05e4411f96def5e8d24c7705bf19db to your computer and use it in GitHub Desktop.
Save snt/bf05e4411f96def5e8d24c7705bf19db to your computer and use it in GitHub Desktop.
encrypt and decrypt using symmetric key with `node-jose`
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# JOSE encryption and decryption with `node-jose`\n",
"\n",
"`node-jose` is `Promise` based."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"{\"JWA\":{},\"JWE\":{},\"JWK\":{\"MODE_SIGN\":\"sign\",\"MODE_VERIFY\":\"verify\",\"MODE_ENCRYPT\":\"encrypt\",\"MODE_DECRYPT\":\"decrypt\",\"MODE_WRAP\":\"wrap\",\"MODE_UNWRAP\":\"unwrap\"},\"JWS\":{},\"util\":{\"base64url\":{},\"utf8\":{}}}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"jose = require('node-jose')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Though key is 256bit binary but we use text of 32 characters for this (though it is insecure, just for test)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"[Promise] {}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"keyPromise = jose.JWK.asKey({kty:'oct', k: jose.util.base64url.encode('1234567890abcdef1234567890abcdef')})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is JWK of key created above."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"[Promise] {}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"kty\":\"oct\",\"kid\":\"9payyFW0LtFts92D19URBI-ylw9Lhveb5tRJbI1S_ow\",\"k\":\"MTIzNDU2Nzg5MGFiY2RlZjEyMzQ1Njc4OTBhYmNkZWY\"}\n"
]
}
],
"source": [
"keyPromise.then(key => console.log(key.toJSON(true)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are going to encrypt a plaintext `hello!` with JWE and decrypt it."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"[Promise] {}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"eyJhbGciOiJBMjU2S1ciLCJraWQiOiI5cGF5eUZXMEx0RnRzOTJEMTlVUkJJLXlsdzlMaHZlYjV0UkpiSTFTX293IiwiZW5jIjoiQTEyOENCQy1IUzI1NiJ9.7dYNSGo0OPsGAzAfkoD1zTJyIvOFaI6D66dr5Hqgw1u0kdXczQuDkQ.TNOfavBvleb2SQtCFcQU4Q.41Z5hFgYM9cpzBy8RDk3pg.yPMnWxHeTpkyL-f5i5dxxw\n",
".......\n",
"{\n",
" \"key\": {\n",
" \"kty\": \"oct\",\n",
" \"kid\": \"9payyFW0LtFts92D19URBI-ylw9Lhveb5tRJbI1S_ow\"\n",
" },\n",
" \"header\": {\n",
" \"alg\": \"A256KW\",\n",
" \"kid\": \"9payyFW0LtFts92D19URBI-ylw9Lhveb5tRJbI1S_ow\",\n",
" \"enc\": \"A128CBC-HS256\"\n",
" },\n",
" \"protected\": [\n",
" \"alg\",\n",
" \"kid\",\n",
" \"enc\"\n",
" ],\n",
" \"plaintext\": {\n",
" \"type\": \"Buffer\",\n",
" \"data\": [\n",
" 104,\n",
" 101,\n",
" 108,\n",
" 108,\n",
" 111,\n",
" 33\n",
" ]\n",
" },\n",
" \"payload\": {\n",
" \"type\": \"Buffer\",\n",
" \"data\": [\n",
" 104,\n",
" 101,\n",
" 108,\n",
" 108,\n",
" 111,\n",
" 33\n",
" ]\n",
" }\n",
"}\n",
".......\n",
"hello!\n"
]
}
],
"source": [
"payload = 'hello!'\n",
"keyPromise.then(key => {\n",
" const encryptedPromise = jose.JWE.createEncrypt({format:'compact'},key).update(payload).final()\n",
" \n",
" encryptedPromise.then(encrypted => {\n",
" console.log(encrypted)\n",
" console.log('.......')\n",
" jose.JWE.createDecrypt(key).decrypt(encrypted).then(result => {\n",
" console.log(JSON.stringify(result, null, 2))\n",
" console.log('.......')\n",
" console.log(result.payload.toString())\n",
" })\n",
" })\n",
" \n",
"})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "NodeJS",
"language": "javascript",
"name": "nodejs"
},
"language_info": {
"codemirror_mode": "javascript",
"file_extension": ".js",
"mimetype": "text/javascript",
"name": "nodejs",
"pygments_lexer": "javascript",
"version": "0.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@krnbr
Copy link

krnbr commented Feb 20, 2023

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment