IGE.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const Helpers = require("../Helpers");
  2. const { IGE: aes_ige } = require("@cryptography/aes");
  3. class IGENEW {
  4. private ige: any;
  5. constructor(key: Buffer, iv: Buffer) {
  6. this.ige = new aes_ige(key, iv);
  7. }
  8. /**
  9. * Decrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector
  10. * @param cipherText {Buffer}
  11. * @returns {Buffer}
  12. */
  13. decryptIge(cipherText: Buffer) {
  14. return Helpers.convertToLittle(this.ige.decrypt(cipherText));
  15. }
  16. /**
  17. * Encrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector
  18. * @param plainText {Buffer}
  19. * @returns {Buffer}
  20. */
  21. encryptIge(plainText: Buffer) {
  22. const padding = plainText.length % 16;
  23. if (padding) {
  24. plainText = Buffer.concat([
  25. plainText,
  26. Helpers.generateRandomBytes(16 - padding),
  27. ]);
  28. }
  29. return Helpers.convertToLittle(this.ige.encrypt(plainText));
  30. }
  31. }
  32. export { IGENEW as IGE };