Browse Source

CBC decrypt

patch-2
Ryan X. Charles 11 years ago
parent
commit
73b904ce21
  1. 16
      lib/expmt/cbc.js
  2. 67
      test/test.cbc.js

16
lib/expmt/cbc.js

@ -43,8 +43,20 @@ CBC.encrypt = function(messagebuf, ivbuf, blockcipher, cipherkeybuf) {
var blocksize = ivbuf.length * 8;
var blockbufs = CBC.buf2blockbufs(messagebuf, blocksize);
var encbufs = CBC.encryptblocks(blockbufs, ivbuf, blockcipher, cipherkeybuf);
var enc = Buffer.concat(encbufs);
return enc;
var encbuf = Buffer.concat(encbufs);
return encbuf;
};
CBC.decrypt = function(encbuf, ivbuf, blockcipher, cipherkeybuf) {
var blocksize = ivbuf.length * 8;
var bytesize = ivbuf.length;
var encbufs = [];
for (var i = 0; i < encbuf.length / bytesize; i++) {
encbufs.push(encbuf.slice(i * bytesize, i * bytesize + bytesize));
}
var blockbufs = CBC.decryptblocks(encbufs, ivbuf, blockcipher, cipherkeybuf);
var buf = CBC.blockbufs2buf(blockbufs, blocksize);
return buf;
};
CBC.encryptblock = function(blockbuf, ivbuf, blockcipher, cipherkeybuf) {

67
test/test.cbc.js

@ -76,6 +76,73 @@ describe('CBC', function() {
});
describe('@encrypt', function() {
it('should return this known value', function() {
var messagebuf1 = new Buffer(128 / 8);
messagebuf1.fill(0);
var messagebuf2 = new Buffer(128 / 8);
messagebuf2.fill(0x10);
var messagebuf = Buffer.concat([messagebuf1, messagebuf2]);
var ivbuf = new Buffer(128 / 8);
ivbuf.fill(0x10);
var cipherkeybuf = new Buffer(128 / 8);
cipherkeybuf.fill(0);
var blockcipher = {};
blockcipher.encrypt = function(messagebuf, cipherkeybuf) {
return messagebuf;
};
blockcipher.decrypt = function(messagebuf, cipherkeybuf) {
return messagebuf;
};
var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf);
var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf);
});
it('should return this shorter known value', function() {
var messagebuf1 = new Buffer(128 / 8);
messagebuf1.fill(0);
var messagebuf2 = new Buffer(120 / 8);
messagebuf2.fill(0x10);
var messagebuf = Buffer.concat([messagebuf1, messagebuf2]);
var ivbuf = new Buffer(128 / 8);
ivbuf.fill(0x10);
var cipherkeybuf = new Buffer(128 / 8);
cipherkeybuf.fill(0);
var blockcipher = {};
blockcipher.encrypt = function(messagebuf, cipherkeybuf) {
return messagebuf;
};
blockcipher.decrypt = function(messagebuf, cipherkeybuf) {
return messagebuf;
};
var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf);
var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf);
});
it('should return this shorter known value', function() {
var messagebuf1 = new Buffer(128 / 8);
messagebuf1.fill(0);
var messagebuf2 = new Buffer(136 / 8);
messagebuf2.fill(0x10);
var messagebuf = Buffer.concat([messagebuf1, messagebuf2]);
var ivbuf = new Buffer(128 / 8);
ivbuf.fill(0x10);
var cipherkeybuf = new Buffer(128 / 8);
cipherkeybuf.fill(0);
var blockcipher = {};
blockcipher.encrypt = function(messagebuf, cipherkeybuf) {
return messagebuf;
};
blockcipher.decrypt = function(messagebuf, cipherkeybuf) {
return messagebuf;
};
var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf);
var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf);
});
});
describe('@encryptblock', function() {
it('should return this known value', function() {

Loading…
Cancel
Save