一.node啟動js公鑰加密 //需要導入模塊npm install node-forge var arguments = process.argv.splice(2); // console.log('所傳遞的參數是:', arguments); var e = arguments[1]; var ...
一.node啟動js公鑰加密
//需要導入模塊npm install node-forge
var arguments = process.argv.splice(2);
// console.log('所傳遞的參數是:', arguments);
var e = arguments[1];
var t = arguments[0];
var name = arguments[2];
var forge = require('node-forge');
// var fs = require('fs'); 要寫入本地加這個
var n = "-----BEGIN PUBLIC KEY-----" + e + "-----END PUBLIC KEY-----"
, r = forge.pki.publicKeyFromPem(n)
, o = r.encrypt(t);
var data = forge.util.encode64(o).toString();
// fs.writeFile(name, data, function (error) {});
console.log(data); //進行輸出
二.python公鑰加密
# 公鑰加密
import base64
import rsa
from Crypto.PublicKey import RSA
def encryptPassword(password, publicKeyStr):
'''
password:密碼
publicKeyStr:公鑰
'''
# 1、base64解碼
publicKeyBytes = base64.b64decode(publicKeyStr.encode())
# 3、生成publicKey對象
key = RSA.import_key(publicKeyBytes)
# 4、對原密碼加密
encryptPassword = rsa.encrypt(password.encode(), key)
return base64.b64encode(encryptPassword).decode()