JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 37
  • Score
    100M100P100Q60574F
  • License MIT

Javascript Crypto Library for browser

Package Exports

  • client-crypto
  • client-crypto/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 (client-crypto) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

client-crypto

Javascript Crypto Library for browser

Install

npm install client-crypto

Usage

流程:

  1. 服务端使用非对称加密RSA生成公钥私钥,并将公钥发送给客户端。
  2. 客户端生成对称加密(这里使用AES)密钥,使用服务端发送的RSA公钥加密该密钥,并将其发送给服务端。
  3. 服务端使用RSA私钥解密,得到该密钥。
    此时,服务端、客户端同时拥有该密钥,使用AES加密解密消息体。

RSA

加密

import Crypto from 'client-crypto';

Crypto.RSA.encrypt('secretKey', 'publicKey');

解密

import Crypto from 'client-crypto';

Crypto.RSA.decrypt('encryptedKey', 'privateKey');

OAEP 加密

使用 OAEP 填充模式结合 SHA-256 哈希算法

import Crypto from 'client-crypto';

Crypto.RSA.encryptOAEP('secretKey', 'publicKey');

OAEP 解密

下面是用 nodejs 进行解密的示例

import crypto from 'crypto';

/** RSA 解密 */
export function privateDecrypt(privateKey: crypto.KeyLike, encryptedText: string) {
    const encryptedBuffer = Buffer.from(encryptedText, 'base64');
    const msgBuffer = crypto.privateDecrypt(
        { key: privateKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' },
        encryptedBuffer
    );

    return String.fromCharCode.apply(null, msgBuffer);
}

AES

默认使用gcm模式。

createKey 生成密钥

import Crypto from 'client-crypto';

Crypto.AES.createKey();

使用该方法生成密钥后,会缓存该密钥,使用加密(encrypt)或解密(decrypt)方法时也可不提供密钥。

encrypt 加密

import Crypto from 'client-crypto';

Crypto.AES.encrypt(data, '密钥');

该方法会先执行JSON.stringify(data),将数据转为json格式的字符串,然后使用AES加密。

decrypt 解密

import Crypto from 'client-crypto';

Crypto.AES.decrypt('encrypted message', '密钥');

该方法会使用AES解密,再执行JSON.parse,获取数据。

getKey 获取密钥

import Crypto from 'client-crypto';

Crypto.AES.getKey();

setKey 设置密钥

import Crypto from 'client-crypto';

Crypto.AES.setKey('密钥');

clearKey 清除密钥

import Crypto from 'client-crypto';

Crypto.AES.clearKey();

SHA256

import Crypto from 'client-crypto';

Crypto.SHA256('message');

使用说明

前端加解密