How to use the createDecipheriv function from crypto

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

crypto.createDecipheriv is a built-in Node.js method that creates a decipher object with the specified algorithm, key, and initialization vector (IV) for decrypting data.

190
191
192
193
194
195
196
197
198
199
// derive the original encryption key for the file
exports
  .deriveKey(mpkey, salt, CRYPTO.DEFAULTS.ITERATIONS)
  .then(dcreds => {
    try {
      let decipher = scrypto.createDecipheriv(
        CRYPTO.DEFAULTS.ALGORITHM,
        dcreds.key,
        iv
      )
fork icon74
star icon450
watch icon26

219
220
221
222
223
224
225
226
227
228
const iv = encrypted.slice(0, 12)
const encData = encrypted.slice(12)
const authTag = encData.slice(encData.byteLength - ((128 + 7) >> 3))
const ciphertext = encData.slice(0, (encData.byteLength - authTag.byteLength))

const decipher = crypto.createDecipheriv("aes-256-gcm", utf8StringToArrayBuffer(key), utf8StringToArrayBuffer(iv))

decipher.setAuthTag(authTag)

if(returnBase64){
fork icon24
star icon118
watch icon8

+ 2 other calls in file

How does crypto.createDecipheriv work?

The crypto.createDecipheriv() method is used to create a Decipher object with a specified algorithm, key and initialization vector (iv), which can be used to decrypt data encrypted with the corresponding crypto.createCipheriv() method.

3
4
5
6
7
8
9
10
11
12
13
} = require('../../../common/utils')


// 退款通知解密key=md5(key)
function decryptData (encryptedData, key, iv = '') {
  // 解密
  const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv)
  // 设置自动 padding 为 true,删除填充补位
  decipher.setAutoPadding(true)
  let decoded = decipher.update(encryptedData, 'base64', 'utf8')
  decoded += decipher.final('utf8')
fork icon49
star icon81
watch icon1

151
152
153
154
155
156
157
158
159
160
        },
    });
};

AES.prototype._decipher = function(enc_data) {
    var decipher = crypto.createDecipheriv('aes-256-cbc', this._key, this._iv);
    decipher.setAutoPadding(false);
    var data = decipher.update(enc_data,'base64','utf-8');
    data += decipher.final('utf-8');
    return data.replace(/\x00.*$/,"");
fork icon23
star icon32
watch icon18

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const crypto = require("crypto");

const algorithm = "aes-256-cbc"; // symmetric encryption algorithm
const key = Buffer.from("01234567890123456789012345678901", "hex"); // 32-byte symmetric key
const iv = Buffer.from("0123456789012345", "hex"); // 16-byte initialization vector

const encryptedData = "U2FsdGVkX19gQ+sq1/CJFVZwplnAtz+eMkX9YD7abz0="; // encrypted data in base64 format

const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decryptedData = decipher.update(encryptedData, "base64", "utf8");
decryptedData += decipher.final("utf8");

console.log(decryptedData);

In this example, we first define the symmetric encryption algorithm, the 32-byte symmetric key, and the 16-byte initialization vector. We also have a string of encrypted data in base64 format that we want to decrypt. We then create a Decipher object using crypto.createDecipheriv, passing in the algorithm, key, and initialization vector as arguments. We then use this Decipher object to decrypt the data, calling decipher.update() with the encrypted data in base64 format and the output encoding set to utf8. We then call decipher.final() to get the final block of decrypted data, again with the output encoding set to utf8. Finally, we log the decrypted data to the console.

83
84
85
86
87
88
89
90
91
92
    var cipher = nodeCrypto.createCipheriv('aes-256-cbc', key, iv);
    cipher.update(data);
    resolve(cipher.final());
  }
  else if (op === 'decrypt') {
    var decipher = nodeCrypto.createDecipheriv('aes-256-cbc', key, iv);
    decipher.update(data);
    resolve(decipher.final());
  }
}
fork icon13
star icon31
watch icon8

199
200
201
202
203
204
205
206
207
208
  if (!iv) {
    let tmp = computeKeyAndIvFromPwd('aes-256-cbc', key);
    key = Buffer.from(tmp[0], 'hex');
    iv = Buffer.from(tmp[1], 'hex');
  }
  var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  decipher.setAutoPadding(true);
  return decipher.update(data, 'base64', 'utf8') + decipher.final('utf8');
};
exports.decryptOld = function (data, key) {
fork icon12
star icon36
watch icon7

+ 2 other calls in file

115
116
117
118
119
120
121
122
123
124

function symmetricDecrypt(key, message) {
    const iv = message.slice(0, 12)
    const tag = message.slice(12, 28)
    const ciphertext = message.slice(28)
    const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv)
    decipher.setAuthTag(tag)
    const decrypted = Buffer.concat([
        decipher.update(ciphertext, 'utf8'),
        decipher.final()
fork icon10
star icon28
watch icon7

47
48
49
50
51
52
53
54
55
56
    throw new Error('Invalid data');

const iv = Buffer.from(data.substring(0, 32), 'hex');
const encrypted = data.substring(32, data.length);

const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);

return new Promise((resolve, reject) => {
    let chunk;
    let decrypted = '';
fork icon5
star icon44
watch icon2

40
41
42
43
44
45
46
47
48
49
    const encode = options.encode === "binary" ? undefined : options.encode || "base64";
    key = Buffer.isBuffer(key) ? key : typeof key == "string" ? key : String(key);
    data = Buffer.isBuffer(data) ? data : Buffer.from(typeof data == "string" ? data : String(data), encode);
    const iv = data.slice(0, options.iv_length || 16);
    const password = crypto.pbkdf2Sync(key, iv.toString(), options.key_iterations || 10000, options.key_length || 32, options.key_hash || 'sha256');
    const decipher = crypto.createDecipheriv(options.algorithm || 'aes-256-cbc', password, iv);
    var msg = Buffer.concat([decipher.update(data.slice(16)), decipher.final()]).toString("utf8");
} catch (e) {
    msg = '';
    logger.debug('decrypt:', options, e.stack);
fork icon5
star icon40
watch icon9

130
131
132
133
134
135
136
137
138
139
}

{
  if (isCCM && common.hasFipsCrypto) {
    assert.throws(() => {
      crypto.createDecipheriv(test.algo,
                              Buffer.from(test.key, 'hex'),
                              Buffer.from(test.iv, 'hex'),
                              options);
    }, errMessages.FIPS);
fork icon42
star icon19
watch icon0

+ 31 other calls in file

12
13
14
15
16
17
18
19
20
21
22
        return { sub: encodedSub, vector: encodedIv };
    }


    static decoder(data, encodedIv, key) {
        const iv = Buffer.from(encodedIv, "hex");
        const decipher = crypto.createDecipheriv(Codec.algorithm, key, iv);
        return decipher.update(data, 'hex', 'utf8') + decipher.final('utf8');
    }
}

fork icon2
star icon4
watch icon0

27
28
29
30
31
32
33
34
35
36

if (!iv) {
  throw new VSError('IV not found', 500);
}

const decipher = crypto.createDecipheriv(
  this.algorithm,
  this.key,
  Buffer.from(iv, 'hex')
);
fork icon1
star icon15
watch icon0

181
182
183
184
185
186
187
188
189
190
	}
} else if (data.protocol == 301) {
	const data2 = protocol301Parser.parse(data.payload.subarray(0, 24));
	if (endpoint.startsWith(data2.endpoint)) {
		const iv = Buffer.alloc(16, 0);
		const decipher = crypto.createDecipheriv("aes-128-cbc", nonce, iv);
		let decrypted = Buffer.concat([decipher.update(data.payload.subarray(24)), decipher.final()]);
		decrypted = zlib.gunzipSync(decrypted);
		rr.emit("response.301", duid, data2.id, decrypted);
	}
fork icon5
star icon11
watch icon5

1770
1771
1772
1773
1774
1775
1776
1777
1778
1779

let resizedIV = Buffer.allocUnsafe(16);
let iv = crypto.createHash("sha256").update("myHashedIV").digest();
iv.copy(resizedIV);
let key = crypto.createHash("sha256").update(password).digest();
let decipher = crypto.createDecipheriv("aes256", key, resizedIV);

let schoolPassword = user.schoolPassword;

let decryptedPass = decipher.update(schoolPassword, "hex", "utf8");
fork icon0
star icon6
watch icon5

1265
1266
1267
1268
1269
1270
1271
1272
1273
1274

let pwhash = crypto.createHash('md5').update(password).digest("hex");

let algorithm = 'aes-256-ctr';

const decipher = crypto.createDecipheriv(algorithm, pwhash, Buffer.from(hash.iv, 'hex'));

const decrypted = Buffer.concat([decipher.update(Buffer.from(hash.content, 'hex')), decipher.final()]);

return decrypted.toString();
fork icon1
star icon2
watch icon1

55
56
57
58
59
60
61
62
63
64
65
  value,
  key = PRIVATE_KEY,
  algo = ENC_ALGO,
  iv = INITIALIZATION_VECTOR
) => {
  const decipher = crypto.createDecipheriv(algo, key, iv);
  return decipher.update(value, "hex", "utf8") + decipher.final("utf8");
};


/**
fork icon0
star icon0
watch icon1

26
27
28
29
30
31
32
33
34
 const authTagLocation = data.length - 16;
 const ivLocation = data.length - 32;
 const authTag = data.slice(authTagLocation);
 const iv = data.slice(ivLocation, authTagLocation);
 const encrypted = data.slice(0, ivLocation);
 const decipher = crypto.createDecipheriv(readStream._algorithm, readStream._fileKey, iv);
 decipher.setAuthTag(authTag);
 return resolve(Buffer.concat([decipher.update(encrypted), decipher.final()]));
}
fork icon0
star icon0
watch icon1

+ 2 other calls in file

775
776
777
778
779
780
781
782
783
784
785
786


const decrypt = (text, password) => {
  let textParts = text.split(':');
  let iv = Buffer.from(textParts.shift(), 'hex');
  let encryptedText = Buffer.from(textParts.join(':'), 'hex');
  let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(password), iv);
  let decrypted = decipher.update(encryptedText);


  decrypted = Buffer.concat([decrypted, decipher.final()]);

fork icon0
star icon0
watch icon1

296
297
298
299
300
301
302
303
304
305
306
    return encryptedData
}


// Decrypting text
function decrypt(text) {
    const decipher = crypto.createDecipheriv(algorithm, Securitykey, initVector);
    let decryptedData = decipher.update(text, "hex", "utf-8");
    decryptedData += decipher.final("utf8");
    return decryptedData
}
fork icon0
star icon0
watch icon1

69
70
71
72
73
74
75
76
77
    const config = require('../setup/config');
    password = typeof password == 'string' ? password : config.secret;
    enc = typeof enc == 'string' ? enc : 'base64';
    const iv = Buffer.from(data.split('.')[0], enc);
    const key = scryptSync(password, iv, 32);
    const decipher = createDecipheriv('aes-256-cbc', key, iv);
    const decrypted = decipher.update(data.split('.')[1], enc, 'utf8');
    return decrypted + decipher.final('utf8');
},
fork icon0
star icon0
watch icon0