mode-ecb.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. // Electronic Code Book
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.ECB = void 0;
  5. const mode_js_1 = require("./mode.js");
  6. class ECB extends mode_js_1.ModeOfOperation {
  7. constructor(key) {
  8. super("ECB", key, ECB);
  9. }
  10. encrypt(plaintext) {
  11. if (plaintext.length % 16) {
  12. throw new TypeError("invalid plaintext size (must be multiple of 16 bytes)");
  13. }
  14. const crypttext = new Uint8Array(plaintext.length);
  15. for (let i = 0; i < plaintext.length; i += 16) {
  16. crypttext.set(this.aes.encrypt(plaintext.subarray(i, i + 16)), i);
  17. }
  18. return crypttext;
  19. }
  20. decrypt(crypttext) {
  21. if (crypttext.length % 16) {
  22. throw new TypeError("invalid ciphertext size (must be multiple of 16 bytes)");
  23. }
  24. const plaintext = new Uint8Array(crypttext.length);
  25. for (let i = 0; i < crypttext.length; i += 16) {
  26. plaintext.set(this.aes.decrypt(crypttext.subarray(i, i + 16)), i);
  27. }
  28. return plaintext;
  29. }
  30. }
  31. exports.ECB = ECB;
  32. //# sourceMappingURL=mode-ecb.js.map