+++ /dev/null
-function Bin_Sbox =Convert_Sbox_Bin(Sbox)\r
-Bin_Sbox=zeros(256,8);\r
-for it=1:256\r
- Bin_Sbox(it,:)=byte2bit(Sbox(it));\r
- %double(dec2bin(Sbox(it),8))-48;\r
- %byte2bit\r
-end\r
-\r
-\r
+++ /dev/null
-function [RK,Sbox,Pbox]=Dynamickeygenerationnew(Secretkey,counter,strh,h)\r
-x=java.security.MessageDigest.getInstance(strh);\r
-x.update(bitxor(Secretkey,counter));\r
-DK=double(typecast(x.digest,'uint8'));\r
-M=sqrt(h*8);\r
-%%%%%%%%%%%% Creation of P-box (blocks permutation)%%%%%%%%%%%%%%%%%%%%%%%%\r
-% construction of P-box\r
-\r
-%Pbox=PermutationCat(Keyp,M);\r
-\r
-Qp=8*h;\r
-Ap=1:Qp;\r
-Keyp=reshape((DK(1:1:16)),1,[]);\r
-Keyp=mod(Keyp,Qp)+1;\r
-\r
-Pbox=supboxvector2(Ap,Qp,Keyp)\r
-\r
-\r
-\r
-% construction of S-box\r
-Q=2^8;\r
-A=1:Q;\r
-KS=reshape(DK(17:32),1,[]);\r
-Sbox=supboxvector2(A,Q,KS)-1;\r
-\r
-%%%%%%%%%%%%%%%% Construction of round keys%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \r
-r=4;\r
-X=reshape(DK(33:48),1,[]);\r
-X=double(typecast(uint8(X),'uint32'))\r
-P=reshape(DK(49:end),1,[]);\r
-P=double(typecast(uint8(P),'uint32'))\r
-N=2^32;\r
-m=ceil(h/4)\r
-RK= roundKeyiterations(X,P,N,r,Sbox,m);\r
-\r
-\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-% %%%%%%%%%%%%% creation of IV (block of h bits) can be employ for CBC, CFB, OFB, CTR mode %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \r
-% s_rk=rc4key(DK(17:1:32));\r
-% IV=reshape(prga( s_rk,h^2),h,h);\r
-% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-% %%%%%%%%%%%%%%%%%%%%creation of the diffusion matrix%%%%%%%%%%%%%%%%%%%%%%%\r
-% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-% sc2=rc4key(DK(end-16:end));\r
-% outd=prga( sc2,(h^2)/4);\r
-% A=reshape(outd,h/2,h/2);\r
-% In=eye(h/2,h/2);\r
-% if (kindd==0)\r
-% G=[A A+In; A-In A ];\r
-% else\r
-% A=mod(A,2);\r
-% G=[A bitxor(A,In) ; bitxor(A,In) A]; \r
-% end
\ No newline at end of file
+++ /dev/null
-function y = bit2byte(x)\r
-% y = bit2byte(x)\r
-% Transform a number of bits into a number of bytes.\r
-\r
-x = double(x);\r
-\r
-x_n = numel(x);\r
-if rem(x_n,8)\r
- x = [x zeros(1,8-rem(x_n,8))];\r
-end\r
-y = zeros(1,ceil(x_n/8));\r
-y_index = 1;\r
-v = 2.^(0:7);\r
-for i=1:8:x_n\r
- y(y_index) = sum(x(i+7:-1:i).*v);\r
- y_index = y_index + 1;\r
-end\r
-y = uint8(y);\r
+++ /dev/null
-function y = byte2bit(x)\r
-% y = byte2bit(x)\r
-% Transform a number of bytes into a number of bits.\r
-\r
-x_n=numel(x);\r
-y=zeros(1,x_n*8);\r
-y_index=1;\r
-for i=1:x_n\r
- for j=8:-1:1\r
-% for j=1:8\r
- y(y_index+8-j) = bitget(x(i),j);\r
- end\r
- y_index = y_index + 8;\r
-end\r
-y = uint8(y);\r
+++ /dev/null
-function dimg =decryptionprocess(img,h,RK,Inv_SubBytes,Inv_Pbox,r)\r
-%%%%%%%%%%%%%% convert image to a vector seq\r
-%seq=double(reshape(img,1,[]));\r
-seq_bin=double(byte2bit(uint8(img))) ;\r
-decr_seq_binary=zeros(1,length(seq_bin));\r
-%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%%\r
-nb= floor(length(seq_bin)/(8*h));\r
-RK_bin=double(reshape(byte2bit(RK),r,[]));\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-%%%%%%%%%%% Convert S-box to binary representation to avoid repeat the\r
-%%%%%%%%%%% byte2bit conversion\r
-Bin_Sbox =double(Convert_Sbox_Bin(Inv_SubBytes));\r
-%%%%%%%%%%%%%%%%%%%% Applied the confusion process (r rounds)\r
-for it=1:nb\r
- temp=seq_bin((it-1)*8*h+1:it*h*8);\r
- for itr=r:-1:1 \r
- temp= temp(Inv_Pbox);\r
- temp=reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]);\r
- % double(byte2bit(Inv_SubBytes(double(bit2byte(temp))+1)));\r
- %\r
-% size(temp)\r
- %byte2bit(Inv_SubBytes(double(bit2byte(temp))+1));\r
- %temp= Inv_SubBytes(temp+1);\r
- temp=bitxor(temp,RK_bin(itr,1:h*8)); \r
- end\r
- decr_seq_binary((it-1)*8*h+1:it*h*8)=temp;\r
-end\r
-\r
-% Convert to Byte representation\r
-\r
-%valbin=reshape(valbin,Tb/8,8);\r
-dimg=uint8(bit2byte(decr_seq_binary));
\ No newline at end of file
+++ /dev/null
-function dimg =decryptionprocess2_CBC(img,h,RK,Inv_SubBytes,Inv_Pbox,r,IV)\r
-%%%%%%%%%%%%%% convert image to a vector seq\r
-%seq=double(reshape(img,1,[]));\r
-seq_bin=double(byte2bit(uint8(img))) ;\r
-decr_seq_binary=zeros(1,length(seq_bin));\r
-%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%%\r
-nb= floor(length(seq_bin)/(8*h));\r
-RKB=byte2bit(RK);\r
-RKB=RKB(1:r*h*8);\r
-%RK_bin=double(reshape(byte2bit(RK),r,[]));\r
-RK_bin=double(reshape(RKB,r,[]));\r
-%RK_bin=double(reshape(byte2bit(RK),r,[]));\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-%%%%%%%%%%% Convert S-box to binary representation to avoid repeat the\r
-%%%%%%%%%%% byte2bit conversion\r
-Bin_Sbox =double(Convert_Sbox_Bin(Inv_SubBytes));\r
-\r
-iv=[];\r
-for i=1:h\r
- iv=[iv byte2bit(IV(i))];\r
-end\r
-IV=double(iv);\r
-\r
-\r
-%%%%%%%%%%%%%%%%%%%% Applied the confusion process (r rounds)\r
-for it=1:nb\r
- temp=seq_bin((it-1)*8*h+1:it*h*8);\r
- temp_IV=temp;\r
- for itr=r:-1:1 \r
- temp= temp(Inv_Pbox);\r
- temp=reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]);\r
- temp=bitxor(temp,RK_bin(itr,1:h*8)); \r
- end\r
- temp=bitxor(temp,IV);\r
- decr_seq_binary((it-1)*8*h+1:it*h*8)=temp;\r
- IV= temp_IV;\r
-end\r
-\r
-% Convert to Byte representation\r
-\r
-%valbin=reshape(valbin,Tb/8,8);\r
-dimg=uint8(bit2byte(decr_seq_binary));
\ No newline at end of file
+++ /dev/null
-function eimg =encryptionprocess(img,h,RK,SubBytes,Pbox,r)\r
-%%%%%%%%%%%%%% convert image to a vector seq\r
-%seq=double(reshape(img,1,[]));\r
-seq_bin=double(byte2bit(uint8(img))) ;\r
-encr_seq_binary=zeros(1,length(seq_bin));\r
-%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%%\r
-nb= floor(length(seq_bin)/(8*h));\r
-RK_bin=double(reshape(byte2bit(RK),r,[]));\r
-Bin_Sbox =double((Convert_Sbox_Bin(SubBytes)));\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-%%%%%%%%%%%%%%%%%%%% Applied the confusion process then the bits permutation for r rounds\r
-for it=1:nb\r
- temp=seq_bin((it-1)*8*h+1:it*8*h);\r
- for itr=1:r\r
- temp=bitxor(temp,RK_bin(itr,1:h*8)); \r
- temp= (reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]));\r
- temp= temp(Pbox);\r
- end\r
- encr_seq_binary((it-1)*8*h+1:it*h*8)=temp;\r
-end\r
-\r
-% Convert to Byte representation\r
-\r
-%valbin=reshape(valbin,Tb/8,8);\r
-eimg=(bit2byte( encr_seq_binary));
\ No newline at end of file
+++ /dev/null
-function eimg =encryptionprocess2_CBC(img,h,RK,SubBytes,Pbox,r,IV)\r
-%%%%%%%%%%%%%% convert image to a vector seq\r
-%seq=double(reshape(img,1,[]));\r
-seq_bin=double(byte2bit(uint8(img))) ;\r
-encr_seq_binary=zeros(1,length(seq_bin));\r
-%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%%\r
-nb= floor(length(seq_bin)/(8*h));\r
-RKB=byte2bit(RK);\r
-RKB=RKB(1:r*h*8);\r
-%RK_bin=double(reshape(byte2bit(RK),r,[]));\r
-RK_bin=double(reshape(RKB,r,[]));\r
-Bin_Sbox =double((Convert_Sbox_Bin(SubBytes)));\r
-%iv=zeros(1,8*h);\r
-%iv(end-h+1:end)=IV;\r
-%IV=iv;\r
-iv=[];\r
-for i=1:h\r
- iv=[iv byte2bit(IV(i))];\r
-end\r
-IV=double(iv);\r
-\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-%%%%%%%%%%%%%%%%%%%% Applied the confusion process then the bits permutation for r rounds\r
-for it=1:nb\r
- temp=seq_bin((it-1)*8*h+1:it*8*h);\r
- temp=bitxor(temp,IV);\r
- for itr=1:r\r
- temp=bitxor(temp,RK_bin(itr,1:h*8)); \r
- temp= (reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]));\r
- temp= temp(Pbox);\r
- end\r
- encr_seq_binary((it-1)*8*h+1:it*h*8)=temp;\r
- IV=temp;\r
-end\r
-\r
-% Convert to Byte representation\r
-\r
-%valbin=reshape(valbin,Tb/8,8);\r
-eimg=(bit2byte( encr_seq_binary));
\ No newline at end of file
+++ /dev/null
-function inv_perm_tabs= inverse_tables(tab)\r
-le=length(tab);\r
-inv_perm_tabs=zeros(1,le);\r
-for ir = 1 :le\r
- inv_perm_tabs(tab(ir)+1) = ir-1 ;\r
-end\r
-\r
+++ /dev/null
-function inv_perm_tabs= inverse_tables2(tab)\r
-tab2=reshape(tab,1,[]);\r
-le=length(tab2);\r
-inv_perm_tabs=zeros(1,le);\r
-for ir = 1 :le\r
- inv_perm_tabs(tab2(ir)) = ir ;\r
-end\r
-%inv_perm_tabs=reshape(inv_perm_tabs,size(tab,1),size(tab,2));\r
+++ /dev/null
-%clear all\r
-%close all\r
-%clc\r
-\r
-\r
-\r
-%%%%%%%%%%%%%%%% Input data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-[filename, pathname] = uigetfile( {'*.bmp';}, 'Pick a file');\r
-name=[ pathname filename];\r
-data=imread(name);\r
-%data=imresize(data,[64 64])\r
-\r
-h=2;% number of bytes in a block (Tb=h*8)\r
-r=4;% number of rounds\r
-sk=16 % number of byes in a block cipher\r
-strh='SHA-512';\r
-% mode=0==> ECB, \r
-% mode 1==> CBC\r
-\r
-mode=1\r
-\r
-\r
-\r
-% calculate the length of data\r
-SI=size(data);\r
-%len=SI(1)*SI(2)*SI(3);\r
-%lenBits=len*8;\r
-%data_line=reshape(double(data),1,len); \r
-\r
-figure()\r
-imshow(uint8(data));\r
-\r
-%%%%%%%%%%%%%%%%% Key generation%%%%%%%%%%%%%%%%%%%%%%%%\r
-Secretkey=floor(rand(1,sk)*256);\r
-counter=floor(rand(1,sk)*256);\r
-%%%%%%%%%%%%%%%%%%%%%%%%%Creation of cipher layer's%%%%%%%%%%%%%%%%%%%%\r
-[RK,Sbox,Pbox]=Dynamickeygenerationnew(Secretkey,counter,strh,h);\r
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Change in data block%%%%%%%%%%%%%%%%%%%%%%%% \r
-\r
-\r
-\r
-\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Encryption Process%%%%%%%%%%%%%%%%%%%%%\r
-tic\r
-if mode==0\r
-eimg =encryptionprocess2(data,h,RK,Sbox,Pbox,r);\r
-elseif mode==1\r
-IV=double(floor(rand(1,h)*256)); \r
-eimg =encryptionprocess2_CBC(data,h,RK,Sbox,Pbox,r,IV); \r
-end\r
-\r
-toc\r
-eimg2=reshape(eimg,size(data));\r
-\r
-figure()\r
-imshow(uint8(eimg2));\r
-\r
-%%%%%%%%%%%%%%%%%%%%% Decryption Side%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
-%%%%%%%%%%%%%%%%%%%% calculate the inverse substitution and diffusion\r
-%%%%%%%%%%%%%%%%%%%% layer%%%%%%%%%%%%%%%%\r
- Inv_Sbox= inverse_tables(Sbox);\r
- Inv_Pbox= inverse_tables2(Pbox);\r
- \r
- \r
- if mode==0\r
- dimg =decryptionprocess2(eimg,h,RK,Inv_Sbox,Inv_Pbox,r);\r
-elseif mode==1\r
-\r
- dimg =decryptionprocess2_CBC(eimg,h,RK,Inv_Sbox,Inv_Pbox,r,IV); \r
- end\r
-\r
-% tic\r
-% dimg =decryptionprocess2(eimg,h,RK,Inv_Sbox,Inv_Pbox,r);\r
-% toc\r
- dimg=reshape(dimg,size(data));\r
- figure()\r
- imshow(uint8(dimg));\r
- \r
+++ /dev/null
-function RK=roundKeyiterations (X,P, N,r,Sbox,m)\r
-% DIfferent size of input block\r
-%m=1;\r
-RK=zeros(r,4*m);\r
-for itr=1:r\r
- for itm=1:m\r
- y=skewtententierperturbed(X(itr),P(itr),N)\r
- %Y(i)=double(typecast(y,'uint8'))\r
- op=double(typecast(uint32(y),'uint8'))\r
- opd(1)=bitxor(bitxor(op(1),op(2)),op(3));\r
- opd(2)=bitxor(bitxor(op(1),op(2)),op(4));\r
- opd(3)=bitxor(bitxor(op(1),op(4)),op(3));\r
- opd(4)=bitxor(bitxor(op(4),op(2)),op(3));\r
- opd=Sbox(opd+1);\r
- RK(itr,(itm-1)*4+1:itm*4)=opd(:);\r
- X(itr)=y;\r
- end\r
-end
\ No newline at end of file
+++ /dev/null
-function y=skewtententierperturbed(x,p,Q)\r
-% x : initial condition\r
-% p: control parameter\r
-% N:precision\r
-if x<=p\r
- y=ceil(Q*(x/p));\r
- else \r
- y=floor(Q*((Q-x)/(Q-p)))+1;\r
-end\r
+++ /dev/null
-function x=supboxvector2(x,Q,p)\r
-% r=1 for each parameters\r
-y=zeros(1,Q);\r
-for i=1:length(p)\r
-%for w=1:r\r
-dinf=find (x<=p(1,i));\r
-dsup=find (x>p(1,i));\r
-y(dinf)=ceil((Q*x(dinf))./p(1,i));\r
-y(dsup)=floor((Q*(Q-x(dsup)))./(Q-p(1,i)))+1; \r
-x=y;\r
-%end\r
-end\r