Skip to content

Commit 3d7cf4d

Browse files
committed
Add encryption and decryption test method
1 parent 8181de6 commit 3d7cf4d

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

src/lib/crypto.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const hashjs = require('hash.js');
66
const elliptic = require('elliptic');
77
const Secp256k1 = elliptic.ec('secp256k1');
88
const addressCodec = require('chainsql-address-codec');
9-
9+
const utils = require('chainsql-keypairs/distrib/npm/utils');
1010
//var debug = require('debug')('crypto');
1111
var crypto = require('crypto');
1212
var sjcl = require('sjcl');
@@ -238,6 +238,23 @@ var symEncrypt = function(symKey, plaintext, algType = 'aes') {
238238
}
239239
};
240240

241+
/**
242+
* 非对称加密方法
243+
* @param plaintext 明文
244+
* @param publicKey 公钥
245+
* @returns string 加密后的十六进制格式
246+
*/
247+
var asymEncrypt = function(plaintext, publicKey, algType = 'ecies') {
248+
if ( algType === "gmAlg" ) {
249+
token = keypairs.gmAlgSm2Enc(publicKey, plaintext);
250+
}else if(algType === "softGMAlg") {
251+
var plaintextHex = Buffer.from(plaintext, 'utf8').toString("hex");
252+
return keypairs.softGMAlgSm2Enc(plaintextHex,publicKey);
253+
}else {
254+
return eciesEncrypt(plaintext, publicKey);
255+
}
256+
};
257+
241258
var aesEncrypt = function(secret, plaintext) {
242259
var secretPadded = paddingPass(secret,AESKeyLength);
243260
var aesKey =Buffer.from(secretPadded, 'utf8');
@@ -259,12 +276,31 @@ var symDecrypt = function(symKey, encryptedHex, algType = 'aes') {
259276
if(algType === "gmAlg") {
260277
return keypairs.gmAlgSymDec(symKey, encryptedHex);
261278
} else if(algType === "softGMAlg"){
262-
return keypairs.softGMAlgSymDec(symKey, encryptedHex);
279+
return keypairs.softGMAlgSymDec(symKey, encryptedHex);
263280
}else {
264281
return aesDecrypt(symKey, encryptedHex);
265282
}
266283
};
267284

285+
/**
286+
* 解密方法
287+
* @param encryptedHex 密文十六进制格式
288+
* @param privateKey 私钥
289+
* @returns string 解密后的明文
290+
*/
291+
var asymDecrypt = function(encryptedHex, privateKey, algType = 'ecies') {
292+
293+
if(algType === "gmAlg") {
294+
return symKey = keypairs.gmAlgSm2Dec(privateKey, encryptedHex);
295+
}else if(algType === "softGMAlg"){
296+
var plainhex = keypairs.softGMAlgSm2Dec(privateKey, encryptedHex);
297+
return Buffer.from(plainhex, 'hex');
298+
}else {
299+
return eciesDecrypt(encryptedHex, privateKey);
300+
}
301+
};
302+
303+
268304
var aesDecrypt = function(secret, encryptedHex) {
269305
var secretPadded = paddingPass(secret,AESKeyLength);
270306
var aesKey =Buffer.from(secretPadded, 'utf8');
@@ -431,5 +467,7 @@ module.exports = {
431467
symEncrypt,
432468
symDecrypt,
433469
encryptText,
434-
decryptText
470+
decryptText,
471+
asymEncrypt,
472+
asymDecrypt
435473
};

test/server.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const r = new ChainsqlAPI();
77
// var common = require(basePath);
88
var crypto = require('../src/lib/crypto');
99
const keypairs = require('chainsql-keypairs');
10-
10+
const utils = require('chainsql-keypairs/distrib/npm/utils');
1111
main();
1212
async function main(){
1313
try {
@@ -32,10 +32,10 @@ async function main(){
3232
// });
3333

3434

35-
// var cipher = crypto.eciesEncrypt("hello","03B7FBF1AC149B0D297B7407CAB9636792333B8D8B8A4036B2D4DE2E6D69D435B5");
36-
// var keypair = keypairs.deriveKeypair("xxHgHoRAHdGZxy5gWUdMeUK7hWrgr");
37-
// var plain = crypto.eciesDecrypt(cipher,keypair.privateKey);
38-
// console.log(plain);
35+
var cipher = crypto.eciesEncrypt("hello","03B7FBF1AC149B0D297B7407CAB9636792333B8D8B8A4036B2D4DE2E6D69D435B5");
36+
var keypair = keypairs.deriveKeypair("xxHgHoRAHdGZxy5gWUdMeUK7hWrgr");
37+
var plain = crypto.eciesDecrypt(cipher,keypair.privateKey);
38+
console.log(plain.toString());
3939

4040
//字段级加密
4141
console.log("multi encrypt test:");
@@ -47,6 +47,32 @@ async function main(){
4747
var text2 = await crypto.decryptText(cip,"xnHAcvtn1eVLDskhxPKNrhTsYKqde");
4848
console.log("plain text2:" + text2);
4949

50+
console.log("Symmetric encryption Test");
51+
var symCipher = crypto.symEncrypt("abcdefghqwertyui","hello,world", "softGMAlg");
52+
console.log(symCipher);
53+
var plainhex = crypto.symDecrypt("abcdefghqwertyui",symCipher, "softGMAlg");
54+
var symDecrypted = utils.arrayToUtf8(utils.hexToArray(plainhex));
55+
console.log(symDecrypted);
56+
57+
var symCipher = crypto.symEncrypt("abcdefghqwertyui","hello,world", "aes");
58+
console.log(symCipher);
59+
var symDecrypted = crypto.symDecrypt("abcdefghqwertyui",symCipher, "aes");
60+
console.log(symDecrypted);
61+
62+
63+
console.log("Asymmetry encryption Test");
64+
var symCipher = crypto.asymEncrypt("hello,world","pYvXDbsUUr5dpumrojYApjG8nLfFMXhu3aDvxq5oxEa4ZSeyjrMzisdPsYjfxyg9eN3ZJsNjtNENbzXPL89st39oiSp5yucU", "softGMAlg");
65+
console.log(symCipher);
66+
var symDecrypted = crypto.asymDecrypt(symCipher,"pwRdHmA4cSUKKtFyo4m2vhiiz5g6ym58Noo9dTsUU97mARNjevj", "softGMAlg");
67+
console.log(symDecrypted.toString());
68+
69+
70+
console.log("Asymmetry encryption Test");
71+
var keypair = keypairs.deriveKeypair("xpvPjSRCtmQ3G99Pfu1VMDMd9ET3W");
72+
var symCipher = crypto.asymEncrypt("hello,world",keypair.publicKey, "ecies");
73+
console.log(symCipher);
74+
var symDecrypted = crypto.asymDecrypt(symCipher,keypair.privateKey, "ecies");
75+
console.log(symDecrypted.toString());
5076

5177
console.log("AesPadding Test");
5278
var aesCipher = crypto.symEncrypt("abcdefg","hello,world");

0 commit comments

Comments
 (0)