Creating A Scrambled Alphabet Challenge Generator In Ethereum and Solidity

I am porting my cipher challenge generator onto Ethereum, and where I want to be able to call up a smart contract and return a scrambled…

Photo by Kanchanara on Unsplash

Creating A Scrambled Alphabet Challenge Generator In Ethereum and Solidity

I am porting my cipher challenge generator onto Ethereum, and where I want to be able to call up a smart contract and return a scrambled alphabet cipher. If you are interested, here’s my current one:

https://asecuritysite.com/cipher/scramb

Why? Well, I want to push myself in creating truly distributed applications and find out how smart contracts work, and their limits.

So, in the following I have created a smart contract that will take an alphabetic array of “abcde…wzyz”, and then randomly shuffle the letters to give a scrambled alphabet. A phrase is then mapped with the scrambled alphabet:

First, we enter the code into Remix and compile it:

Next, we start ganache for our local blockchain:

We can then deploy the smart contract to our newly created blockchain:

We should see that some gas has been charged to the first account:

And now we can test:

The output produced is:

Find the message from: 
wot gswsyt bg wot  eiwtyitw, tfvtrednnx ei tavdimeik wot ydikt bg dvvnerdwebif, eihbnhtf d  lsro mttvty mtkytt bg vyehdrx, dim dswotiwerdwebi.  
The mapping is: 
abcdefghijklmnopqrstuvwxyz
dzrmtgkoejqnlibvcyfwshuaxp

We can see that the first word is “wot”, and that a “w” is mapped to a “t”, an “o” is mapped to an “h”, and a “t” is mapped to an “e”. So the first word is “the”. You should be able to then show that the message is “The future of the Internet, especially in expanding the range of applications, involves a much deeper degree of privacy, and authentication.”

The scrambling process is defined as follows:

string in1="a b c d e f g h i j k l m n o p q r s t u v w x y z ";
string  [] memory in2 = in1.split(" ");                                                               string memory out="";
for (uint256 i = 0; i < 26; i++) {                                      
uint256 j=random(26,i);
string memory tmp=in2[i];
in2[i]=in2[j];
in2[j]=tmp;
}

With this, we create an array with the alphabetic characters and put into in2. Next for each character in this area, we randomly swap it with another position. After this in2 will have the scrambled alphabet mapping. We can then put it into a string (out) with:

string memory out="";
for (uint256 i = 0; i < 26; i++) {
out=string(abi.encodePacked(out, in2[i]));
}

Next, we can do the substitution:

bytes memory word=bytes(phrases[random(phrases.length,0)].lower());
bytes memory map=bytes(out);

for (uint256 i=0;i<word.length;i++)
{
if (uint8(word[i])<97) rtn=string(abi.encodePacked(rtn, word[i]));
else {
uint256 pos=uint8(word[i])-uint8(97)
rtn=string(abi.encodePacked(rtn, (map[pos])));
}
}

For this, if we have a lowercase letter (which has an ASCII integer value of greater than 96), we then remap the character from our input phrase (word).

Here are some more Ethereum applications:

https://asecuritysite.com/ethereum