JSPM

  • Created
  • Published
  • Downloads 13590
  • Score
    100M100P100Q137439F
  • License MIT

Universal Module for AES Encryption and Decryption in JavaScript

Package Exports

  • js-crypto-aes
  • js-crypto-aes/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (js-crypto-aes) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Universal Module for AES Encryption and Decryption in JavaScript

CircleCI

WARNING: At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

Introduction and Overview

This library is designed to be 'universal' as an AES function, i.e., it works both on most browsers and on Node.js just by importing from npm/source code. Note that in the design principle, the library fully utilizes native APIs like WebCrypto API to accelerate its operation if available.

Installation

At your project directory, do either one of the following.

  • From npm/yarn:
    $ npm install --save js-crypto-aes // npm
    $ yarn add js-crypto-aes // yarn
  • From GitHub:
    $ git clone https://github.com/junkurihara/js-crypto-aes.git

Then you should import the package as follows.

import aes from 'js-crypto-aes'; // for npm
import aes from 'js-crypto-aes/dist/index.js'; // for github

Usage

Encryption in AES-GCM

const msg = ...; // arbitrary length of message in Uint8Array
const key = ...; // 16 bytes or 32 bytes key in Uint8Array
const iv = ...; // 12 bytes IV in Uint8Array for AES-GCM mode
const additionalData = ...; // optional AAD
aes.encrypt(msg, key, {name: 'AES-GCM', iv, additionalData, tagLength: 16}).then( (encrypted) => {
  // now you get an Uint8Array of encrypted message
});

Decryption in AES-GCM

const data = ...; // encryted message in Uint8Array
const key = ...; // 16 bytes or 32 bytes key in Uint8Array
const iv = ...; // 12 bytes IV in Uint8Array for AES-GCM mode that is exactly same as the one used in encryption
const additionalData = ...; // optional AAD
aes.decrypt(data, key, {name: 'AES-GCM', iv, additionalData, tagLength: 16}).then( (decrypted) => {
  // now you get an Uint8Array of decrypted message
});

Encryption in AES-CBC

const msg = ...; // arbitrary length of message in Uint8Array
const key = ...; // 16 bytes or 32 bytes key in Uint8Array
const iv = ...; // 16 bytes IV in Uint8Array for AES-CBC mode
aes.encrypt(msg, key, {name: 'AES-CBC', iv}).then( (encrypted) => {
  // now you get an Uint8Array of encrypted message
});

Decryption in AES-CBC

const data = ...; // encryted message in Uint8Array
const key = ...; // 16 bytes or 32 bytes key in Uint8Array
const iv = ...; // 16 bytes IV in Uint8Array for AES-CBC mode that is exactly same as the one used in encryption
aes.decrypt(data, key, {name: 'AES-CBC', iv}).then( (decrypted) => {
  // now you get an Uint8Array of decrypted message
});

Note

At this point, this module has the following limitations:

  • Supports only AES-GCM and AES-CBC modes
  • Supports 128 bits and 256 bits keys in Chrome (192 bits key works in Node.js)

License

Licensed under the MIT license, see LICENSE file.