Creating A Scrambled Alphabet Challenge Generator In Ethereum and SoliditySo, 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: pragma solidity ^0.5.0; import "https://github.com/willitscale/solidity-util/blob/master/lib/Strings.sol"; contract Test { using Strings for string; 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 [] phrases=["The future of the Internet, especially in expanding the range of applications, involves a much deeper degree of privacy, and authentication.", "Without these the Internet cannot be properly used to replace existing applications such as in voting, finance, and so on.", "The future is thus towards data encryption which is the science of cryptographics , and provides a mechanism for two entities to communicate without any other entity being able to read their messages."]; function scrambled() view public returns (string memory) { string [] memory in2 = in1.split(" "); for (uint256 i = 0; i < 26; i++) { uint256 j=random(26,i); string memory tmp=in2[i]; in2[i]=in2[j]; in2[j]=tmp; } string memory out=""; for (uint256 i = 0; i < 26; i++) { out=string(abi.encodePacked(out, in2[i])); } bytes memory word=bytes(phrases[random(phrases.length,0)].lower()); bytes memory map=bytes(out); string memory rtn="Find the message from: "; 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]))); } } rtn=string(abi.encodePacked(rtn, "\n\nThe mapping is:\nabcdefghijklmnopqrstuvwxyz\n")); rtn=string(abi.encodePacked(rtn, out)); return string(rtn); } function random(uint number,uint i) view public returns(uint){ return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty, msg.sender,uint(i)))) % number; } } 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). |