本文整理汇总了TypeScript中crypto.createDecipheriv函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createDecipheriv函数的具体用法?TypeScript createDecipheriv怎么用?TypeScript createDecipheriv使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createDecipheriv函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: eciesDecrypt
eciesDecrypt(recipientPrivateKey, cipherText) {
var self = this;
// debug("recipientPrivateKey=%s", util.inspect(recipientPrivateKey));//XXX
var level = recipientPrivateKey.ecparams.keylen;
var curveName = recipientPrivateKey.curveName;
// debug("=============> %d", level);
if (this.securityLevel != level) {
throw Error("Invalid key. It's security does not match the current security level " + this.securityLevel + " " + level);
}
//cipherText = ephemeralPubKeyBytes + encryptedTokBytes + macBytes
//ephemeralPubKeyBytes = first ((384+7)/8)*2 + 1 bytes = first 97 bytes
//hmac is sha3_384 = 48 bytes or sha3_256 = 32 bytes
var Rb_len = Math.floor((level + 7) / 8) * 2 + 1;
var D_len = level >> 3;
var ct_len = cipherText.length;
if (ct_len <= Rb_len + D_len)
throw new Error("Illegal cipherText length: " + ct_len + " must be > " + (Rb_len + D_len));
var Rb = cipherText.slice(0, Rb_len); // ephemeral public key bytes
var EM = cipherText.slice(Rb_len, ct_len - D_len); // encrypted content bytes
var D = cipherText.slice(ct_len - D_len);
// debug("Rb :\n", new Buffer(Rb).toString('hex'));
// debug("EM :\n", new Buffer(EM).toString('hex'));
// debug("D :\n", new Buffer(D).toString('hex'));
var EC = elliptic.ec;
//var curve = elliptic.curves['p'+level];
var ecdsa = new EC('p' + level);
//convert bytes to usable key object
var ephPubKey = ecdsa.keyFromPublic(new Buffer(Rb, 'hex'), 'hex');
//var encPrivKey = ecdsa.keyFromPrivate(ecKeypair2.prvKeyObj.prvKeyHex, 'hex');
var privKey = ecdsa.keyFromPrivate(recipientPrivateKey.prvKeyHex, 'hex');
// debug('computing Z...', privKey, ephPubKey);
var Z = privKey.derive(ephPubKey.pub);
// debug('Z computed', Z);
// debug('secret: ', new Buffer(Z.toArray(), 'hex'));
var kdfOutput = self.hkdf(Z.toArray(), ECIESKDFOutput, null, null);
var aesKey = kdfOutput.slice(0, AESKeyLength);
var hmacKey = kdfOutput.slice(AESKeyLength, AESKeyLength + HMACKeyLength);
// debug('secret: ', new Buffer(Z.toArray(), 'hex'));
// debug('aesKey: ', new Buffer(aesKey, 'hex'));
// debug('hmacKey: ', new Buffer(hmacKey, 'hex'));
var recoveredD = self.hmac(hmacKey, EM);
debug('recoveredD: ', new Buffer(recoveredD).toString('hex'));
if (D.compare(new Buffer(recoveredD)) != 0) {
// debug("D="+D.toString('hex')+" vs "+new Buffer(recoveredD).toString('hex'));
throw new Error("HMAC verify failed");
}
var iv = EM.slice(0, IVLength);
var cipher = crypto.createDecipheriv('aes-256-cfb', new Buffer(aesKey), iv);
var decryptedBytes = cipher.update(EM.slice(IVLength));
// debug("decryptedBytes: ",new Buffer(decryptedBytes).toString('hex'));
return decryptedBytes;
}
示例2: decrypt
/**
* Decrypts the object using AES256.
*/
public decrypt(encrypted: string): string {
Logger.log.debug("AesEncryptor.decrypt: start.");
if (!this.passphrase) {
throw new EncryptionError("Encryption passphrase not available.", "AES256");
}
try {
Logger.log.debug(`AesEncryptor.decrypt: encrypted data: ${encrypted}`);
let params: EncryptionParameters = this.deriveKeyAndIV(encrypted);
let decipher: Crypto.Decipher = Crypto.createDecipheriv("aes256", params.key, params.iv);
let decrypted: string = decipher.update(params.content, "base64", "utf8");
decrypted += decipher.final("utf8");
Logger.log.debug(`AesEncryptor.decrypt: data decrypted successfully.`);
return decrypted;
} catch (e) {
Logger.log.error(`AesEncryptor.decrypt: Failed to decrypt: ${e}`);
throw new EncryptionError(`Failed to decrypt: ${e}`, "AES256");
}
}
示例3: decryptPrivateKey
function decryptPrivateKey(encPrivateKey, pubKey, encryptionKey) {
const key = Buffer.from(encryptionKey, 'hex');
const doubleHash = Buffer.from(SHA256(SHA256(pubKey)), 'hex');
const iv = doubleHash.slice(0, 16);
const decipher = crypto.createDecipheriv(algo, key, iv);
const decrypted =
decipher.update(encPrivateKey, 'hex', 'utf8') + decipher.final('utf8');
return decrypted;
}
示例4: decryptEncryptionKey
export function decryptEncryptionKey(encEncryptionKey, password) {
const password_hash = Buffer.from(SHA512(password));
const key = password_hash.slice(0, 32);
const iv = password_hash.slice(32, 48);
const decipher = crypto.createDecipheriv(algo, key, iv);
const decrypted =
decipher.update(encEncryptionKey, 'hex', 'hex' as any) + decipher.final('hex');
return decrypted;
}
示例5:
export const decryptValue = (value: string): string => {
const [type, iv, ...data] = value.split(':');
const ivBuffer = iv === 'null' ? null : Buffer.from(iv, 'hex');
const encryptedText = Buffer.from(data.join(':'), 'hex');
const decipher = crypto.createDecipheriv(type, Buffer.from(ENCRYPTION_KEY), ivBuffer);
const decrypted = decipher.update(encryptedText);
return Buffer.concat([decrypted, decipher.final()]).toString();
};
示例6: aes256GCMDecrypt
static aes256GCMDecrypt(key:Buffer, ct:Buffer) {
let decipher = crypto.createDecipheriv('aes-256-gcm', key, ct.slice(0, GCMStandardNonceSize));
decipher.setAuthTag(ct.slice(ct.length - GCMTagSize));
let dec = decipher.update(
ct.slice(GCMStandardNonceSize, ct.length - GCMTagSize).toString('hex'),
'hex', 'hex'
);
dec += decipher.final('hex');
return dec;
}
示例7: decryptString
decryptString(data: EncryptedData): string
{
const
secret = this.privKey.computeSecret(pubKeyToBuffer(data.pubKey)),
iv = Buffer.alloc(16, 0),
cipher = createDecipheriv("aes-256-cbc", secret, iv);
let decrypted = cipher.update(data.payload, "base64", "utf8");
decrypted += cipher.final("utf8");
return decrypted;
}
示例8: pbkdf2
return pbkdf2(password, salt, pbkdf2Iterations, pbkdf2KeyLen, pbkdf2Digest).then((key) => {
const iv = new Buffer(data.iv, 'base64');
const decipher = crypto.createDecipheriv(cipherAlgorithm, key, iv);
let tag = data.tag;
if (tag) {
decipher.setAuthTag(new Buffer(tag, 'base64'));
}
let value = decipher.update(data.value, 'base64', 'utf8');
value += decipher.final('utf8');
return value;
}).then(JSON.parse);
示例9: decrypt
export function decrypt(encryptedData: string, encryptKey: string) {
const encryptedBuffer = Buffer.from(encryptedData, 'base64').toString('hex')
const iv = Buffer.alloc(IV_LENGTH, encryptedBuffer.substr(0, IV_LENGTH * 2), 'hex')
const payload = Buffer.from(encryptedBuffer.substr(IV_LENGTH * 2), 'hex')
const decipher = createDecipheriv(ALGORITHM, encryptKey, iv)
const decrypted = Buffer.concat([
decipher.update(payload),
decipher.final()
])
return decrypted.toString()
}
示例10: decryptScalar
export function decryptScalar(value: any, key: Buffer, aad: string, digest: crypto.Hash | null, unencrypted: boolean) {
if (unencrypted || typeof value !== 'string') {
if (digest) {
digest.update(toBytes(value));
}
return value;
}
const valre = value.match(/^ENC\[AES256_GCM,data:(.+),iv:(.+),tag:(.+),type:(.+)\]/);
if (!valre) {
return value;
}
const encValue = Buffer.from(valre[1], 'base64');
const iv = Buffer.from(valre[2], 'base64');
const tag = Buffer.from(valre[3], 'base64');
const valtype = valre[4];
var decryptor = crypto.createDecipheriv('aes-256-gcm', key, iv);
decryptor.setAuthTag(tag);
decryptor.setAAD(Buffer.from(aad));
const cleartext = decryptor.update(encValue, undefined, 'utf8') + decryptor.final('utf8');
if (digest) {
digest.update(cleartext);
}
switch (valtype) {
case 'bytes':
return cleartext;
case 'str':
return cleartext;
case 'int':
return parseInt(cleartext, 10);
case 'float':
return parseFloat(cleartext);
case 'bool':
return cleartext === 'true';
default:
throw new SopsError("Unknown type ${type}");
}
}
示例11: PasswordError
return Promise.resolve().then(() => {
const salt = dataBuffer.slice(0, 16)
const hmacSig = dataBuffer.slice(16, 48) // 32 bytes
const cipherText = dataBuffer.slice(48)
const hmacPayload = Buffer.concat([salt, cipherText])
const keysAndIV = crypto.pbkdf2Sync(password, salt, 100000, 48, 'sha512')
const encKey = keysAndIV.slice(0, 16)
const macKey = keysAndIV.slice(16, 32)
const iv = keysAndIV.slice(32, 48)
const decipher = crypto.createDecipheriv('aes-128-cbc', encKey, iv)
let plaintext = decipher.update(cipherText).toString('hex')
plaintext += decipher.final().toString('hex')
const hmac = crypto.createHmac('sha256', macKey)
hmac.write(hmacPayload)
const hmacDigest = hmac.digest()
// hash both hmacSig and hmacDigest so string comparison time
// is uncorrelated to the ciphertext
const hmacSigHash = crypto.createHash('sha256')
.update(hmacSig)
.digest()
.toString('hex')
const hmacDigestHash = crypto.createHash('sha256')
.update(hmacDigest)
.digest()
.toString('hex')
if (hmacSigHash !== hmacDigestHash) {
// not authentic
throw new PasswordError('Wrong password (HMAC mismatch)')
}
const mnemonic = bip39.entropyToMnemonic(plaintext)
if (!bip39.validateMnemonic(mnemonic)) {
throw new PasswordError('Wrong password (invalid plaintext)')
}
return mnemonic
})
示例12: decrypt
export function decrypt(encrypted: string, opts: Options): Either<string, string> {
const components = encrypted.split('.');
if (components.length !== 3) return left('Invalid payload');
setupKeys(opts);
const iv: Buffer = B.toBuffer(components[0]);
const ciphertext = B.toBuffer(components[1]);
const hmac = B.toBuffer(components[2]);
function cleanup() {
if (iv) zeroBuffer(iv);
if (ciphertext) zeroBuffer(ciphertext);
if (hmac) zeroBuffer(hmac);
if (expectedHmac) zeroBuffer(expectedHmac);
}
// make sure IV is right length
if (iv.length !== 16) {
cleanup();
return left('invalid iv length');
}
const expectedHmac = computeHmac(iv, ciphertext, opts);
if (!timingSafeEqual(hmac, expectedHmac)) {
cleanup();
return left('invalid signature');
}
const decipher = createDecipheriv(
opts.encryptionAlgorithm as string,
opts.encryptionKey,
iv
);
let plaintext = decipher.update(ciphertext, 'binary', 'utf8');
plaintext += decipher.final('utf8');
cleanup();
return right(plaintext);
}
示例13: aes_decode
static aes_decode(encodingStr: string): string {
//使用的算法
var algorithm = 'AES-128-ECB';
//使用的密匙
var key = 'jydasai38hao1616';
//输出的格式
var outputEncoding = 'utf8';
//输入数据编码
var inputEncoding = 'base64';
//创建解密器
var decipher = crypto.createDecipheriv(algorithm, key,"");
decipher.setAutoPadding(true);
//解密数据
var data = decipher.update(encodingStr,inputEncoding,outputEncoding);
data += decipher.final(outputEncoding);
return data;
}
示例14:
const nonce = crypto.randomBytes(12);
const aad = Buffer.from('0123456789', 'hex');
const cipher = crypto.createCipheriv('aes-192-ccm', key, nonce, {
authTagLength: 16
});
const plaintext = 'Hello world';
cipher.setAAD(aad, {
plaintextLength: Buffer.byteLength(plaintext)
});
const ciphertext = cipher.update(plaintext, 'utf8');
cipher.final();
const tag = cipher.getAuthTag();
const decipher = crypto.createDecipheriv('aes-192-ccm', key, nonce, {
authTagLength: 16
});
decipher.setAuthTag(tag);
decipher.setAAD(aad, {
plaintextLength: ciphertext.length
});
const receivedPlaintext: string = decipher.update(ciphertext, null, 'utf8');
decipher.final();
}
{
const key = 'keykeykeykeykeykeykeykey';
const nonce = crypto.randomBytes(12);
const aad = Buffer.from('0123456789', 'hex');
const cipher = crypto.createCipheriv('aes-192-gcm', key, nonce);
示例15: createDeOrCipher
function createDeOrCipher(type: string, algorithm: string, password: string, iv?: Buffer): { cipher: crypto.Cipher | crypto.Decipher, iv: Buffer } {
let cipherAlgorithm = algorithm.toLowerCase();
let keyIv = SupportedCiphers[cipherAlgorithm];
if (!keyIv) {
cipherAlgorithm = 'aes-256-cfb';
keyIv = SupportedCiphers[cipherAlgorithm];
}
let key = new Buffer(password);
let keyLength = keyIv[0];
if (key.length > keyLength) key = key.slice(0, keyLength);
if (key.length < keyLength) key = new Buffer(password.repeat(keyLength / password.length + 1)).slice(0, keyLength);
iv = iv || crypto.randomBytes(keyIv[1]);
let cipher = type === 'cipher' ? crypto.createCipheriv(cipherAlgorithm, key, iv) : crypto.createDecipheriv(cipherAlgorithm, key, iv);
return { cipher, iv };
}