📄 rsa.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program applies (RSA) %
% Developed by Hiba Al-younes %
% 5th Year, Computer Engineering Department, University of Damascus %
% Information and Network Security Material %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
format long
%generate primary parameters (p & q)
maple('z1:=',randint(1,1,[12 20]));
p=maple('nextprime(z1)');
p=str2double(p);
maple('z2:=',randint(1,1,[22 30]));
q=maple('nextprime(z2)');
q=str2double(q);
%compute n=p*q.
n=p*q;
maple('n1:=',n);
% opening the file being ciphered
f=fopen('message.txt');
[msg,count]=fread(f);
fclose(f);
%compute Euler function .
Qn=Euler(n);
maple('Qn1:=',Qn);
%compute general key .
maple('z3:=',randint(1,1,30));
e=maple('nextprime(z3)');
e=str2double(e);
while(gcd(Qn,e)~=1)
maple('z3:=',randint(1,1,30));
e=maple('nextprime(z3)');
e=str2double(e);
end
maple('e1:=',e);
%%%%%%%%%%%%%%%%%%%%%%%
% Compute Cipher text %
%%%%%%%%%%%%%%%%%%%%%%%
Ciphertext=[];
for i=1:1:count
maple('msg1:=',msg(i));
c=maple('(msg1) &^(e1) mod (n1)');
c=str2double(c);
Ciphertext = [Ciphertext c];
end
Ciphertext=double(Ciphertext);
f=fopen('cipher message.txt','w');
cn=fwrite(f,Ciphertext,1);
fclose(f);
%%%%%%%%%%%%%%
% Decryption %
%%%%%%%%%%%%%%
%compute private key
d=maple('(e1) &^(-1) mod (Qn1)');
d=str2double(d);
maple('d1:=',d);
plaintext=[];
for i=1:1:count
maple('C1:=',Ciphertext(i));
m=maple('(C1) &^(d1) mod (n1)');
m=str2double(m);
plaintext=[plaintext m];
end
f=fopen('decmessage.txt','w');
fwrite(f,plaintext);
fclose(f);
open 'message.txt';
open 'cipher message.txt';
open 'decmessage.txt';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -