node启动js公钥加密,python公钥加密

2020-07-02 15:53:50 浏览数 (1)

一.node启动js公钥加密

代码语言:javascript复制
//需要导入模块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公钥加密

代码语言:javascript复制
# 公钥加密
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()

0 人点赞