Substitution Cipher Implementation - File Encryption/Decryption



Task

Encrypt a input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file.

This type of Encryption/Decryption scheme is often called a Substitution Cipher.



Related tasks

 Caesar cipher
 Rot-13
 Vigenère Cipher/Cryptanalysis


See also

 Wikipedia: Substitution cipher


cypher(O, S) :-
	nonvar(O),
	var(S),
	atom_chars(O,Oc),
	sub_chars(Oc,Sc),
	atom_chars(S,Sc).
cypher(O, S) :-
	nonvar(S),
	var(O),
	atom_chars(S,Sc),
	sub_chars(Oc,Sc),
	atom_chars(O,Oc).	
 
% mapping based on ADA implementation but have added space character	
base(['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',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,' ']).
subs(['V',s,c,i,'B',j,e,d,g,r,z,y,'H',a,l,v,'X','Z','K',t,'U','P',u,m,'G',f,'I',w,'J',x,q,'O','C','F','R','A',p,n,'D',h,'Q','W',o,b,' ','L',k,'E','S','Y','M','T','N']).
 
sub_chars(Original,Subbed) :-
	base(Base),
	subs(Subs),
	maplist(sub_char(Base,Subs),Original,Subbed).	
 
sub_char([Co|_],[Cs|_],Co,Cs) :- !.
sub_char([_|To],[_|Ts], Co, Cs) :- sub_char(To,Ts,Co,Cs).