【问题标题】:How to encrypt through node.js of encrypted string using algorithm RSA/ECB/PKCS1Padding如何使用算法 RSA/ECB/PKCS1Padding 通过 node.js 对加密字符串进行加密
【发布时间】:2014-01-31 19:32:51
【问题描述】:

我已经通过 Java 代码使用算法 RSA/ECB/PKCS1Padding 加密了字符串,现在同样需要使用 node.js 加密。我不知道如何使用算法 RSA/ECB/PKCS1Padding 通过 node.js 进行加密。 有什么建议? Java 代码是:

public static String encrypt(String source, String publicKey)
            throws Exception {
    Key key = getPublicKey(publicKey);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] b = source.getBytes();
    byte[] b1 = cipher.doFinal(b);
    return new String(Base64.encodeBase64(b1), "UTF-8");
}

【问题讨论】:

  • 有这方面的消息吗?
  • 对此有任何答案吗?

标签: java javascript node.js


【解决方案1】:

使用 cryto 库的节点 js 代码:

const crypto = require('crypto')

const encryptWithPublicKey = function(toEncrypt) {
  
  var publicKey = '-----BEGIN PUBLIC KEY-----****' //your public key
  
  var buffer = Buffer.from(toEncrypt, 'utf8');
  var encrypted = crypto.publicEncrypt({key:publicKey, padding : crypto.constants.RSA_PKCS1_PADDING}, buffer)
  return  encrypted.toString("base64");
  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2016-09-19
    相关资源
    最近更新 更多