cryptomancy-bijection
Version:
A crude implementation of a feistel cipher
60 lines (52 loc) • 1.65 kB
JavaScript
var Bij = require(".");
var test = function () {
var list_size = 256;
var key = Bij.make_key('pewpewpew');
var block_size = Bij.block_size_for(list_size);
var round_keys = Bij.make_round_keys(block_size, key);
Bij.range(0, list_size).forEach(function (n) {
console.log("%s => %s", n, Bij.to_int(Bij.encrypt_until_within_range(n, block_size, round_keys, list_size)));
});
};
var from_int = function (n) {
return new Uint8Array([
(65280 & n) >> 8,
n & 255
]);
};
var test2 = function () {
var list_size = 256;
var key = Bij.make_key('pewpewpew');
var block_size = Bij.block_size_for(list_size);
var round_keys = Bij.make_round_keys(block_size, key);
Bij.range(0, 65535).forEach(function (n) {
console.log("%s => %s", n, Bij.to_int(
Bij.encrypt(from_int(n), block_size, round_keys)
));
});
};
var test3 = function () {
var list_size = Math.pow(256, 2) / 2; //- 10; //256 * 256
var key = Bij.make_key('pewpewpew');
var block_size = Bij.block_size_for(list_size);
var round_keys = Bij.make_round_keys(block_size, key);
var map = {};
var limit = 32;
Bij.range(0, limit).forEach(function (n) {
map[n] = true;
});
Bij.range(0, 65536).forEach(function (n) {
var dec = Bij.to_int(Bij.encrypt(from_int(n), block_size, round_keys));
if (dec >= limit) { return; }
if (map[dec]) {
delete map[dec];
} else {
console.log("DUPLICATE");
}
console.log("%s => %s", n, dec);
});
console.log(map);
};
//test();
//test2();
test3();