Last active
October 23, 2015 11:44
-
-
Save SeqviriouM/2e75e024f9ab26f20709 to your computer and use it in GitHub Desktop.
S-DES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BitArray = require('node-bitarray'); | |
var text = BitArray.fromBinary('10110110').toJSON().reverse(); | |
var key = BitArray.fromBinary('1111010110').toJSON().reverse(); | |
var extendTransposition = [1, 4, 3, 4, 2, 1, 3, 2]; | |
var transposition = [3, 4, 2, 1]; | |
var s1 = [[0, 1, 2, 1], [2, 3, 0, 3], [2, 1, 2, 1], [3, 0, 3, 0]]; | |
var s2 = [[0, 2, 0 ,1], [0, 1, 3, 2], [3, 2, 3, 1], [0, 2, 3, 1]]; | |
var p_pryamoi = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] | |
var p_sjatie = [6, 3, 7, 4, 8, 5, 10, 9] | |
var nach = [2, 6, 3, 1, 4, 8, 5, 7] | |
var con = [4, 1, 3, 5, 7, 2, 8, 6] | |
function permutation(input, scheme) { | |
return input.map(function(item, index, arr) { | |
return arr[scheme[index]-1]; | |
}); | |
} | |
function shift(input, shiftValue) { | |
return input.map(function(item, index, arr) { | |
return arr[(index + shiftValue) % arr.length]; | |
}); | |
} | |
function size(input, scheme) { | |
return scheme.map(function (item, index) { | |
return input[item-1]; | |
}) | |
} | |
function getValueFromBlock(input, block) { | |
var copiedInput = input.slice(); | |
console.log(input); | |
var valY = new BitArray(copiedInput.splice(1, 2).reverse()).toNumber(); | |
var valX = new BitArray(copiedInput.reverse()).toNumber(); | |
console.log('valY: ', valY); | |
console.log('valX: ', valX); | |
var result = block[valX][valY]; | |
return BitArray.factory(result, 2).reverse(); | |
} | |
function initialize(key) { | |
var workingKey = key.slice(); | |
console.log(workingKey); | |
console.log('extendedTransposition: ', extendTransposition); | |
console.log('transposition: ', transposition); | |
var transpositionKey = permutation(key, p_pryamoi); | |
var L0 = transpositionKey.slice(0, 5); | |
var R0 = transpositionKey.slice(5); | |
console.log('L0: ', L0); | |
console.log('R0: ', R0); | |
var shiftL0_1 = shift(L0, 1); | |
var shiftR0_1 = shift(R0, 1); | |
console.log('shift_L0_1: ', shiftL0_1); | |
console.log('shift_R0_1: ', shiftR0_1); | |
var key1 = size(shiftL0_1.concat(shiftR0_1), p_sjatie); | |
console.log('Key1: ', key1); | |
var shiftL0_2 = shift(shiftL0_1, 2); | |
var shiftR0_2 = shift(shiftR0_1, 2); | |
console.log('shift_L0_2: ', shiftL0_1); | |
console.log('shift_R0_2: ', shiftR0_1); | |
var key2 = size(shiftL0_2.concat(shiftR0_2), p_sjatie); | |
console.log('Key2: ', key2); | |
return { key1: key1, key2: key2}; | |
} | |
function round(input, input1, key) { | |
var extendInput = size(input, extendTransposition); | |
console.log('extendInput: ', extendInput); | |
console.log('Round1 key: ', key); | |
var xorResult = BitArray.xor(extendInput, key, BitArray.octet([])); | |
console.log('XOR result: ', xorResult); | |
var L = xorResult.slice(0, 4); | |
var R = xorResult.slice(4); | |
var s1_val = getValueFromBlock(L, s1); | |
var s2_val = getValueFromBlock(R, s2); | |
console.log('s1_val: ', s1_val); | |
console.log('s2_val: ', s2_val); | |
var block_val = s1_val.concat(s2_val); | |
var valueWithTransposition = permutation(block_val, transposition); | |
console.log('valueWithTransposition: ', valueWithTransposition); | |
console.log('Input: ', input); | |
var output = BitArray.xor(valueWithTransposition, input1, BitArray.factory(0, 4)); | |
console.log('Output: ', output); | |
return output; | |
} | |
console.log('--------------CODING--------------'); | |
var keys = initialize(key); | |
var textWithPermutation = permutation(text, nach); | |
var L0 = textWithPermutation.slice(0, 4); | |
var R0 = textWithPermutation.slice(4); | |
var L1 = R0.slice(); | |
var R1 = round(L1, L0, keys.key1); | |
var R2 = R1.slice(); | |
var L2 = round(R2, L1, keys.key2); | |
var LR2 = L2.concat(R2); | |
var ciphertext = permutation(LR2, con); | |
console.log('Ciphertext: ', ciphertext); | |
console.log('-------------DECODING-------------'); | |
var textWithPermutation = permutation(ciphertext, nach); | |
var L2 = textWithPermutation.slice(0, 4); | |
var R2 = textWithPermutation.slice(4); | |
var R1 = round(R2, L2, keys.key2); | |
var L1 = R2.slice(); | |
var L0 = round(R1, L1, keys.key1); | |
var R0 = R1.slice(); | |
var LR0 = L0.concat(R0); | |
var decodedtext = permutation(LR0, con); | |
console.log('Decoded text: ', decodedtext); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment