How to use the privateDecrypt function from crypto

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

32
33
34
35
36
37
38
39
40
41
let finalDec = ''
const n = Math.ceil(buf.length / BLOCK_SIZE)
const arr = Array(n).fill(0)
const privc = cert.priv(privateKey)
arr.forEach((_, i) => {
  const b = crypto.privateDecrypt(
    {
      key: privc,
      padding: crypto.constants.RSA_PKCS1_PADDING,
    },
fork icon62
star icon243
watch icon17

+ 3 other calls in file

28
29
30
31
32
33
34
35
36
37
  return { wrapped: publicEncrypt({ key, oaepHash, padding }, payload) }
}

const unwrapKey = (padding, oaepHash, { [KEYOBJECT]: keyObject }, payload) => {
  const key = keyObject.asInput ? keyObject.asInput(false) : keyObject
  return privateDecrypt({ key, oaepHash, padding }, payload)
}

module.exports = (JWA) => {
  ['RSA1_5', 'RSA-OAEP', 'RSA-OAEP-256'].forEach((jwaAlg) => {
fork icon279
star icon0
watch icon1

95
96
97
98
99
100
101
102
103
104
// read the private key:
options.key = fs.readFileSync('private_key.pem', 'utf8');
// read the encrypted file:
let input = fs.readFileSync(someFile);
// decrypt it with the private key:
let result = crypto.privateDecrypt(options, input);
// print it:
console.log(result.toString());
// clear the key:
options.key = '';
fork icon35
star icon30
watch icon4

33
34
35
36
37
38
39
40
41
42
    var padding = constants.RSA_PKCS1_OAEP_PADDING;
    if (options.encryptionScheme === 'pkcs1') {
        padding = constants.RSA_PKCS1_PADDING;
    }

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

37
38
39
40
41
42
43
44
45

    return enc.toString('base64');
};

rsaWrapper.decrypt = (privateKey, message) => {
    let enc = crypto.privateDecrypt({
        key: privateKey,
        padding: crypto.RSA_PKCS1_OAEP_PADDING
    }, Buffer.from(message, 'base64'));
fork icon36
star icon63
watch icon11

410
411
412
413
414
415
416
417
418
419
420


const decryptMetadataPrivateKey = (data, privateKey) => {
    return new Promise(async (resolve, reject) => {
        derKeyToPem(privateKey).then((key) => {
            try{
                const decrypted = crypto.privateDecrypt({
                    key,
                    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
                    oaepHash: "sha512"
                }, base64ToArrayBuffer(data))
fork icon24
star icon118
watch icon8

154
155
156
157
158
159
160
161
162
163
}

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

88
89
90
91
92
93
94
95
96
97
        },
        plaintext
    )
}
function rsaDecrypt(privateKey, message) {
    return crypto.privateDecrypt(
        {
            key: privateKey,
            padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
            oaepHash: 'sha256'
fork icon10
star icon28
watch icon7

18
19
20
21
22
23
24
25
26
27
  );
}

try {
  // privateDecrypt will throw an error with an invalid key
  crypto.privateDecrypt(key, encrypted);
} catch (err) {
  throw new Error(
    `The certificate key "${chalk.yellow(keyFile)}" is invalid.\n${
      err.message
fork icon1
star icon0
watch icon0

64
65
66
67
68
69
70
71
72
73
74


//RSA Decryption
function rsaDecryptFromBase64(encryptedData, privateKey) {
  //private key and padding is passed
  //decrypts the encrypted Data using privateDecrypt function
  let decryptedText = crypto.privateDecrypt(
    {
      key: privateKey,
      padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
      oaepHash: "sha256",
fork icon0
star icon0
watch icon0

0
1
2
3
4
5
6
7
8
9
10
var crypto = require('crypto')
if (typeof crypto.publicEncrypt !== 'function') {
  crypto = require('./browser')
}
exports.publicEncrypt = crypto.publicEncrypt
exports.privateDecrypt = crypto.privateDecrypt


if (typeof crypto.privateEncrypt !== 'function') {
  exports.privateEncrypt = require('./browser').privateEncrypt
} else {
fork icon0
star icon0
watch icon0

+ 4 other calls in file

45
46
47
48
49
50
51
52
53
54
55
      log.log("encypted data: ", encrypted.toString("base64"));
    return encrypted;
}


const decryptedData = function (privateKey, encryptedData) {
    const decrypted = crypto.privateDecrypt(
        {
          key: privateKey.toString(),
          passphrase: 'top-secret',
          // In order to decrypt the data, we need to specify the
fork icon0
star icon0
watch icon0

39
40
41
42
43
44
45
46
47
48
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);

decryptedBuffer = crypto.privateDecrypt(rsaPkcs8KeyPem, encryptedBuffer);
assert.strictEqual(decryptedBuffer.toString(), input);
fork icon0
star icon0
watch icon0

+ 59 other calls in file

69
70
71
72
73
74
75
76
77
78
    encoding: 'hex'
  }, Buffer.from(ab2enc).toString('hex'));
}

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

decryptedBuffer = crypto.privateDecrypt(rsaPkcs8KeyPem, encryptedBuffer);
fork icon0
star icon0
watch icon0

+ 13 other calls in file

0
1
2
3
4
5
6
7
8
9
10
var crypto = require('crypto');
if (typeof crypto.publicEncrypt !== 'function') {
  crypto = require('./browser');
}
exports.publicEncrypt = crypto.publicEncrypt;
exports.privateDecrypt = crypto.privateDecrypt;


if (typeof crypto.privateEncrypt !== 'function') {
  exports.privateEncrypt = require('./browser').privateEncrypt;
} else {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

3
4
5
6
7
8
9
10
11
12
const encrypted = Buffer.from(fs.readFileSync('protected.txt', 'utf8'), 'base64')

const privateKey = crypto.createPrivateKey(fs.readFileSync('private.pem', 'utf8'))

const decrypted = crypto.privateDecrypt({key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING}, Buffer.from(encrypted, 'base64'))
// const decrypted = crypto.privateDecrypt(privateKey, Buffer.from(encrypted, 'base64'))

console.log(decrypted.toString('utf8'))

// crypto.constants.RSA_PKCS1_PADDING
fork icon0
star icon0
watch icon1

+ 3 other calls in file