disp('******************************************************************') disp('Computes x^b mod n according to the square and multiply algorithm ') disp('******************************************************************') disp(' ') disp(' ') t=input ('If you want to RSA cipher press -> 1 RSA decipher -> 0 ...'); switch(t) case {1} x=input ('Give me x= '); b=input ('Give me b= '); n=input ('Give me n= '); binary1=dec2bin(b); l=length(dec2bin(b)); z=1; for i=1:1:l z=mod(z.*z,n); itt=i; if binary1(i)=='1' z=mod(z.*x,n); end end z case{0} x=input ('Give me x= '); a=input ('Give me a= '); n=input ('Give me n= '); binary1=dec2bin(a); l=length(dec2bin(a)); z=1; for i=1:1:l z=mod(z.*z,n); itt=i; if binary1(i)=='1' z=mod(z.*x,n); end end z otherwise disp('Unknown selection') end disp('Implementation by Diomedes Kastanis')