Task

Implement functions to encrypt and decrypt a message using the straddling checkerboard method. The checkerboard should take a 28 character alphabet (A-Z plus a full stop and an escape character) and two different numbers representing the blanks in the first row. The output will be a series of decimal digits.

Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.




 
module Straddling_checkerboard {
	function encrypt$(message$, alphabet) {
		message$=filter$(ucase$(message$),"-/,. ")
		def num1, num2
		num1=instr(alphabet#val$(0)," ")
		num2=instr(alphabet#val$(0)," ", num1+1)-1
		num1--
		escapenum=instr(alphabet#val$(2),"/")-1
		escapenum1=instr(alphabet#val$(2),".")-1
		num0=-10
		num10=num1*10-20
		num20=num2*10-30
		numbers=(escapenum+num2*10)*10
		numbers1=(escapenum1+num2*10)*10
		boardrow=each(alphabet)
		cipherkey$="0123456789" :	while boardrow :cipherkey$+=array$(boardrow) :end while
		encoded$=""
		for i=1 to len(message$)
			n=instr(cipherkey$, mid$(message$,i,1))-1
			if n<10 then
				n+=if(random(10)<5->numbers, numbers1)
			else.if n<20 then
				n+=num0
			else.if n<30 then
				n+=num10
			else
				n+=num20
			end if
			encoded$+=str$(n,"")
		next
		=encoded$
	}
	function decrypt$(encoded$, alphabet) {
		def num1, num2
		num1=instr(alphabet#val$(0)," ")
		num2=instr(alphabet#val$(0)," ", num1+1)-1
		num1--
		escapenum=instr(alphabet#val$(2),"/")-1
		escapenum1=instr(alphabet#val$(2),".")-1
		def i=1, decoded$
		j=len(encoded$)+1
		while i<j
		m=val(mid$(encoded$,i,1))
		if m=num1 then
			decoded$+=mid$(alphabet#val$(1), val(mid$(encoded$,i+1,1))+1,1)
			i++
		else.if m=num2 then
			if i+1<j then
				mm=val(mid$(encoded$,i+1,1))
				if mm=escapenum or mm=escapenum1 then
					decoded$+=mid$(encoded$,i+2,1)
					i+=2
				else
					decoded$+=mid$(alphabet#val$(2), val(mid$(encoded$,i+1,1))+1,1)
					i++
				end if
			else
				decoded$+=mid$(alphabet#val$(2), val(mid$(encoded$,i+1,1))+1,1)
				i++	
			end if
		else
			decoded$+=mid$(alphabet#val$(0), m+1,1)
		end if
		i++
		end while
		=decoded$
	}
	Module DisplayBoard (alphabet, &Doc$){
		num1=instr(alphabet#val$(0)," ")
		num2=instr(alphabet#val$(0)," ", num1+1)-1
		num1--
		escapenum=instr(alphabet#val$(2),"/")-1
		escapenum1=instr(alphabet#val$(2),".")-1
		\\ display straddling checkerboard 
		checkerboard =cons(("0123456789",),alphabet)
		labels=(" "," ",str$(num1,""), str$(num2,""))
		disp=each(checkerboard)
		while disp
			Doc$=labels#val$(disp^)+" "+array$(disp)+{
			}
		end while
	}
	Document Doc$
	Const nl$={
	}
	Function OnePad$(Message$, PadSerial, n=1) {
		x=random(!PadSerial)  ' push old seed, set the new one
		encoded$=""
		for i=1 to len(Message$)
		encoded$+=str$((val(mid$(Message$,i,1))+10+n*random(1, 10))mod 10,"")
		next i
		x=random(!) ' restore old seed
		=encoded$
	}
	\\ select a random alphabet
	select case random(1,3)
	case 1
	alphabet=("ESTONIAR  ","BCDFGHJKLM","/PQUVWXYZ.") : Doc$="First "+nl$
	case 2
	alphabet=("ET AON RIS","BCDFGHJKLM","PQ/UVWXYZ.") : Doc$="Second"+nl$
	case 3
	alphabet=("ESTONIAR  ","BCDFGHJKLM","PQU.VWXYZ/") : Doc$="Third"+nl$
	end select
	DisplayBoard alphabet, &Doc$
	msg$="One night-it was on the twentieth of March, 1888-I was returning"
	Doc$= "original message:"+nl$
	Doc$= msg$+nl$
	Doc$= "encrypted message:"+nl$
	crypt$=encrypt$(msg$, alphabet)
	Doc$=crypt$+nl$
	serial=random(1234567,9345678)
	Doc$= "encrypted message using one pad using serial:"+str$(serial)+nl$
	crypt$=OnePad$(crypt$, serial)
	Doc$=crypt$+nl$
	Doc$= "decrypted message using one pad:"+nl$
	crypt$=OnePad$(crypt$, serial,-1)
	Doc$=crypt$+nl$
	Doc$= "decrypted message:"+nl$
	Doc$=decrypt$(crypt$, alphabet)
	Print #-2, Doc$  ' render to console using new lines codes from Doc$
	Clipboard Doc$
}
Straddling_checkerboard
 