npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zaes-js

v2.1.0

Published

AES encryption/decryption library

Downloads

21

Readme

zaes-js

ES 6 npm version Build Status Codacy Badge Code Climate Codacy Badge

AES encryption implementation.

Attention!

Before you will learn this lib, I highly recommend you to go through other aes implementations in js, because I imlemented zaes.js only because other libraries did not meet my requirements. Some of them you can find here.

Installation

npm install zaes-js

Usage

Usage of zaes-js on browser is similar to usage in nodejs. The only difference is initialization.

Initialization

Node js 6+

var aes = require("zaes-js");

Browser

There is bundle.zaes.js in ./node_modules/zaes-js/ directory.

Include it on your html:

<script src="/path/to/bundle.zaes.js"></script>

After loading script there will be aes global varialbe available.

Generating key

To encrypt string with AES, you always need a key. You can use own key or generate it using aes.generateKey function that returns secure random key.

Note!

If you using your own key be sure that is valid. Valid means that it is array of bytes(0-255) with length 16, 24 or 32(for 128, 192 and 256 bit keys respectively).

var keyLength = 16; // Or it can be 24 or 32
var key = aes.generateKey(keyLength);

key -> [90, 50, 232, 234, 132, 249, 197, 211, 125, 63, 231, 52, 35, 126, 190, 42]

Encrypting

Use aes.encrypt(bytes, key) function to encrypt array of bytes.

Where bytes is array of bytes with any length and key is AES key to encrypt with.

var bytes = [122, 97, 101, 115, 32, 106, 115];
var encrypted = aes.encrypt(bytes, key);

encrypted -> [32, 223, 97, 187, 151, 149, 181, 126, 92, 222, 193, 174, 219, 39, 60, 231]

Decrypting

Use aes.decrypt(bytes, key) to decrypt data that was encrypted with passed key.

var decrypted = aes.decrypt(encrypted, key);

decrypted -> [122, 97, 101, 115, 32, 106, 115]

Utils

You read above how to encrypt/decrypt bytes. But what if you will want string to be encrypted.

For this case zaes-js includes utils module, that allows to convert strings to bytes and bytes to strings.

See examples below for single byte characters in string(UTF8, ASCII).

var message = "my top secret message";
var bytes = aes.utils.stringToBytes(message);

var key = aes.generateKey(32); // top secret key

var cipher = aes.encrypt(bytes);

var initialMessage = aes.utils.bytesToString(aes.decrypt(bytes, key));

If your string contains character which coded with 2 or more bytes you need to specify it in param for stringToBytes and bytesToString functions.

var bytesPerChar = 2;
var message = "鸠尠㜂᠆삮";  // chinese characters codes with 2 bytes
var bytes = aes.utils.stringToBytes(message, bytesPerChar);

var initMessage = aes.utils.bytesToString(bytes, bytesPerChar);

If you don't sure about bytesPerChar param you can use detectBytesPerChar function.

var message = "strange message, with 鸠尠㜂᠆삮 characters";
var bytesPerChar = aes.utils.detectBytesPerChar(message); // it will return 2, because of chinese chars

// then use bytesPerChar in stringToBytes or bytesToString