How to use the publicEncrypt function from crypto

Find comprehensive JavaScript crypto.publicEncrypt code examples handpicked from public code repositorys.

24
25
26
27
28
29
30
31
32
33
  )),
    t.join("&")
}
// 创建加密算法
const rsapublicKeyEncode = function (data, publicKey) {
  let crypted = crypto.publicEncrypt({
    key: publicKey,
    padding: crypto.constants.RSA_PKCS1_PADDING
  }, Buffer.from(data)).toString('base64');
  return crypted;
fork icon639
star icon209
watch icon12

+ 3 other calls in file

8
9
10
11
12
13
14
15
16
17
let finalBuf = Buffer.from([])
const n = Math.ceil(buf.length / MAX_CHUNK_SIZE)
const arr = Array(n).fill(0)
const pubc = cert.pub(key)
arr.forEach((_, i) => {
  const f = crypto.publicEncrypt(
    {
      key: pubc,
      padding: crypto.constants.RSA_PKCS1_PADDING, // RSA_PKCS1_OAEP_PADDING
    },
fork icon62
star icon243
watch icon17

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
function encryptKeyInfoWithScheme(symmetricKey, options, scheme, callback) {
  const padding = scheme === 'RSA-OAEP' ? crypto.constants.RSA_PKCS1_OAEP_PADDING : crypto.constants.RSA_PKCS1_PADDING;
  const symmetricKeyBuffer = Buffer.isBuffer(symmetricKey) ? symmetricKey : Buffer.from(symmetricKey, 'utf-8');

  try {
    var encrypted = crypto.publicEncrypt({
      key: options.rsa_pub,
      padding: padding
    }, symmetricKeyBuffer);
    var base64EncodedEncryptedKey = encrypted.toString('base64');
fork icon54
star icon35
watch icon88

+ 3 other calls in file

23
24
25
26
27
28
29
30
31
32
  }
}

const wrapKey = (padding, oaepHash, { [KEYOBJECT]: keyObject }, payload) => {
  const key = keyObject.asInput ? keyObject.asInput(true) : keyObject
  return { wrapped: publicEncrypt({ key, oaepHash, padding }, payload) }
}

const unwrapKey = (padding, oaepHash, { [KEYOBJECT]: keyObject }, payload) => {
  const key = keyObject.asInput ? keyObject.asInput(false) : keyObject
fork icon279
star icon0
watch icon1

81
82
83
84
85
86
87
88
89
90
// set the public key for encrypting:
options.key = fs.readFileSync('public_key.pem', 'utf8');
// make a Buffer from the input text:
let msg = Buffer.from(someText);
// encrypt it with the public key:
let result = crypto.publicEncrypt(options, msg);

// write the encrypted message to the file:
fs.writeFileSync(someFile, result);
// clear the public key:
fork icon35
star icon30
watch icon4

14
15
16
17
18
19
20
21
22
23
    var padding = constants.RSA_PKCS1_OAEP_PADDING;
    if (options.encryptionScheme === 'pkcs1') {
        padding = constants.RSA_PKCS1_PADDING;
    }

    return crypto.publicEncrypt({
        key: options.rsaUtils.exportKey('public'),
        padding: padding
    }, buffer);
}
fork icon210
star icon0
watch icon10

119
120
121
122
123
124
125
126
127
128
  const xorBytes = Buffer.allocUnsafe(nullFinishedPwd.length);
  const seedLength = seed.length;
  for (let i = 0; i < xorBytes.length; i++) {
    xorBytes[i] = nullFinishedPwd[i] ^ seed[i % seedLength];
  }
  return crypto.publicEncrypt({ key: publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING }, xorBytes);
}

response(packet, out, opts, info) {
  const marker = packet.peek();
fork icon86
star icon308
watch icon44

28
29
30
31
32
33
34
35
36
    console.log('Decrypted RSA string ...');
    console.log(dec);
};

rsaWrapper.encrypt = (publicKey, message) => {
    let enc = crypto.publicEncrypt({
        key: publicKey,
        padding: crypto.RSA_PKCS1_OAEP_PADDING
    }, Buffer.from(message));
fork icon36
star icon63
watch icon11

391
392
393
394
395
396
397
398
399
400
401


const encryptMetadataPublicKey = async (data, publicKey) => {
    return new Promise(async (resolve, reject) => {
        derKeyToPem(publicKey).then((key) => {
            try{
                const encrypted = crypto.publicEncrypt({
                    key,
                    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
                    oaepHash: "sha512"
                }, Buffer.from(data))
fork icon24
star icon118
watch icon8

95
96
97
98
99
100
101
102
103
        'padding': constants.RSA_PKCS1_PADDING
    };
};

AES.prototype._generate_session_key = function() {
    this._session_key = crypto.publicEncrypt(this._public_key, new Buffer(this._key.toString('hex')+':'+this._iv.toString('hex')));
    this._register_keyexchange_response();
    this._connection.send('jdev/sys/keyexchange/'+this._session_key.toString('base64'));
};
fork icon23
star icon32
watch icon18

+ 3 other calls in file

106
107
108
109
110
111
112
113
114
115
}

// 3-5. Attempt to encrypt using crypto lib
try {
  data = Buffer.from(data)
  result = crypto.publicEncrypt(
    {
      key: key.handle, 
      padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
    },
fork icon16
star icon0
watch icon3

78
79
80
81
82
83
84
85
86
87
})()

/*** From rsa-encrypt.js ***/

function rsaEncrypt(publicKey, plaintext) {
    return crypto.publicEncrypt(
        {
            key: publicKey,
            padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
            oaepHash: 'sha256'
fork icon10
star icon28
watch icon7

14
15
16
17
18
19
20
21
22
23
  readStream,
  cipher,
  writeStream
)

const header = await crypto.publicEncrypt(
  {
    key: publicKey.toString(),
    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
    oaepHash: 'sha256',
fork icon1
star icon4
watch icon2

9
10
11
12
13
14
15
16
17
18
// throw an easy to debug error
function validateKeyAndCerts({ cert, key, keyFile, crtFile }) {
  let encrypted;
  try {
    // publicEncrypt will throw an error with an invalid cert
    encrypted = crypto.publicEncrypt(cert, Buffer.from('test'));
  } catch (err) {
    throw new Error(
      `The certificate "${chalk.yellow(crtFile)}" is invalid.\n${err.message}`
    );
fork icon0
star icon4
watch icon0

+ 2 other calls in file

33
34
35
36
37
38
39
40
41
42
43
function encrypt(password, scramble, key) {
  const stage1 = xorRotating(
    Buffer.from(`${password}\0`, 'utf8'),
    scramble
  );
  return crypto.publicEncrypt(key, stage1);
}


module.exports = (pluginOptions = {}) => ({ connection }) => {
  let state = 0;
fork icon0
star icon0
watch icon1

+ 2 other calls in file

120
121
122
123
124
125
126
127
128
129
let downloadFile = req.body.file;

let response = await axios.get('http://localhost:1234/publickey', {});
let serverPublicKey = response.data;

let encryptedFilename = crypto.publicEncrypt({
    key: serverPublicKey,
    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
    oaepHash: "sha256",
},
fork icon0
star icon0
watch icon0

+ 11 other calls in file

46
47
48
49
50
51
52
53
54
55
56


//RSA Encryption
function rsaEncryptToBase64(dataToBeEncrypted, publicKey) {
  //public key and padding is passed
  //encrypts the data using publicEncrypt function
  let encryptedText = crypto.publicEncrypt(
    {
      key: publicKey,
      padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
      oaepHash: "sha256",
fork icon0
star icon0
watch icon0

-1
fork icon0
star icon0
watch icon0

+ 4 other calls in file

28
29
30
31
32
33
34
35
36
37
38
39
    return keys;
}




const encryptedData = function (publicKey, data) {
    const encrypted = crypto.publicEncrypt(
        {
          key: publicKey,
          padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
          oaepHash: "sha256",
fork icon0
star icon0
watch icon0

37
38
39
40
41
42
43
44
45
46
47
48
{
  const input = 'I AM THE WALRUS';
  const bufferToEncrypt = Buffer.from(input);
  const bufferPassword = Buffer.from('password');


  let encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt);


  let decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer);
  assert.strictEqual(decryptedBuffer.toString(), input);

fork icon0
star icon0
watch icon0

+ 35 other calls in file