⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdes.m

📁 This program applies Simplified DES (S-DES) Ciphering Algorithm Developed by Maimouna Al-ammar 5t
💻 M
字号:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program applies Simplified DES (S-DES) Ciphering Algorithm   %
% Developed by Maimouna Al-ammar                                    %
% 5th Year, Computer Engineering Department, University of Damascus %
% Information and Network Security Material                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% opening the file being ciphered
f1=fopen('PlainText.txt');
% opening a new file to store ciphered text
f2=fopen('CipheredText.txt','w');
% reading file for the first time to know number of bytes
[Data1,count]=fread(f1);
fclose(f1);
% requesting ciphering key from user
key=input('key=');
if ((key>=0) && (key<=1023))   
binKey=dec2binvec(key,10);
%Generating k1 and k2 from given key
[k1,k2]=genKey(binKey);
k1=[1 0 0 0 1 1 0 0];
k2=[0 1 1 1 0 0 1 1];
% empty array where ciphered text will be stored
cipheredtext=[];
f1=fopen('PlainText.txt');
while(count~=0)
% reading one byte at once from original file
[Data]=fread(f1,1);
% converting ASCII data into binary vlaues
BD=dec2binvec(Data,8);
% Initial Permutation
IP=[];
IP=[BD(2) BD(6) BD(3) BD(1) BD(4) BD(8) BD(5) BD(7)];
% applying fk on IP result for key k1
[exor,right]=fk(IP,k1);
% swaping operation
sw=[exor right];
% applying fk on sw result for key k2
[exor1,right1]=fk(sw,k2);
fk2=[exor1 right1];
% applying inversed permutation on the fk2 result
inverseIP=[fk2(4) fk2(1) fk2(3) fk2(5) fk2(7) fk2(2) fk2(8) fk2(6)];
cipherbyte=inverseIP;
% writing ciphered byte to a special file
ciphered=bi2de(cipherbyte);
cipheredtext=[cipheredtext ciphered];
count=count-1;
end
% closing original and ciphered files
fclose(f1);
cnt=fwrite(f2,cipheredtext);
fclose(f2);

% DECIPHERING SECTION
% opening the file being deciphered
f3=fopen('CipheredText.txt');
% opening a new file to store deciphered text
f4=fopen('DecipheredText.txt','w');
% reading file for the first time to know number of bytes
[Dat1,count2]=fread(f3);
fclose(f3);
% empty array where deciphered text will be stored
decipheredtext=[];
f3=fopen('CipheredText.txt');
while(count2~=0)
% reading one byte at once from original file
[Dat]=fread(f3,1);
% converting ASCII data into binary vlaues
BD1=dec2binvec(Dat,8);
% Initial Permutation
IPc=[];
IPc=[BD1(2) BD1(6) BD1(3) BD1(1) BD1(4) BD1(8) BD1(5) BD1(7)];
% applying fk on IP result for key k1
[exorc,rightc]=fk(IPc,k2);
% swaping operation
swc=[exorc rightc];
% applying fk on sw result for key k2
[exor2c,right2c]=fk(swc,k1);
fk1c=[exor2c right2c];
% applying inversed permutation on the fk2 result
inverseIPc=[fk1c(4) fk1c(1) fk1c(3) fk1c(5) fk1c(7) fk1c(2) fk1c(8) fk1c(6)];
decipherbyte=inverseIPc;
% writing ciphered byte to a special file
decipheredbyte=binvec2dec(decipherbyte);

decipheredtext=[decipheredtext decipheredbyte];
count2=count2-1;
end
% closing original and ciphered files
fclose(f3);
cnt2=fwrite(f4,decipheredtext);
fclose(f4);
else
error('Key value must wobble between 0 and 1023 (10-bit key)!');
end
%end of program

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -