1. Technology

Converting Arrays

Using Two Arrays to Generate a Third.

In a prior tutorial on Letters to Numbers we looked at how to convert text into an array of numbers with A converted to 0 and Z converted to 25. That is the first half of what we'd need to do in order to create a simple transposition cypher. In this tutorial we are going to look at the second half of that process - how to convert an array of numbers into an array of whatever we like using a pattern where the elements in a pattern array indicate what needs to be substituted for each number in creating a replacement array.

For the purpose of this example we are going to look at a very basic transposition cypher of the kind that young children learn when they first learn about secret codes. This one simply picks a code word that has no repeating letters and adds the rest of the alphabet still in alphabetical order after it with each letter substituting for the one in the same position in the pattern. The code word I am going to use for the example is "favourites" which at ten characters long and containing a "V" which is fairly close to the end of the alphabet will do a reasonably good job of scrambling any text we feed to it. (Note it doesn't matter if you usually use the American spelling for the word without the U since as long as you know exactly what the word is you are using and it doesn't contain any repeating letters the transposition will still work),.

The first thing we need to do is to define an array containing our pattern:

var pat = ['f','a','v','o','u','r','i','t','e','s', 'b','c','d','g','h','j','k','l','m','n','p','q','w','x','y','z'];

For our transposition cypher we have all the letters of the alphabet rearranged into the order that our cypher requires as the 26 elements in our array. You can of course use this pattern substitution routine to replace your array of numbers with anything at all and so the pattern array can contain numbers or objects or whatever it is that you require where your starting point is an array of numbers indicating the positions within the pattern array to extract.

Next we set up our pattern function that will take an array of numbers and the pattern array and return a third array where the numbers are replaced by the elements from the pattern array in the equivalent positions.

function pattern(inp,map) {
var out = [];
var l = inp.length;
var m = map.length;
for (var i = 0; i < l; i++) {
out[i] = map[inp[i]%m];
}
return out;
}

The first line in this function creates the new array we are going to generate from our array of numbers and pattern. The second and third lines save the lengths of the two arrays we feed in so as to avoid needing to look them up each time through the loop.

The main part of our function is a loop which processes each of the elements of our array of numbers in turn. What it basically does is to grab the element from whatever position the number from the number array says from the pattern array and copies it to the output array. The function assumes that the first array input contains numbers and the only validation that it performs on those numbers is that if a number is passed in that would reference beyond the end of the pattern array it wraps back to the start of the pattern. So 0, 26, 52 etc all would reference the first element in the pattern array.

A simple test to show how this function works in conjunction with the prior function. Let's say we start with the text string 'abcdefghij' which when we feed it through our text to numbers function will return the array [0,1,2,3,4,5,6,7,8,9]. If we then feed this array along with our transposition pattern using the secret word "favourites" into the pattern function we will get the array ['f','a','v','o','u','r','i','t','e','s'] returned from that function. We can convert this back into a text string using the array join method so that 'abcdefghij' is converted to 'favourites'. You can now feed any text string (letters only) into this and have the encoded version produced as the result.

alert(pattern(str2num('abcdefghij'),pat).join(''));

As a working example see what my name 'Stephen' gets translated into with this simple transposition cypher.(Click on my name to pop up an alert with the results).

To be able to decrypt text that uses this simple symetrical cypher we just need to set up a pattern that does the reverse of the original. For my example pattern the reverse pattern is:

var revpat = ['b','k','l','m','i','a','n','o','g','p', 'q','r','s','t','d','u','v','f','j','h','e','c','w','x','y','z'];

©2014 About.com. All rights reserved.