import * as crypto from 'crypto' ; import { decrypt as pkcs7Decrypt } from './pkcs7' ; export type CipherDecrypt = ( encryptedMessage : string ) => { message : string ; receiveId : string ; } ; export const createCipherDecrypt = ( { aesKey , iv } ) : CipherDecrypt => ( encryptedMessage ) => { const base64DecodedMessage = Buffer . from ( encryptedMessage , 'base64' ) ; const cipher = crypto . createDecipheriv ( 'aes-256-cbc' , aesKey , iv ) . setAutoPadding ( false ) ; const aesDecryptedMessage = pkcs7Decrypt ( Buffer . concat ( [ cipher . update ( base64DecodedMessage ) , cipher . final ( ) ] ) ) ; const messageLength = aesDecryptedMessage . slice ( 16 , 20 ) . readUIntBE ( 0 , 4 ) ; const message = aesDecryptedMessage . slice ( 20 , 20 + messageLength ) . toString ( ) ; const receiveId = aesDecryptedMessage . slice ( 20 + messageLength ) . toString ( ) ; return { message , receiveId , } ; } ;