crypto#createDecipheriv TypeScript Examples

The following examples show how to use crypto#createDecipheriv. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: crypto.ts    From FIDO2Client with MIT License 6 votes vote down vote up
static decrypt(key: Buffer, cipher: Buffer): Buffer {
        switch (Fido2Spec) {
            case Fido2SpecVersion.FIDO_2_1: {
                let decipher = createDecipheriv('aes-256-cbc', key, Buffer.alloc(16));
                decipher.setAutoPadding(false);
                return decipher.update(cipher);
            }
            default:
                throw new CommonFido2SpecNotImplemented();
        }
    }
Example #2
Source File: node-crypto.ts    From mtcute with GNU Lesser General Public License v3.0 6 votes vote down vote up
createAesCtr(key: Buffer, iv: Buffer, encrypt: boolean): IEncryptionScheme {
        const cipher = (
            encrypt ? createCipheriv : createDecipheriv
        )(`aes-${key.length * 8}-ctr`, key, iv)

        const update = (data: Buffer) => cipher.update(data)

        return {
            encrypt: update,
            decrypt: update,
        }
    }
Example #3
Source File: node-crypto.ts    From mtcute with GNU Lesser General Public License v3.0 6 votes vote down vote up
createAesEcb(key: Buffer): IEncryptionScheme {
        const methodName = `aes-${key.length * 8}-ecb`

        return {
            encrypt(data: Buffer) {
                const cipher = createCipheriv(methodName, key, null)
                cipher.setAutoPadding(false)
                return Buffer.concat([cipher.update(data), cipher.final()])
            },
            decrypt(data: Buffer) {
                const cipher = createDecipheriv(
                    methodName,
                    key,
                    null
                )
                cipher.setAutoPadding(false)
                return Buffer.concat([cipher.update(data), cipher.final()])
            },
        }
    }
Example #4
Source File: crypter.ts    From fivem with MIT License 6 votes vote down vote up
/**
     * @description
     * Decrypt a hash/encrypted data
     *
     * @example
     * ```ts
     * decrypt('e7b75a472b65bc4a42e7b3f788...') // Result: "bacon"
     * ```
     */
    decrypt = (hash: { iv: any; content: any }) => {
        const decipher = createDecipheriv(this.#algorithm, this.#secretKey, Buffer.from(hash.iv, "hex"));
        const decrpyted = Buffer.concat([decipher.update(Buffer.from(hash.content, "hex")), decipher.final()]);

        return decrpyted.toString();
    };
Example #5
Source File: decrypt.ts    From server with GNU General Public License v3.0 4 votes vote down vote up
decrypt: IDecryptFunction = {
  aes: {
    /**
     * Decrypt String Encrypted with AES Algorithm
     *
     * @param {string} encryptedStr - Data to be Decrypted
     * @returns {string} - Decrypted String
     */
    str: (encryptedStr: string): string => {
      const { GLOBAL_PASSPHRASE, IV } = process.env;
      if (GLOBAL_PASSPHRASE && IV) {
        const algorithm = 'aes-256-cbc';
        const [iv, secret] = [
          Buffer.from(IV, 'hex'),
          Buffer.from(GLOBAL_PASSPHRASE, 'hex'),
        ];
        const decipher = createDecipheriv(algorithm, secret, iv);
        let decryptedData = decipher.update(encryptedStr, 'hex', 'utf-8');
        decryptedData += decipher.final('utf8');
        return decryptedData;
      } else {
        throw new Error(
          'GLOBAL_PASSPHRASE, IV not found in Environment Variables, Kindly Setup',
        );
      }
    },

    /**
     * Decrypt Object Encrypted with AES Algorithm
     *
     * @param {string} encryptedStr - Data to be Decrypted
     * @returns {Object} - Decrypted Object
     */
    obj: <T>(encryptedStr: string): T => {
      const { GLOBAL_PASSPHRASE, IV } = process.env;
      if (GLOBAL_PASSPHRASE && IV) {
        const algorithm = 'aes-256-cbc';
        const [iv, secret] = [
          Buffer.from(IV, 'hex'),
          Buffer.from(GLOBAL_PASSPHRASE, 'hex'),
        ];
        const decipher = createDecipheriv(algorithm, secret, iv);
        let decryptedData = decipher.update(encryptedStr, 'hex', 'utf-8');
        decryptedData += decipher.final('utf8');
        const jsonData: ICryptoObjData<T> = JSON.parse(decryptedData);
        return jsonData.data;
      } else {
        throw new Error(
          'GLOBAL_PASSPHRASE, IV not found in Environment Variables, Kindly Setup',
        );
      }
    },
  },
  rsa: {
    /**
     * Decrypt String Encrypted with RSA Algorithm
     *
     * @param {string} encryptedStr - Data to be Decrypted
     * @returns {Promise<string>} - Decrypted String
     */
    str: async (encryptedStr: string): Promise<string> => {
      const privateKey = await Keys.findOne({ type: 'privatekey' }).exec();
      if (privateKey) {
        const pvtKey = await importJWK(privateKey.key, 'PS256');
        const { plaintext } = await compactDecrypt(encryptedStr, pvtKey);
        const decodedText = new TextDecoder().decode(plaintext);
        return decodedText;
      } else {
        throw new Error(
          'Private Key Not Available in the Database, Please Setup first, then try this',
        );
      }
    },

    /**
     * Decrypt Object Encrypted with RSA Algorithm
     *
     * @param {string} encryptedStr - Data to be Decrypted
     * @returns {Promise<Object>} - Decrypted Object
     */
    obj: async <T>(encryptedStr: string): Promise<T> => {
      const privateKey = await Keys.findOne({ type: 'privatekey' }).exec();
      if (privateKey) {
        const pvtKey = await importJWK(privateKey.key, 'PS256');
        const { plaintext } = await compactDecrypt(encryptedStr, pvtKey);
        const decodedText = new TextDecoder().decode(plaintext);
        const objectData: ICryptoObjData<T> = JSON.parse(decodedText);
        return objectData.data;
      } else {
        throw new Error(
          'Private Key Not Available in the Database, Please Setup first, then try this',
        );
      }
    },
  },
}