Skip to content

Instantly share code, notes, and snippets.

@bjtucker
Last active November 1, 2015 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjtucker/968e82aa41cd13d0df6e to your computer and use it in GitHub Desktop.
Save bjtucker/968e82aa41cd13d0df6e to your computer and use it in GitHub Desktop.
DES Initial Permutation
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #000;
}
.line {
fill: none;
stroke: #000;
stroke-width: 3px;
stroke-linecap: round;
stroke-linejoin: round;
}
.line-halo {
stroke: #000;
stroke-linecap: round;
stroke-width: 5px;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="cubehelix.js"></script>
<script>
//using code from d3 examples by Matt Bostock
//
//des encryption routines by Eugene Styer
//http://people.eku.edu/styere/Encrypt/JS-DES.html
//
Array.prototype.extend = function (other_array) {
other_array.forEach(function(v) {this.push(v)}, this);
}
var n = 64
var array = d3.range(n)
var arrays = [array,array]
var elements = d3.range(n).map(function(i) { return {value: i, indexes: []}; });
array_final=(RSA_initial_permutation(array))
array_start = array
//for(var j=0;j<8;j++){
for(var i=0;i<64;i++){
array=shift_select(array,array_final,i)
arrays.push(array)
}
arrays.push(array)
arrays.push(array_final)
arrays.push(array_final)
arrays.push(array_final)
arrays.push(array_final)
var color = d3.scale.cubehelix()
.domain([0, n / 2, n - 1])
.range([d3.hsl(-40, 1, .4), d3.hsl(60, 1, 1), d3.hsl(160, 1, .4)]);
arrays.forEach(function(array, t) {
array.forEach(function(value, i) {
elements[value].indexes.push({time: t, index: i});
});
});
var margin = {top: 20, right: 20, bottom: 20, left: 20},
strokeWidth = 6,
width = 700 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.domain(d3.range(n))
.rangePoints([0, width]);
var y = d3.scale.ordinal()
.domain(d3.range(arrays.length))
.rangePoints([0, height]);
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.index); })
.y(function(d) { return y(d.time); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "line")
.selectAll("path")
.data(elements)
.enter().append("path")
.style("stroke", function(d) { return color(d.value); })
.attr("d", function(d) { return line(d.indexes); })
.select(function() { return this.parentNode.insertBefore(this.cloneNode(false), this); })
.attr("class", "line-halo")
.style("stroke", null);
function interpolateLine(points) {
var p0 = points[0],
x0 = p0[0],
y0 = p0[1],
path = [p0];
for (var i = 1, n = points.length, p1, x1, y1; i < n; ++i) {
p1 = points[i];
x1 = p1[0];
y1 = p1[1];
path.push("V", (y1) / 10, "L", x1, ",", (y1 *9) /10, "V", y1);
x0 = x1;
y0 = y1;
}
return path.join("");
}
function shift_select(array,array_final,dest) {
array_out=[]
for(var i=0;i<array.length;i++){
if (i==dest) {
array_out.push(array_final[dest])
}
if (array[i]!=array_final[dest]) {
array_out.push(array[i])
}
}
return(array_out)
}
// initial permutation (split into left/right halves )
// since DES numbers bits starting at 1, we will ignore x[0]
var IP_perm = new Array( -1,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 );
// copy bits in a permutation
// dest = where to copy the bits to
// src = Where to copy the bits from
// perm = The order to copy/permute the bits
// note: since DES ingores x[0], we do also
function permute( dest, src, perm )
{
var i;
var fromloc;
for( i=1; i<perm.length; i++ )
{
fromloc = perm[i];
dest[i] = src[fromloc];
}
}
function RSA_initial_permutation(array) {
var n = array.length,
a =[array[0]],
array1 = new Array(n+1);
a.extend(array)
var IP_perm = new Array( -1,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 );
// handle the initial permutation
permute( array1,a, IP_perm );
array1.shift() // convert to zero-based array
return array1;
}
d3.select(self.frameElement).style("height", height + margin.top + margin.bottom + "px");
</script>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #000;
}
.line {
fill: none;
stroke: #000;
stroke-width: 6px;
stroke-linecap: round;
stroke-linejoin: round;
}
.line-halo {
stroke-linecap: butt;
stroke-width: 10px;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="cubehelix.js"></script>
<script>
//using code from d3 examples by Matt Bostock
//
//des encryption routines by Eugene Styer
//http://people.eku.edu/styere/Encrypt/JS-DES.html
//
Array.prototype.extend = function (other_array) {
other_array.forEach(function(v) {this.push(v)}, this);
}
var n = 64
var array = d3.range(n)
var arrays = [array,array]
var elements = d3.range(n).map(function(i) { return {value: i, indexes: []}; });
array=(RSA_initial_permutation(array))
arrays.push(array)
arrays.push(array)
var color = d3.scale.cubehelix()
.domain([0, n / 2, n - 1])
.range([d3.hsl(-40, 1, .4), d3.hsl(60, 1, 1), d3.hsl(160, 1, .4)]);
arrays.forEach(function(array, t) {
array.forEach(function(value, i) {
elements[value].indexes.push({time: t, index: i});
});
});
var margin = {top: 20, right: 20, bottom: 20, left: 20},
strokeWidth = 6,
width = 960 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.domain(d3.range(n))
.rangePoints([0, width]);
var y = d3.scale.ordinal()
.domain(d3.range(arrays.length))
.rangePoints([0, height]);
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.index); })
.y(function(d) { return y(d.time); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "line")
.selectAll("path")
.data(elements)
.enter().append("path")
.style("stroke", function(d) { return color(d.value); })
.attr("d", function(d) { return line(d.indexes); })
.select(function() { return this.parentNode.insertBefore(this.cloneNode(false), this); })
.attr("class", "line-halo")
.style("stroke", null);
function interpolateLine(points) {
var p0 = points[0],
x0 = p0[0],
y0 = p0[1],
path = [p0];
for (var i = 1, n = points.length, p1, x1, y1; i < n; ++i) {
p1 = points[i];
x1 = p1[0];
y1 = p1[1];
path.push("V", (y1) / 10, "L", x1, ",", (y1 *9) /10, "V", y1);
x0 = x1;
y0 = y1;
}
return path.join("");
}
// initial permutation (split into left/right halves )
// since DES numbers bits starting at 1, we will ignore x[0]
var IP_perm = new Array( -1,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 );
// copy bits in a permutation
// dest = where to copy the bits to
// src = Where to copy the bits from
// perm = The order to copy/permute the bits
// note: since DES ingores x[0], we do also
function permute( dest, src, perm )
{
var i;
var fromloc;
for( i=1; i<perm.length; i++ )
{
fromloc = perm[i];
dest[i] = src[fromloc];
}
}
function RSA_initial_permutation(array) {
var n = array.length,
a =[array[0]],
array1 = new Array(n+1);
a.extend(array)
// initial permutation (split into left/right halves )
// since DES numbers bits starting at 1, we will ignore x[0]
//array1 = [
//a[58],a[50],a[42],a[34],a[26],a[18],a[10],a[2],
//a[60],a[52],a[44],a[36],a[28],a[20],a[12],a[4],
//a[62],a[54],a[46],a[38],a[30],a[22],a[14],a[6],
//a[0],a[56],a[48],a[40],a[32],a[24],a[16],a[8],
//a[57],a[49],a[41],a[33],a[25],a[17],a[9],a[1],
//a[59],a[51],a[43],a[35],a[27],a[19],a[11],a[3],
//a[61],a[53],a[45],a[37],a[29],a[21],a[13],a[5],
//a[63],a[55],a[47],a[39],a[31],a[23],a[15],a[7]]
var IP_perm = new Array( -1,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 );
// handle the initial permutation
permute( array1,a, IP_perm );
//var fromloc
// var i=0
// for( i=0; //i<array0.length; i++ )
// {
//fromloc=IP_perm[i];
// fromloc = perm[i];
// dest[i] = src[fromloc];
//array1[i]=array0[i]
// }
array1.shift()
return array1;
}
d3.select(self.frameElement).style("height", height + margin.top + margin.bottom + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment