mode-ecb.js 1012 B

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