I'm trying to build a Rail Fence Encryptor, so I need to load a one dimensional array, which goes across, in to a two dimensional array, from top to bottom, so it the positions look like this
***1D array***
1 2 3 4 5 6 7 8 9
***2D array***
1 4 7
2 5 8
3 6 9
The code then needs to be encrypted by reading the resultant rows, thus;
147 258 369
I'm programming in the C language
More information about the Rail Fence Cipher can be found at
http://en.wikipedia.org/wiki/Rail_fence
I also need to make a decrypter, but I don't know where to start with that!
If you could help me with any part of that, I will be VERY grateful!
Thanks!
In C, how can I incrementally load the values from a one dimensional array into a two dimensional array?
The following should provide the two dimensional array lookup:
int encryptDim1[] = {1,2,3,4,5,6,7,8,9};
int encryptDim2[3][3], row, col;
for (row=0; row%26lt;3; row++)
for (col=0; col%26lt;3; col++)
encryptDim2[row][col] = encryptDim1[row*3+col];
As for a decrypter, it appears Rail Fence decoding takes more code/effort than encoding. Below is some encode/decode source code I found at the link below:
function rail_decode(t, r)
{
var o_idx = new Array((r - 1) * 2);
var out_a = new Array(r);
var i, j, k, o=0;
for (i = 0; i %26lt; o_idx.length; i ++)
{
j = (o + i) % o_idx.length;
if (j %26lt; r)
{
o_idx[i] = j;
}
else
{
o_idx[i] = (2 * (r - 1)) - j;
}
}
for (i = 0; i %26lt; out_a.length; i ++)
{
out_a[i] = 0;
}
for (i = 0; i %26lt; t.length; i ++)
{
out_a[o_idx[i % o_idx.length]] ++;
}
j = 0;
for (i = 0; i %26lt; out_a.length; i ++)
{
out_a[i] = t.slice(j, j + out_a[i]);
j += out_a[i].length;
}
j = "";
for (i = 0; i %26lt; t.length; i ++)
{
k = o_idx[i % o_idx.length];
j += out_a[k].charAt(0);
out_a[k] = out_a[k].slice(1, out_a[k].length);
}
return j;
}
function rail_encode(t, r)
{
var o_idx = new Array(r * 2 - 2);
var out_a = new Array(r);
var i, j, o=0;
for (i = 0; i %26lt; r; i ++)
{
o_idx[i] = i;
out_a[i] = ""
}
for (j = 0; j %26lt; r - 2; j ++)
{
o_idx[i + j] = i - (j + 2);
}
for (i = 0; i %26lt; t.length; i ++)
{
out_a[o_idx[o]] += t.charAt(i);
o = (o + 1) % o_idx.length
}
j = "";
for (i = 0; i %26lt; r; i ++)
{
j += out_a[i];
}
return j;
}
Reply:The answer to this is a nested for loop
for ()
{
for ()
{
}
}
That's basically all I can tell you without giving too much
Reply:It's been a while since I've done C, but I think it's similar to Java. You declare a 2D array something like this...
int[][] twoDeeArray;
This is an array of arrays. You access the positions like this...
twoDeeArray[0][0] = 1; //First position in the first array
twoDeeArray[0][1] = 2; //Second position in the first array, etc.
twoDeeArray[1][0] = X; //First position in the second array.
Gluck.
Reply:Find a way to walk through one of your arrays, and map it's form onto the other. For example, walk along the width of the 2D, going down each column one at a time, and use a variable to index your 1D array.
This means that your outer loop (the row loop) has a variable that will go from 0 to the width of your 2D array - 1, and an inside loop (the column loop) goes from 0 to the hight of your 2D - 1.
Then you could use something like this "twoD[row][column] = OneD[i++]" to put a value from the 1D into the 2D.
To decrypt, you just reverse that one part: "OneD[i++] = twoD[row][column]", and now you're taking things off in order.
Make sure that your 1D will fit into the 2D for encryption and visa-versa for decryption.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment