🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of The-F00L

The-F00L's solution

to Atbash Cipher in the CFML Track

Published at Apr 13 2021 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

Examples

  • Encoding test gives gvhg
  • Decoding gvhg gives test
  • Decoding gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt gives thequickbrownfoxjumpsoverthelazydog

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

AtbashCipherTest.cfc

component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'AtbashCipher' );
	}

	function run(){
	
		describe( "My AtbashCipher class", function(){			

			describe( 'encode', function(){

				it( 'encode yes', function(){
					expect( SUT.encode( phrase='yes' ) ).toBe( 'bvh' );
				});

				it( 'encode no', function(){
					expect( SUT.encode( phrase='no' ) ).toBe( 'ml' );
				});

				it( 'encode OMG', function(){
					expect( SUT.encode( phrase='OMG' ) ).toBe( 'lnt' );
				});

				it( 'encode spaces', function(){
					expect( SUT.encode( phrase='O M G' ) ).toBe( 'lnt' );
				});

				it( 'encode mindblowingly', function(){
					expect( SUT.encode( phrase='mindblowingly' ) ).toBe( 'nrmwy oldrm tob' );
				});

				it( 'encode numbers', function(){
					expect( SUT.encode( phrase='Testing, 1 2 3,  testing.' ) ).toBe( 'gvhgr mt123 gvhgr mt' );
				});

				it( 'encode deep thought', function(){
					expect( SUT.encode( phrase='Truth is fiction.' ) ).toBe( 'gifgs rhurx grlm' );
				});

				it( 'encode all the letters', function(){
					expect( SUT.encode( phrase='The quick brown fox jumps over the lazy dog.' ) ).toBe( 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt' );
				});

			});

			describe( 'decode', function(){

				it( 'decode exercism', function(){
					expect( SUT.decode( phrase='vcvix rhn' ) ).toBe( 'exercism' );
				});

				it( 'decode a sentence', function(){
					expect( SUT.decode( phrase='zmlyh gzxov rhlug vmzhg vkkrm thglm v' ) ).toBe( 'anobstacleisoftenasteppingstone' );
				});

				it( 'decode numbers', function(){
					expect( SUT.decode( phrase='gvhgr mt123 gvhgr mt' ) ).toBe( 'testing123testing' );
				});

				it( 'decode all the letters', function(){
					expect( SUT.decode( phrase='gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt' ) ).toBe( 'thequickbrownfoxjumpsoverthelazydog' );
				});

			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="AtbashCipherTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );
	}

}
/**
* Your implmentation of the AtbashCipher exercise
*/
component {
		letters = ['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']
		lettersRev= arrayReverse(letters);
	function encode( phrase ) {
		result='';
		cnt=0;
		phrase=lCase(phrase).split('');
		for (i = 1; i < arrayLen(phrase)+1; i++) {
			if (reFind("[A-Za-z0-9]", phrase[i])!=0) {
				if (cnt==5) {
					result&=' ';
					cnt=0;
				}
				if (isNumeric(phrase[i])) {
					result&=phrase[i];
					cnt+=1;
				}
				if(reFind("[A-Za-z]", phrase[i])!=0){
					result &= lettersRev[letters.find(phrase[i])]
					cnt+=1;
				}
			}
		}
		return result;

	}

	function decode( phrase ) {
		phrase=phrase.split('');
		result='';
		for (i = 1; i < arrayLen(phrase)+1; i++) {
			if (isNumeric(phrase[i])) {
				result&=phrase[i];
			}
			if(reFind("[A-Za-z]", phrase[i])!=0){
				result &= letters[lettersRev.find(phrase[i])]
			}
		}
		return result;
	}

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?