📄 rsa.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program applies (RSA) Electronic signature Algorithm %
% Developed by Maimouna Al-ammar %
% 5th Year, Computer Engineering Department, University of Damascus %
% Information and Network Security Material %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%========================================================================
% The RSA algorithm involves three steps: key generation, encryption and
% decryption
%========================================================================
% Remove all variables, functions in the matlab Workspace to forbid any
% conflicts in values
clear;
%========================================================================
% STEP1: KEY GENERATION
%========================================================================
% 1-choosing two primary numbers p and q
%---------------------------------------
p=randint(1,1,[12 20]);
while (strcmp(isPrimary(p),'false'))
p=randint(1,1,[12 20]);
end
q=randint(1,1,[23 40]);
while (strcmp(isPrimary(q),'false'))
q=randint(1,1,[23 40]);
end
% 2-computing n=p.q
%-------------------
n=p*q;
% 3-Appplying Ular function on n
%-------------------------------
phi_n=Ular(n);
% phi_n can also be computed as follows: phi_n=(p-1).(q-1)
%--------------------------------------------------------
% 4-Choosing an integer e which satisfies two conditions:
% I- 1 <e< phi_n
% II- gcd(phi_n,e)=1
% Public key is now constituted of {e,n}
%--------------------------------------------------------
e=randint(1,1,[2 40]);
while (strcmp(isPrimary(e),'false'))
e=randint(1,1,[2 40]);
end
while(gcd(phi_n,e)~=1)
e=randint(1,1,[2 40]);
while (strcmp(isPrimary(e),'false'))
e=randint(1,1,[2 40]);
end
end
%----------------------------------------------------------
% 5-Determining an integer e which satisfies next relation:
% e.d mod phi_n=1
% and achieves: 1 <d< phi_n
% Private key is now constituted of {d,n}
%----------------------------------------------------------
maple('e:=',e);
maple('n:=',n);
maple('phi_n:=',phi_n);
d=maple('(e) &^(-1) mod (phi_n)');
d=str2double(d);
maple('d:=',d);
%========================================================================
% STEP2: ENCRYPTION
%========================================================================
% opening file being encrypted
f1=fopen('Origin.txt','r+');
% Asking for prefered number of blocks which the text will be devided to
blocks=input('number of blocks=');
%===============================================================
% Appending padding so that length of message is multiple of block size
%===============================================================
[Data,count]=fread(f1);
r=mod(count,blocks);
if r==0
fclose(f1);
else
padding=32*ones(1,blocks-r);
cnt=fwrite(f1,padding);
fclose(f1);
end
% Reading file to know new number of bytes
f1=fopen('Origin.txt');
[Data,count]=fread(f1);
fclose(f1);
% Number of bytes in each block
byteperBlock=count/blocks;
% Opening file for ciphering
f1=fopen('Origin.txt');
% Empty array where message data will be stored
M=[];
for i=1:byteperBlock:count
m=fread(f1,byteperBlock);
m=m';
M=[M;m];
end
%--------------------
% Ciphering Operation
%--------------------
% Empty array where ciphered data will be stored
cipheredData=[];
[m1,n1]=size(M);
% First for loop scans all data blocks
% rows loop: each row represents a block of data
for i=1:m1
% ciphered block is stored here at every loop
cipheredBlock=[];
% Second for loop applies ciphering on each block of data
% columns loop
for j=1:n1
maple('M:=',M(i,j));
cipheredByte=maple('(M) &^(e) mod (n)');
cipheredByte=str2double(cipheredByte);
cipheredBlock = [cipheredBlock cipheredByte];
end
cipheredData = [cipheredData; cipheredBlock];
end
cipheredData=cipheredData';
cipheredData=cipheredData(:);
cipheredData=cipheredData';
xlswrite('CipheredData.xlsx',cipheredData);
%disp('Ciphered Data:')
%disp(char(cipheredData))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -