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

jpass

v0.0.1-alpha.3

Published

A library used to encrypt text with the RSA public information in the browser.

Downloads

18

Readme

JPASS

npm version Build Status install size npm downloads

一个适用于浏览器端和 Node.js 的 RSA 库,主要基于 Tom Wu 的 RSA and ECC in JavaScript 代码进行封装。支持的功能有:

  • 生成 RSA key;
  • 支持长文本分组加密及解密;
  • 支持多字节文本加密及解密(中文、emoji 表情等);

示例

var JPass = require('jpass').JPass;

// 1. 加密与解密:

var jpass = new JPass();

// 生成秘钥,长度为 1024(长度越长越安全,但是耗时也越长)
jpass.generate(1024);

// 需要加密的文本
var text = 'Secret is 秘密 ㊙️';

// 使用 `BYTE_MODE` 进行加密(加密后的数据是经过 base64 编码的)
var encrypted1 = jpass.encrypt('BYTE_MODE', text);
// 使用 `URL_MODE` 进行加密(加密后的数据是经过 base64 编码的)
var encrypted2 = jpass.encrypt('URL_MODE', text);

// 解密
var decrypted1 = jpass.decrypt('BYTE_MODE', encrypted1);
var decrypted2 = jpass.decrypt('URL_MODE', encrypted2);

console.log(decrypted1 === text); // true
console.log(decrypted2 === text); // true

// 2. 序列化与反序列化

// 序列化:返回一个 Object,可转换为 JSON 字符串进行保存
var object = jpass.serialize();

// 反序列化
var jpass2 = new JPass();
jpass2.parse(object);

// 使用反序列化后的 JPass 进行解密
var decrypted3 = jpass2.decrypt('BYTE_MODE', encrypted1);
var decrypted4 = jpass2.decrypt('URL_MODE', encrypted2);

console.log(decrypted3 === text); // true
console.log(decrypted4 === text); // true

// 3. 获取公钥信息和使用公钥信息加密

// 获取公钥信息
var details = jpass2.getPublic();
var modulus = details.modulus; // base64 编码的 modulus
var exponent = details.exponent; // base64 编码的 private exponent

var jpass3 = new JPass();

// 设置公钥信息
jpass3.setPublic(modulus, exponent);

// 加密
var encrypted3 = jpass3.encrypt('BYTE_MODE', text);
var encrypted4 = jpass3.encrypt('URL_MODE', text);

// 解密公钥加密的数据
var decrypted5 = jpass2.decrypt('BYTE_MODE', encrypted3);
var decrypted6 = jpass2.decrypt('URL_MODE', encrypted4);

console.log(decrypted5 === text); // true
console.log(decrypted6 === text); // true

BYTE_MODEURL_MODE

由于原始库是不支持多字节字符加密的,为了解决这个问题,我们在进行加密前对原始数据进行了转义处理。BYTE_MODEURL_MODE 是我们提供的两种处理模式。

  • BYTE_MODE:在加密前将原始字符串转换为字节数组,然后对字节数组进行加密。后端解密后得到的数据为字节数组(如在 Java 语言中),需要将该字节数组转换为字符串才能得到原始字符串。
  • URL_MODE:在加密前对原始字符串进行 URL 编码,然后对编码后的字符串进行加密。后端解密后得到的数据为 URL 编码后的原始字符串(如在 PHP 语言中),需要将该字符串进行 URL 解码后才能得到原始字符串。

如果后端支持字节数组操作,应该尽量选择 BYTE_MODE,该模式加密后的数据体积要更小。另外,由于前端进行了分组加密处理,后端在解密时需实现分组解密。如果后端使用 Node.js,也可以使用本模块来进行解密,本模块既支持 BYTE_MODE 解密,也支持 URL_MODE 解密,且两种模式都支持分组解密。

License