Python and prolog encryption project based on “caesar cipher” encryption technique.

How to use:

  • in terminal: python PrologGenerator.py :> prologCode.pl
  • run prolog code via terminal(swipl prologCode.pl) or gui then use:
    • shift(a, X).
    • hideChar(a).
    • showChar(a).
    • guessChar(a).
    • hideString(test).
    • showString(nkdohg).
    • guessString(abc).
    • readFile.
    • dfs(a, X, z). continue pressing ;

for windows users: download and use python and prolog gui.

Examples:

#Ex.1 walk start from a with key = 3

?- walk(a,X,z).
X = d ;
X = g ;
X = j ;
X = m ;
X = p ;
X = s ;
X = v ;
X = y ;
X = b ;
X = e ;
X = h ;
X = k ;
X = n ;
X = q ;
X = t ;
X = w ;
X = z ;
X = c ;
false.

#Ex.2 encrypt string met3cs with key = 3

?- hideString(met3cs).
phw6fv
true.

#Ex.3 decrypt string phw6fv with key = 3

?- showString(phw6fv).
met3cs
true.

#Ex.4 encrypt char z with key = 3

?- hideChar(z).
c
true.

Files Structure:

examples
   \ input.txt
   \ prologCode.pl
PrologGenerator.py
input.txt
prologCode.pl

——-

prologCode.pl

shift(a, d).
shift(b, e).
shift(c, f).
shift(d, g).
shift(e, h).
shift(f, i).
shift(g, j).
shift(h, k).
shift(i, l).
shift(j, m).
shift(k, n).
shift(l, o).
shift(m, p).
shift(n, q).
shift(o, r).
shift(p, s).
shift(q, t).
shift(r, u).
shift(s, v).
shift(t, w).
shift(u, x).
shift(v, y).
shift(w, z).
shift(x, a).
shift(y, b).
shift(z, c).
shift(0, 3).
shift(1, 4).
shift(2, 5).
shift(3, 6).
shift(4, 7).
shift(5, 8).
shift(6, 0).
shift(7, 1).
shift(8, 2).
shift(9, 3).
shift(., *).
shift(*,.).


hideChar(X):- shift(X,Y), write(Y).
showChar(X):- shift(Y,X), write(Y).
guessChar(X):- shift(X,Y), shift(Z,X), write("maybe: "), write(Y), write("\nOR\nmaybe: "), write(Z), write('\n=========\n').


numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).
numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).
hideString(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).
showString(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).
guessString(String) :- write("maybe: "), hideString(String), write("\nOR\nmaybe: "), showString(String), write('\n=========\n').


readFile :-
open('input.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.
read_file(Stream,[]) :- at_end_of_stream(Stream).
read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(' : '), write('\n'), guessString(X), write('\n'), read_file(Stream,L).


walk(X, Z, _ORG):- shift(X,Z).
walk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).

input.txt

khaled'.
'000000'.
'a.goal'.
'without.a'.
'plan.is'.
'just.a.wish.'.

PrologGenerator.py

# Encryption and Decryption Project using Prolog and Python
# \_based on "Caesar cipher" encryption technique..
#
# Author: Khaled Alam


import string



# START Configuration(changeable): >>>>>>>>>>>>>>>>>>>>>>>>>>>

shiftingKey = 3      # << 3 default(Caesar cipher)
factName = 'shift'
bothPredicateName = 'guess'
encryptPredicateName = 'hide'
decryptPredicateName = 'show'


factLower = True
factUpper = False  # not handled
factDigits = True

inputFileName = 'input.txt'


# END Configuration(changeable): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>



# START BUILD THE FACTS BASE.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#lower chars:
if factLower is True:
	for char in string.ascii_lowercase:
		if ord(char) + shiftingKey > ord('z'):
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey - 26) + ').'
		else:
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey) + ').'

#upper chars:
if factUpper is True:
	for char in string.ascii_uppercase:
		if ord(char) + shiftingKey > ord('Z'):
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey - 26) + ').'
		else:
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey) + ').'
#digits:
if factDigits is True:
	for digit in range(10):
	    print factName + '(' + str(digit) + ', ' + str((digit + shiftingKey) % 9) + ').'


print('shift(., *).\nshift(*,.).')


# END BUILD THE FACTS BASE.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')


# START BUILD BASE FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


# >>> 1 CHAR/DIGIT <<<<<<<<<<<<<<<<<<

#encrypt 1 char or digit:
print(encryptPredicateName + 'Char(X):- ' + factName + '(X,Y), write(Y).')

#decrypt 1 char or digit:
print(decryptPredicateName + 'Char(X):- ' + factName + '(Y,X), write(Y).')

#print both guesses of 1 char or digit:
print(bothPredicateName + 'Char(X):- shift(X,Y), shift(Z,X), write(\"maybe: \"), write(Y), '+
'write(\"\\nOR\\nmaybe: \"), write(Z), write(\'\\n=========\\n\').')


print('\n')


# >>>    STRINGS   <<<<<<<<<<<<<<<<<<

print('numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).')
print('numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).')

#encrypt string:
print(encryptPredicateName + 'String(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).')

#decrypt string:
print(decryptPredicateName + 'String(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).')

#print both guesses of string:
print(bothPredicateName + 'String(String) :- write(\"maybe: \"), hideString(String), write(\"\\nOR\\nmaybe: \"),'+
' showString(String), write(\'\\n=========\\n\').')

# END BUILD BASE FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')



# START BUILD EXTRA FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


#Read from file
print('readFile :-')
print('open(\'' + inputFileName + '\', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.')
print('read_file(Stream,[]) :- at_end_of_stream(Stream).')
print('read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(\' : \'),'+
' write(\'\\n\'), ' + bothPredicateName + 'String(X), write(\'\\n\'), read_file(Stream,L).')

print('\n')

print('walk(X, Z, _ORG):- shift(X,Z).\nwalk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).')


# END BUILD EXTRA FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')

examples/prologCode.pl

shift(a, d).
shift(b, e).
shift(c, f).
shift(d, g).
shift(e, h).
shift(f, i).
shift(g, j).
shift(h, k).
shift(i, l).
shift(j, m).
shift(k, n).
shift(l, o).
shift(m, p).
shift(n, q).
shift(o, r).
shift(p, s).
shift(q, t).
shift(r, u).
shift(s, v).
shift(t, w).
shift(u, x).
shift(v, y).
shift(w, z).
shift(x, a).
shift(y, b).
shift(z, c).
shift(0, 3).
shift(1, 4).
shift(2, 5).
shift(3, 6).
shift(4, 7).
shift(5, 8).
shift(6, 0).
shift(7, 1).
shift(8, 2).
shift(9, 3).
shift(., *).
shift(*,.).


hideChar(X):- shift(X,Y), write(Y).
showChar(X):- shift(Y,X), write(Y).
guessChar(X):- shift(X,Y), shift(Z,X), write("maybe: "), write(Y), write("\nOR\nmaybe: "), write(Z), write('\n=========\n').


numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).
numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).
hideString(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).
showString(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).
guessString(String) :- write("maybe: "), hideString(String), write("\nOR\nmaybe: "), showString(String), write('\n=========\n').


readFile :-
open('input.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.
read_file(Stream,[]) :- at_end_of_stream(Stream).
read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(' : '), write('\n'), guessString(X), write('\n'), read_file(Stream,L).


walk(X, Z, _ORG):- shift(X,Z).
walk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).

examples/input.txt

'khaled'.
'000000'.
'a.goal'.
'without.a'.
'plan.is'.
'just.a.wish.'.
Facebook Comments
Last modified: August 1, 2019