|
379 | 379 | } |
380 | 380 |
|
381 | 381 | ModeOfOperationECB.prototype.encrypt = function(plaintext) { |
382 | | - return this._aes.encrypt(plaintext); |
| 382 | + if ((plaintext.length % 16) !== 0) { |
| 383 | + throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); |
| 384 | + } |
| 385 | + |
| 386 | + var ciphertext = createBuffer(plaintext.length); |
| 387 | + var block = createBuffer(16); |
| 388 | + |
| 389 | + for (var i = 0; i < plaintext.length; i += 16) { |
| 390 | + copyBuffer(plaintext, block, 0, i, i + 16); |
| 391 | + block = this._aes.encrypt(block); |
| 392 | + copyBuffer(block, ciphertext, i, 0, 16); |
| 393 | + } |
| 394 | + |
| 395 | + return ciphertext; |
383 | 396 | } |
384 | 397 |
|
385 | | - ModeOfOperationECB.prototype.decrypt = function(ciphertext, encoding) { |
386 | | - return this._aes.decrypt(ciphertext); |
| 398 | + ModeOfOperationECB.prototype.decrypt = function(ciphertext) { |
| 399 | + if ((ciphertext.length % 16) !== 0) { |
| 400 | + throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); |
| 401 | + } |
| 402 | + |
| 403 | + var plaintext = createBuffer(ciphertext.length); |
| 404 | + var block = createBuffer(16); |
| 405 | + |
| 406 | + for (var i = 0; i < ciphertext.length; i += 16) { |
| 407 | + copyBuffer(ciphertext, block, 0, i, i + 16); |
| 408 | + block = this._aes.decrypt(block); |
| 409 | + copyBuffer(block, plaintext, i, 0, 16); |
| 410 | + } |
| 411 | + |
| 412 | + return plaintext; |
387 | 413 | } |
388 | 414 |
|
389 | 415 |
|
|
411 | 437 | } |
412 | 438 |
|
413 | 439 | ModeOfOperationCBC.prototype.encrypt = function(plaintext) { |
414 | | - if (plaintext.length != 16) { |
415 | | - throw new Error('invalid plaintext size (must be 16 bytes)'); |
| 440 | + if ((plaintext.length % 16) !== 0) { |
| 441 | + throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); |
416 | 442 | } |
417 | 443 |
|
418 | | - var precipherblock = createBuffer(plaintext); |
419 | | - for (var i = 0; i < 16; i++) { |
420 | | - precipherblock[i] ^= this._lastCipherblock[i]; |
421 | | - } |
| 444 | + var ciphertext = createBuffer(plaintext.length); |
| 445 | + var block = createBuffer(16); |
| 446 | + |
| 447 | + for (var i = 0; i < plaintext.length; i += 16) { |
| 448 | + copyBuffer(plaintext, block, 0, i, i + 16); |
| 449 | + |
| 450 | + for (var j = 0; j < 16; j++) { |
| 451 | + block[j] ^= this._lastCipherblock[j]; |
| 452 | + } |
422 | 453 |
|
423 | | - this._lastCipherblock = this._aes.encrypt(precipherblock); |
| 454 | + this._lastCipherblock = this._aes.encrypt(block); |
| 455 | + copyBuffer(this._lastCipherblock, ciphertext, i, 0, 16); |
| 456 | + } |
424 | 457 |
|
425 | | - return this._lastCipherblock; |
| 458 | + return ciphertext; |
426 | 459 | } |
427 | 460 |
|
428 | 461 | ModeOfOperationCBC.prototype.decrypt = function(ciphertext) { |
429 | | - if (ciphertext.length != 16) { |
430 | | - throw new Error('invalid ciphertext size (must be 16 bytes)'); |
| 462 | + if ((ciphertext.length % 16) !== 0) { |
| 463 | + throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); |
431 | 464 | } |
432 | 465 |
|
433 | | - var plaintext = this._aes.decrypt(ciphertext); |
434 | | - for (var i = 0; i < 16; i++) { |
435 | | - plaintext[i] ^= this._lastCipherblock[i]; |
436 | | - } |
| 466 | + var plaintext = createBuffer(ciphertext.length); |
| 467 | + var block = createBuffer(16); |
| 468 | + |
| 469 | + for (var i = 0; i < ciphertext.length; i += 16) { |
| 470 | + copyBuffer(ciphertext, block, 0, i, i + 16); |
| 471 | + block = this._aes.decrypt(block); |
437 | 472 |
|
438 | | - copyBuffer(ciphertext, this._lastCipherblock); |
| 473 | + for (var j = 0; j < 16; j++) { |
| 474 | + plaintext[i + j] = block[j] ^ this._lastCipherblock[j]; |
| 475 | + } |
| 476 | + |
| 477 | + copyBuffer(ciphertext, this._lastCipherblock, 0, i, i + 16); |
| 478 | + } |
439 | 479 |
|
440 | 480 | return plaintext; |
441 | 481 | } |
|
647 | 687 | ModeOfOperationCTR.prototype.decrypt = ModeOfOperationCTR.prototype.encrypt; |
648 | 688 |
|
649 | 689 |
|
650 | | - // The bsic modes of operation as a map |
| 690 | + // The basic modes of operation as a map |
651 | 691 | var ModeOfOperation = { |
652 | 692 | ecb: ModeOfOperationECB, |
653 | 693 | cbc: ModeOfOperationCBC, |
|
0 commit comments