rsa.pl : This Program implements the RSA Encryption Algorithm. This Program implements the RSA Encryption Algorithm. It calculates the private and public keys, encrypt numbers and letters and decrypt an encoded string back to ASCII values.
How to use: For calculating the public and private Keys you have to give: Two prime numbers X and Y in the form: prime(X,Y) A coprime number to the product (X-1)(Y-1) in the form: possibleCoprime(X,Y) To encrypt you need to know the two public keys X and Y and a String S in the form abcd or a list of numbers of the for[N1,N2,N3]: puK1(X), puK2(Y),plainText(S). For Decoding you have to know the second private Key Z and the public Key X, and L, a list of numbers: prK2(Z), puK1(X), encText(L).
Program: Change the code, then submit! /* rsa.pl: This Program implements the RSA Encryption Algorithm. (C)Charlotte Gloger, 19. Juni 2009 This program is distributed under the terms of the GNU General Public License: http://www.gnu.org/licenses/gpl.html %% DESCRIPTION This Program implements the RSA Encryption Algorithm. It calculates the private and public keys, encrypt numbers and letters and decrypt an encoded string back to ASCII values. %% HOW TO USE For calculating the 'public and private Keys' you have to give:# Two 'prime numbers X and Y' in the form: *prime(X,Y)*# A 'coprime number' to the product (X-1)(Y-1) in the form: *possibleCoprime(X,Y)*# To 'encrypt' you need to know the two public keys X and Y and a String S in the form abcd or a list of numbers of the for[N1,N2,N3]: *puK1(X), puK2(Y),plainText(S)*.# For 'Decoding' you have to know the second private Key Z and the public Key X, and L, a list of numbers: *prK2(Z), puK1(X), encText(L)*.# %% SEE ALSO %% SAMPLE QUERIES %generating the public keys Q: prime(11,13),possibleCoprime(7). A: prK1(120), prK2(103), puK1(143), puK2(7). %encoding with given public keys Q: puK1(143),puK2(11), plainText([97,98,99,100,101]). A: 141,54,44,133,134,yes. %decoding with given public and private key Q: prK2(131), puK1(143),encText([141,54,44,133,134]). A: 97,98,99,100,101,yes. */ :- use_module(library(chr)). :- chr_constraint prime/2, possibleCoprime/1, prK1/1, prK2/1, puK1/1, puK2/1, gcd/1, euclidean/5, exp/4, coprime/2, encText/1, plainText/1. % Generating Keys Reading in Primes and another Number which has only trival common divisors with M prime(X,Y), possibleCoprime(A) <=> N is(X*Y), puK1(N), M is((X-1)*(Y-1)), prK1(M), gcd(A),gcd(M),coprime(A,M). % test if common divisor is trivial, which means that puK2 and prK1 are coprimes % this is a requirment for the possible Coprime Number A coprime(A,PrK1), gcd(0), gcd(_) <=> nl,write(A),write(' and '),write(PrK1),write(' have a non trival GCD!'),nl, write('Please choose a different number for possibleCoprime.'). coprime(PuK2,PrK1), gcd(1), gcd(_) <=> puK2(PuK2), euclidean(PrK1,PuK2,_,_,Y), PrK2 is PrK1+Y, prK2(PrK2). coprime(_,_), gcd(A)\ gcd(M) <=> 1 < A, A =< M | L is (M mod A), gcd(L). % Extended Euclidean algorithm % solves the equation: D = A*X+B*Y % for D = 1 and A=M=(prime1-1)*(prime2-1)=prK1, and B=A=radom chosen number,coprime to M = puK2 % the equation has the form X*prK1+Y*puK2 = 1, X,Y integers euclidean(A,0,D,X,Y) <=> D=A, X=1, Y=0. euclidean(A,B,D,X,Y) <=> B1 is (A mod B), A1 is B, euclidean(A1,B1,D,X1,Y1), X=Y1, Y is (X1-(div(A,B))*Y1). % Encoding with public Keys puK1(_),puK2(_),plainText([]) <=> true. puK1(PuK1),puK2(PuK2)\ plainText([H|R]) <=> exp(H,PuK2,X,PuK1),write(X),nl, plainText(R). % Decoding,with puK1 and prK2 prK2(_), puK1(_), encText([]) <=> true. prK2(PrK), puK1(PuK)\ encText([H|R]) <=> exp(H,PrK,X,PuK),write(X), nl, encText(R). % Calculates H^A mod K1 = E, to mimimize the needed time for the calculation, mod K is used in every recursion step. % optimized exp func: exp(H,0,E,K) <=> E=1. exp(H,A,E,K) <=> A>0, A mod 2 =:= 0 | A1 is (div(A,2)),exp(H,A1,R1,K), E is ((R1*R1) mod K). exp(H,A,E,K) <=> A>0, A mod 2 =:= 1 | A1 is (A-1),exp(H,A1,R1,K), E is ((H*R1) mod K).
Console: Enter query or select example from below, then submit and wait for answer! % loading chrww/rsa.pl | ?- consult(...). * [H,K] - singleton variables * Approximate lines: 54-60, file: '/home/webchr/tmp/_gehCRj.pl' yes [7.397 seconds] | ?-