1. 首页
  2. JAVASCRIPT
  3. [nodejs]TypeScript 使用crypto 进行rsa公钥解密操作

[nodejs]TypeScript 使用crypto 进行rsa公钥解密操作

上一篇文章中提到了公钥加密的操作,使用的函数是crypto.publicEncrypt(key, buffer)现在这一篇文章来解释一下公钥解密的操作函数为crypto.publicDecrypt(k...

上一篇文章中提到了公钥加密的操作,使用的函数是

crypto.publicEncrypt(key, buffer)

现在这一篇文章来解释一下公钥解密的操作

函数为

crypto.publicDecrypt(key, buffer)


在类中添加你的公钥

this.publickey = "-----BEGIN PUBLIC KEY-----\n" +
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJgmDnxap+ffqi+85JI1gORWBM\n" +
            "qFWjtUy+m6V3mUyI72wo123123123123123123123123123c7ZmyOgmwfSXgE0ec\n" +
            "/QNt+CkzQOyfNE49alxOZoF4eHAcRaTR8E4kUwSY2ryZKKq6eYV+iP3lmiKjHLIj\n" +
            "vnAURDBnzTqIwDgnEQIDAQAB\n" +
            "-----END PUBLIC KEY-----\n";

在类中编写如下函数

    DecryptByPublicKey(str) {
        return crypto.publicDecrypt({ key: this.publicKey, padding: crypto.constants.RSA_PKCS1_PADDING },
         new Buffer(str, "base64")).toString();
    }


完整的类如下,使用时,请替换你的公钥私钥

import * as fs from 'fs';
const crypto = require('crypto');
export class CryptHelper {
    private privateKey: string;
    private publickey: string;
    constructor() {
        this.privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
            "MIICXQIBAAKBgQDJgmDnxap+ffqi+85JI1gORWBMqFWjtUy+m6V3mUyI72woaRYw\n" +
            "QGuChW1jL/G/m1EdcED/2EZc7ZmyOgmwfSXgE0ec/QNt+CkzQOyfNE49alxOZoF4\n" +
            "eHAcRaTR8E4kUwSY2ryZKKq6eYV+iP3lmiKjHLIjvnAURDBnzTqIwDgnEQIDAQAB\n" +
            "AoGBALZY9iomNC98/taH+Nc1234123412341234123412341HNt+moL6PPPqClUch\n" +
            "dqkF+rQiBZ+/bYTgOaoPXP9JcIAlEv+uhgjcdWtfZqlPJizJT4rDIRMxKu/v6quR\n" +
            "EUqh8Q9v9LoSPi5OJ3hbIqGeGdhYP06acr2GV7xq6IfiufLVAkEA+KH9zgDU/Ukz\n" +
            "+aJ3keQWXS5VT4QHL/g/123412341234123412341234SOCZ+ENeaedaMsJjQDS9\n" +
            "ilJtwCko2wJBAM967lcm7edrJmfQzBmtg7GXZyqP/w8FfNNgsuro417QSfck5kzQ\n" +
            "9mCKPRk/STQD6APLwT7nsdS/u8aOfyQ5bYMCQDMaKG5U4XIFv7cJW4YS2T3hrgsf\n" +
            "2dJUNcejwGgU/wogyWhHbr+QWAwyq1q7Pyo1vowJzxIrvfaOYH6znncmfo0CQQDO\n" +
            "nnyWXBVVfdZ8Umm/NJXOmJlLwKTfX7akhksHNy+e70yJi7yxSPPixif8aZLL8wvu\n" +
            "I85ph7EHzn0ASk4BVBNjAkBztoHeR1234123412341234131i0c2APYA9vxfTM8K\n" +
            "j+/Awx4SD3eHoQAPKRgtYSb1LGxJ1Tw0OzqtkZ9+EyJp\n" +
            "-----END RSA PRIVATE KEY-----\n";
        this.publickey = "-----BEGIN PUBLIC KEY-----\n" +
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJgmDnxap+ffqi+85JI1gORWBM\n" +
            "qFWjtUy+m6V3mUyI72wo123123123123123123123123123c7ZmyOgmwfSXgE0ec\n" +
            "/QNt+CkzQOyfNE49alxOZoF4eHAcRaTR8E4kUwSY2ryZKKq6eYV+iP3lmiKjHLIj\n" +
            "vnAURDBnzTqIwDgnEQIDAQAB\n" +
            "-----END PUBLIC KEY-----\n";
    }
    public EncryptByKey(str: string) {
        return crypto.publicEncrypt({ key: this.publickey, padding: crypto.constants.RSA_PKCS1_PADDING }, new Buffer(str)).toString("base64");
    }
    public DecryptByKey(str: string) {
        return crypto.privateDecrypt({ key: this.privateKey, padding: crypto.constants.RSA_PKCS1_PADDING }, new Buffer(str, "base64")).toString();
    }
   public  DecryptByPublicKey(str: string) {
        return crypto.publicDecrypt({ key: this.publicKey, padding: crypto.constants.RSA_PKCS1_PADDING },
         new Buffer(str, "base64")).toString();
    }
}


哎哎
相关推荐