How to resolve the algorithm Substitution cipher step by step in the Picat programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Substitution cipher step by step in the Picat programming language
Table of Contents
Problem Statement
Substitution Cipher Implementation - File Encryption/Decryption
Encrypt an 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Substitution cipher step by step in the Picat programming language
Source code in the picat programming language
main =>
S = "The quick brown fox jumped over the lazy dog",
cypher(S,E), % encrypt
println(E),
cypher(D, E), % decrypt
println(D),
S == D,
println(ok).
cypher(O, S) :-
nonvar(O),
var(S),
sub_chars(O,S).
cypher(O, S) :-
nonvar(S),
var(O),
sub_chars(O,S).
base("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ").
subs("VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWob LkESYMTN").
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).
maplist(Goal, List1, List2) :-
maplist_(List1, List2, Goal).
maplist_([], X, _) :- X = [].
maplist_([Elem1|Tail1],
[Elem2|Tail2],
Goal) :-
call(Goal, Elem1, Elem2),
maplist_(Tail1, Tail2, Goal).
main =>
S = "The quick brown fox jumped over the lazy dog!!!",
E = encrypt(S),
println(E),
D = decrypt(E),
println(D),
D == S,
println(ok),
nl.
encrypt(L) = [EncryptMap.get(C,C) : C in L] =>
base(Base),
subs(Subs),
EncryptMap = new_map([B=S : {B,S} in zip(Base,Subs)]).
decrypt(L) = [DecryptMap.get(C,C) : C in L] =>
base(Base),
subs(Subs),
DecryptMap = new_map([S=B : {B,S} in zip(Base,Subs)]).
base("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ").
subs("VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWob LkESYMTN").
You may also check:How to resolve the algorithm Named parameters step by step in the R programming language
You may also check:How to resolve the algorithm MD5 step by step in the Frink programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the Rust programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Count the coins step by step in the Sidef programming language