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

multi-crypto-js

v1.0.3

Published

前端js的多种混合加密实现库

Downloads

29

Readme

简要说明:

​ 此工具是一个集RSA、AES、SM2、SM4为一体的算法工具,支持base64和hex两种格式的密钥、向量、签名结果以及加密结果.

​ 其中, RSA基于jsencrypt封装、AES基于Crypto-JS封装、SM2基于sm-crypto封装、SM4基于第三方开源实现封装.

​ AES、SM4算法生成的密钥和向量均为128位(base64格式或hex格式解码后的长度).

​ AES加密算法使用的是 AES/CBC/PKCS7Padding.

​ SM4加密算法使用的是 SM4/CBC/PKCS7Padding.

​ RSA加密算法使用的是 RSA/None/PKCS1Padding, 暂不支持分段加解密.

​ RSA签名算法支持MD5withRSA、SHA1withRSA、SHA256withRSA、SHA384withRSA和SHA512withRSA.

​ SM2签名算法支持SM3withSM2.

​ 此工具支持常用浏览器和微信小程序.

const LONG_TEXT = "加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容。 在航空学中,指利用航空摄影像片上已知的少数控制点,通过对像片测量和计算的方法在像对或整条航摄带上增加控制点的作业。"
const SHORT_TEXT = "这是一段短字符串"

一、Sm2+Sm4

签名+验签

const app = require("multi-crypto-js").Sm2Sm4;
let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

let signatureBase64 = app.signByPrivateKey(SHORT_TEXT, keyPair1.privateKey, app.FORMAT_BASE64);
console.log("signature base64: " + signatureBase64);
let signatureHex = app.signByPrivateKey(SHORT_TEXT, keyPair2.privateKey, app.FORMAT_HEX);
console.log("signature hex: " + signatureHex);

let verifySignatureBase64 = app.verifySignByPublicKey(SHORT_TEXT, signatureBase64, keyPair1.publicKey, app.FORMAT_BASE64);

let verifySignatureHex = app.verifySignByPublicKey(SHORT_TEXT, signatureHex, keyPair2.publicKey, app.FORMAT_HEX);

加密+解密

const app = require("multi-crypto-js").Sm2Sm4;
let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

let encryptedBase641 = app.encryptByPublicKey(SHORT_TEXT, keyPair1.publicKey);
console.log("encrypt base64: " + encryptedBase641);
let encryptedHex1 = app.encryptByPublicKey(SHORT_TEXT, keyPair2.publicKey, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex1);

let decryptedBase641 = app.decryptByPrivateKey(encryptedBase641, keyPair1.privateKey);
let decryptedHex1 = app.decryptByPrivateKey(encryptedHex1, keyPair2.privateKey, app.FORMAT_HEX);

let keyBase64 = app.generateDataKey();
let ivBase64 = app.generateDataIv();
console.log("key base64: " + keyBase64);
console.log("iv base64: " + ivBase64);

let keyHex = app.generateDataKey(app.FORMAT_HEX);
let ivHex = app.generateDataIv(app.FORMAT_HEX);
console.log("key hex: " + keyHex);
console.log("iv hex: " + ivHex);

let encryptedBase642 = app.encryptByDataKey(LONG_TEXT, keyBase64, ivBase64);
console.log("encrypt base64: " + encryptedBase642);
let encryptedHex2 = app.encryptByDataKey(LONG_TEXT, keyHex, ivHex, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex2);

let decryptedBase642 = app.decryptByDataKey(encryptedBase642, keyBase64, ivBase64);
let decryptedHex2 = app.decryptByDataKey(encryptedHex2, keyHex, ivHex, app.FORMAT_HEX);

二、Sm2+AeS

签名+验签

const app = require("multi-crypto-js").Sm2Aes;
let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

let signatureBase64 = app.signByPrivateKey(SHORT_TEXT, keyPair1.privateKey, app.FORMAT_BASE64);
console.log("signature base64: " + signatureBase64);
let signatureHex = app.signByPrivateKey(SHORT_TEXT, keyPair2.privateKey, app.FORMAT_HEX);
console.log("signature hex: " + signatureHex);

let verifySignatureBase64 = app.verifySignByPublicKey(SHORT_TEXT, signatureBase64, keyPair1.publicKey, app.FORMAT_BASE64);

let verifySignatureHex = app.verifySignByPublicKey(SHORT_TEXT, signatureHex, keyPair2.publicKey, app.FORMAT_HEX);

加密+解密

const app = require("multi-crypto-js").Sm2Aes;
let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

let encryptedBase641 = app.encryptByPublicKey(SHORT_TEXT, keyPair1.publicKey);
console.log("encrypt base64: " + encryptedBase641);
let encryptedHex1 = app.encryptByPublicKey(SHORT_TEXT, keyPair2.publicKey, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex1);

let decryptedBase641 = app.decryptByPrivateKey(encryptedBase641, keyPair1.privateKey);
let decryptedHex1 = app.decryptByPrivateKey(encryptedHex1, keyPair2.privateKey, app.FORMAT_HEX);

let keyBase64 = app.generateDataKey();
let ivBase64 = app.generateDataIv();
console.log("key base64: " + keyBase64);
console.log("iv base64: " + ivBase64);

let keyHex = app.generateDataKey(app.FORMAT_HEX);
let ivHex = app.generateDataIv(app.FORMAT_HEX);
console.log("key hex: " + keyHex);
console.log("iv hex: " + ivHex);

let encryptedBase642 = app.encryptByDataKey(LONG_TEXT, keyBase64, ivBase64);
console.log("encrypt base64: " + encryptedBase642);
let encryptedHex2 = app.encryptByDataKey(LONG_TEXT, keyHex, ivHex, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex2);

let decryptedBase642 = app.decryptByDataKey(encryptedBase642, keyBase64, ivBase64);
let decryptedHex2 = app.decryptByDataKey(encryptedHex2, keyHex, ivHex, app.FORMAT_HEX);

三、Rsa+Sm4

签名+验签

const app = require("multi-crypto-js").RsaSm4;
let signatureAlgorithms = [ app.SIGNATURE_MD5withRSA, app.SIGNATURE_SHA1withRSA, app.SIGNATURE_SHA256withRSA, app.SIGNATURE_SHA384withRSA, app.SIGNATURE_SHA512withRSA ];

let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

signatureAlgorithms.forEach(signatureAlgorithm => {
    let signatureBase64 = app.signByPrivateKey(SHORT_TEXT, keyPair1.privateKey, app.FORMAT_BASE64, signatureAlgorithm);
    console.log("signature " + signatureAlgorithm + " base64: " + signatureBase64);
    let signatureHex = app.signByPrivateKey(SHORT_TEXT, keyPair2.privateKey, app.FORMAT_HEX, signatureAlgorithm);
    console.log("signature " + signatureAlgorithm + " hex: " + signatureHex);

    let verifySignatureBase64 = app.verifySignByPublicKey(SHORT_TEXT, signatureBase64, keyPair1.publicKey, app.FORMAT_BASE64, signatureAlgorithm);
    
    let verifySignatureHex = app.verifySignByPublicKey(SHORT_TEXT, signatureHex, keyPair2.publicKey, app.FORMAT_HEX, signatureAlgorithm);
    

})

加密+解密

const app = require("multi-crypto-js").RsaSm4;
let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

let encryptedBase641 = app.encryptByPublicKey(SHORT_TEXT, keyPair1.publicKey);
console.log("encrypt base64: " + encryptedBase641);
let encryptedHex1 = app.encryptByPublicKey(SHORT_TEXT, keyPair2.publicKey, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex1);

let decryptedBase641 = app.decryptByPrivateKey(encryptedBase641, keyPair1.privateKey);
let decryptedHex1 = app.decryptByPrivateKey(encryptedHex1, keyPair2.privateKey, app.FORMAT_HEX);

let keyBase64 = app.generateDataKey();
let ivBase64 = app.generateDataIv();
console.log("key base64: " + keyBase64);
console.log("iv base64: " + ivBase64);

let keyHex = app.generateDataKey(app.FORMAT_HEX);
let ivHex = app.generateDataIv(app.FORMAT_HEX);
console.log("key hex: " + keyHex);
console.log("iv hex: " + ivHex);

let encryptedBase642 = app.encryptByDataKey(LONG_TEXT, keyBase64, ivBase64);
console.log("encrypt base64: " + encryptedBase642);
let encryptedHex2 = app.encryptByDataKey(LONG_TEXT, keyHex, ivHex, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex2);

let decryptedBase642 = app.decryptByDataKey(encryptedBase642, keyBase64, ivBase64);
let decryptedHex2 = app.decryptByDataKey(encryptedHex2, keyHex, ivHex, app.FORMAT_HEX);

四、Rsa+Aes

签名+验签

const app = require("multi-crypto-js").RsaAes;
let signatureAlgorithms = [ app.SIGNATURE_MD5withRSA, app.SIGNATURE_SHA1withRSA, app.SIGNATURE_SHA256withRSA, app.SIGNATURE_SHA384withRSA, app.SIGNATURE_SHA512withRSA ];

let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

signatureAlgorithms.forEach(signatureAlgorithm => {
    let signatureBase64 = app.signByPrivateKey(SHORT_TEXT, keyPair1.privateKey, app.FORMAT_BASE64, signatureAlgorithm);
    console.log("signature " + signatureAlgorithm + " base64: " + signatureBase64);
    let signatureHex = app.signByPrivateKey(SHORT_TEXT, keyPair2.privateKey, app.FORMAT_HEX, signatureAlgorithm);
    console.log("signature " + signatureAlgorithm + " hex: " + signatureHex);

    let verifySignatureBase64 = app.verifySignByPublicKey(SHORT_TEXT, signatureBase64, keyPair1.publicKey, app.FORMAT_BASE64, signatureAlgorithm);
    
    let verifySignatureHex = app.verifySignByPublicKey(SHORT_TEXT, signatureHex, keyPair2.publicKey, app.FORMAT_HEX, signatureAlgorithm);
    

})

加密+解密

const app = require("multi-crypto-js").RsaAes;
let keyPair1 = app.generateKeyPair();
console.log("privateKey base64: " + keyPair1.privateKey);
console.log("publicKey base64: " + keyPair1.publicKey);
console.log("algorithm base64: " + keyPair1.algorithm);

let keyPair2 = app.generateKeyPair(app.FORMAT_HEX);
console.log("privateKey hex: " + keyPair2.privateKey);
console.log("publicKey hex: " + keyPair2.publicKey);
console.log("algorithm hex: " + keyPair2.algorithm);

let encryptedBase641 = app.encryptByPublicKey(SHORT_TEXT, keyPair1.publicKey);
console.log("encrypt base64: " + encryptedBase641);
let encryptedHex1 = app.encryptByPublicKey(SHORT_TEXT, keyPair2.publicKey, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex1);

let decryptedBase641 = app.decryptByPrivateKey(encryptedBase641, keyPair1.privateKey);
let decryptedHex1 = app.decryptByPrivateKey(encryptedHex1, keyPair2.privateKey, app.FORMAT_HEX);

let keyBase64 = app.generateDataKey();
let ivBase64 = app.generateDataIv();
console.log("key base64: " + keyBase64);
console.log("iv base64: " + ivBase64);

let keyHex = app.generateDataKey(app.FORMAT_HEX);
let ivHex = app.generateDataIv(app.FORMAT_HEX);
console.log("key hex: " + keyHex);
console.log("iv hex: " + ivHex);

let encryptedBase642 = app.encryptByDataKey(LONG_TEXT, keyBase64, ivBase64);
console.log("encrypt base64: " + encryptedBase642);
let encryptedHex2 = app.encryptByDataKey(LONG_TEXT, keyHex, ivHex, app.FORMAT_HEX);
console.log("encrypt hex: " + encryptedHex2);

let decryptedBase642 = app.decryptByDataKey(encryptedBase642, keyBase64, ivBase64);
let decryptedHex2 = app.decryptByDataKey(encryptedHex2, keyHex, ivHex, app.FORMAT_HEX);

五、动态整合使用

签名+验签

const app = require("multi-crypto-js").MultiCrypto;
let algorithms = [
    [app.ALGORITHM_RSA_AES, app.SIGNATURE_MD5withRSA],
    [app.ALGORITHM_RSA_SM4, app.SIGNATURE_MD5withRSA],
    [app.ALGORITHM_RSA_AES, app.SIGNATURE_SHA1withRSA],
    [app.ALGORITHM_RSA_SM4, app.SIGNATURE_SHA1withRSA],
    [app.ALGORITHM_RSA_AES, app.SIGNATURE_SHA256withRSA],
    [app.ALGORITHM_RSA_SM4, app.SIGNATURE_SHA256withRSA],
    [app.ALGORITHM_RSA_AES, app.SIGNATURE_SHA384withRSA],
    [app.ALGORITHM_RSA_SM4, app.SIGNATURE_SHA384withRSA],
    [app.ALGORITHM_RSA_AES, app.SIGNATURE_SHA512withRSA],
    [app.ALGORITHM_RSA_SM4, app.SIGNATURE_SHA512withRSA],
    [app.ALGORITHM_SM2_AES, app.SIGNATURE_SM3withSM2],
    [app.ALGORITHM_SM2_SM4, app.SIGNATURE_SM3withSM2]
];

algorithms.forEach(a => {
    let algorithm = a[0];
    let signatureAlgorithm = a[1];

    let keyPair1 = app.generateKeyPair(algorithm);
    console.log("privateKey base64: " + keyPair1.privateKey);
    console.log("publicKey base64: " + keyPair1.publicKey);
    console.log("algorithm base64: " + keyPair1.algorithm);

    let keyPair2 = app.generateKeyPair(algorithm, app.FORMAT_HEX);
    console.log("privateKey hex: " + keyPair2.privateKey);
    console.log("publicKey hex: " + keyPair2.publicKey);
    console.log("algorithm hex: " + keyPair2.algorithm);

    let signatureBase64 = app.signByPrivateKey(SHORT_TEXT, keyPair1.privateKey, algorithm, app.FORMAT_BASE64, signatureAlgorithm);
    console.log(algorithm + " signature " + signatureAlgorithm + " base64: " + signatureBase64);
    let signatureHex = app.signByPrivateKey(SHORT_TEXT, keyPair2.privateKey, algorithm, app.FORMAT_HEX, signatureAlgorithm);
    console.log(algorithm + " signature " + signatureAlgorithm + " hex: " + signatureHex);

    let verifySignatureBase64 = app.verifySignByPublicKey(SHORT_TEXT, signatureBase64, keyPair1.publicKey, algorithm, app.FORMAT_BASE64, signatureAlgorithm);
    
    let verifySignatureHex = app.verifySignByPublicKey(SHORT_TEXT, signatureHex, keyPair2.publicKey, algorithm, app.FORMAT_HEX, signatureAlgorithm);
    

})

加密+解密

const app = require("multi-crypto-js").MultiCrypto;
let algorithms = [app.ALGORITHM_RSA_AES, app.ALGORITHM_RSA_SM4, app.ALGORITHM_SM2_AES, app.ALGORITHM_SM2_SM4];
let formats = [app.FORMAT_BASE64, app.FORMAT_HEX];
algorithms.forEach(algorithm => {
    formats.forEach(format => {
        let keyPair = app.generateKeyPair(algorithm, format);
        console.log("privateKey " + format + ": " + keyPair.privateKey);
        console.log("publicKey " + format + ": " + keyPair.publicKey);
        console.log("algorithm " + algorithm + ": " + keyPair.algorithm);

        let encrypted1 = app.encryptByPublicKey(SHORT_TEXT, keyPair.publicKey, algorithm, format);
        console.log(algorithm + " encrypt " + format + ": " + encrypted1);
        let decrypted1 = app.decryptByPrivateKey(encrypted1, keyPair.privateKey, algorithm, format);
        

        let key = app.generateDataKey(algorithm, format);
        let iv = app.generateDataIv(algorithm, format);
        let encrypted2 = app.encryptByDataKey(LONG_TEXT, key, iv, algorithm, format);
        console.log(algorithm + " encrypt " + format + ": " + encrypted2);
        let decrypted2 = app.decryptByDataKey(encrypted2, key, iv, algorithm, format);
        
    })
});