A Caesar Cipher Quiz Generator from a Smart ContractThe following code is designed to generate a simple Caesar cipher, and where we will take a range of words, and then move these words by a given number of places: pragma solidity ^0.8.0; contract Test { string [] words = ["glasgow", "edinburgh", "oxford","cambridge","london", "stirling", "birmingham","hull","newcastle","manchester","reading","liverpool","bristol","sheffield","leeds","leicester", "bradford","coventry","cardiff","swansea","belfast","nottingham","derby","southampton","portsmouth", "brighton","plymouth","luton1","bolton"]; function caeser(string memory data, uint8 key) pure public returns (string memory) { bytes memory shiftedData = bytes(data); for (uint i = 0; i < shiftedData.length; i++) { shiftedData[i] = shiftChar(shiftedData[i], key); } return string(shiftedData); } function shiftChar( bytes1 b1, uint8 k) pure internal returns ( bytes1) { uint8 character = uint8(b1); uint8 characterShift; if (character >= 65 && character <= 90) characterShift = 65; else if (character >= 97 && character <=122) characterShift = 97; return bytes1(((character - characterShift + k) % 26) + characterShift); } function random(uint number) private view returns(uint){ return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty, msg.sender))) % number; } function generate() public view returns (string memory ) { uint8 r=uint8(random(uint(words.length))); uint8 shift=uint8(random(26)); string memory res=caeser(words[r],shift); string memory result = string (abi.encodePacked("Can you find the UK town/city for the Caeser cipher of ", res,"\n\nThe answer is ",words[r])); return (string(result)); } } In this case, we have two public methods: generate(), and caeser(). With generate() we will generate a new cipher challenge, and with the random shift value, and to show the shifted version and the solution. With caeser() we pass in a string, and then the shift value, and will return the cipher. We can first use Remix to enter the code and compile: If successful we can startup Ganache, and run our local blockchain: Next, we can deploy our compiled smart contract: We should find that the first account will have spent some gas to create the contract: and that a contract has been created: Next, we can test: The result when we select generate() is: Can you find the UK town/city for the Caeser cipher of cjsnjohibn The answer is birmingham Now we can test the other function: The result for two shifts for “Edinburgh” is: gfkpdwtij Now, we can move to a test network. We now select “Injected Web3” and the Ropsten network, and link our Metamask wallet: We then deploy the contract to the test network: And which should then be mined and deployed: We can then go to the contract on the test network: and view it [here]: Next we must verify and publish the contract: And define things like our compiler version (0.8.0): And then paste in the code we used: and then a successful build: And showing the code [here]: We can now test on the test network: And it all works! Here is a demo: |