简要说明:
此工具是一个集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 ) ;
} )
} ) ;