6

I have encrypted string using algorithm RSA/ECB/PKCS1Padding through Java code now the same need to be encrypted using node.js. I don't know how to encrypt through node.js using algorithm RSA/ECB/PKCS1Padding . Any suggestions? the Java code is:

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");
}
2
  • Any news on this? Commented Nov 10, 2017 at 13:39
  • Any answers on this? Commented Nov 22, 2018 at 7:48

1 Answer 1

2

node js code using the cryto library:

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");
  
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.