From: couturie Date: Sun, 30 Apr 2017 07:59:23 +0000 (+0200) Subject: adding of the chaotic cipher code X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/commitdiff_plain/dd9401058647b7c859ac99b19dab03919c766ecf?ds=sidebyside;hp=b11d630985cdd7cad65e5592b903ced900719079 adding of the chaotic cipher code --- diff --git a/Chaotic_code/Convert_Sbox_Bin.m b/Chaotic_code/Convert_Sbox_Bin.m new file mode 100644 index 0000000..d473e2e --- /dev/null +++ b/Chaotic_code/Convert_Sbox_Bin.m @@ -0,0 +1,9 @@ +function Bin_Sbox =Convert_Sbox_Bin(Sbox) +Bin_Sbox=zeros(256,8); +for it=1:256 + Bin_Sbox(it,:)=byte2bit(Sbox(it)); + %double(dec2bin(Sbox(it),8))-48; + %byte2bit +end + + diff --git a/Chaotic_code/Dynamickeygenerationnew.m b/Chaotic_code/Dynamickeygenerationnew.m new file mode 100644 index 0000000..32a3155 --- /dev/null +++ b/Chaotic_code/Dynamickeygenerationnew.m @@ -0,0 +1,53 @@ +function [RK,Sbox,Pbox]=Dynamickeygenerationnew(Secretkey,counter,strh,h) +x=java.security.MessageDigest.getInstance(strh); +x.update(bitxor(Secretkey,counter)); +DK=double(typecast(x.digest,'uint8')); +M=sqrt(h*8); +%%%%%%%%%%%% Creation of P-box (blocks permutation)%%%%%%%%%%%%%%%%%%%%%%%% +% construction of P-box + +%Pbox=PermutationCat(Keyp,M); + +Qp=8*h; +Ap=1:Qp; +Keyp=reshape((DK(1:1:16)),1,[]); +Keyp=mod(Keyp,Qp)+1; + +Pbox=supboxvector2(Ap,Qp,Keyp) + + + +% construction of S-box +Q=2^8; +A=1:Q; +KS=reshape(DK(17:32),1,[]); +Sbox=supboxvector2(A,Q,KS)-1; + +%%%%%%%%%%%%%%%% Construction of round keys%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +r=4; +X=reshape(DK(33:48),1,[]); +X=double(typecast(uint8(X),'uint32')) +P=reshape(DK(49:end),1,[]); +P=double(typecast(uint8(P),'uint32')) +N=2^32; +m=ceil(h/4) +RK= roundKeyiterations(X,P,N,r,Sbox,m); + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% %%%%%%%%%%%%% creation of IV (block of h bits) can be employ for CBC, CFB, OFB, CTR mode %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% s_rk=rc4key(DK(17:1:32)); +% IV=reshape(prga( s_rk,h^2),h,h); +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% %%%%%%%%%%%%%%%%%%%%creation of the diffusion matrix%%%%%%%%%%%%%%%%%%%%%%% +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% sc2=rc4key(DK(end-16:end)); +% outd=prga( sc2,(h^2)/4); +% A=reshape(outd,h/2,h/2); +% In=eye(h/2,h/2); +% if (kindd==0) +% G=[A A+In; A-In A ]; +% else +% A=mod(A,2); +% G=[A bitxor(A,In) ; bitxor(A,In) A]; +% end \ No newline at end of file diff --git a/Chaotic_code/bit2byte.m b/Chaotic_code/bit2byte.m new file mode 100644 index 0000000..b1d9fed --- /dev/null +++ b/Chaotic_code/bit2byte.m @@ -0,0 +1,18 @@ +function y = bit2byte(x) +% y = bit2byte(x) +% Transform a number of bits into a number of bytes. + +x = double(x); + +x_n = numel(x); +if rem(x_n,8) + x = [x zeros(1,8-rem(x_n,8))]; +end +y = zeros(1,ceil(x_n/8)); +y_index = 1; +v = 2.^(0:7); +for i=1:8:x_n + y(y_index) = sum(x(i+7:-1:i).*v); + y_index = y_index + 1; +end +y = uint8(y); diff --git a/Chaotic_code/byte2bit.m b/Chaotic_code/byte2bit.m new file mode 100644 index 0000000..2a79021 --- /dev/null +++ b/Chaotic_code/byte2bit.m @@ -0,0 +1,15 @@ +function y = byte2bit(x) +% y = byte2bit(x) +% Transform a number of bytes into a number of bits. + +x_n=numel(x); +y=zeros(1,x_n*8); +y_index=1; +for i=1:x_n + for j=8:-1:1 +% for j=1:8 + y(y_index+8-j) = bitget(x(i),j); + end + y_index = y_index + 8; +end +y = uint8(y); diff --git a/Chaotic_code/decryptionprocess2.m b/Chaotic_code/decryptionprocess2.m new file mode 100644 index 0000000..8b5024c --- /dev/null +++ b/Chaotic_code/decryptionprocess2.m @@ -0,0 +1,32 @@ +function dimg =decryptionprocess(img,h,RK,Inv_SubBytes,Inv_Pbox,r) +%%%%%%%%%%%%%% convert image to a vector seq +%seq=double(reshape(img,1,[])); +seq_bin=double(byte2bit(uint8(img))) ; +decr_seq_binary=zeros(1,length(seq_bin)); +%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%% +nb= floor(length(seq_bin)/(8*h)); +RK_bin=double(reshape(byte2bit(RK),r,[])); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%% Convert S-box to binary representation to avoid repeat the +%%%%%%%%%%% byte2bit conversion +Bin_Sbox =double(Convert_Sbox_Bin(Inv_SubBytes)); +%%%%%%%%%%%%%%%%%%%% Applied the confusion process (r rounds) +for it=1:nb + temp=seq_bin((it-1)*8*h+1:it*h*8); + for itr=r:-1:1 + temp= temp(Inv_Pbox); + temp=reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]); + % double(byte2bit(Inv_SubBytes(double(bit2byte(temp))+1))); + % +% size(temp) + %byte2bit(Inv_SubBytes(double(bit2byte(temp))+1)); + %temp= Inv_SubBytes(temp+1); + temp=bitxor(temp,RK_bin(itr,1:h*8)); + end + decr_seq_binary((it-1)*8*h+1:it*h*8)=temp; +end + +% Convert to Byte representation + +%valbin=reshape(valbin,Tb/8,8); +dimg=uint8(bit2byte(decr_seq_binary)); \ No newline at end of file diff --git a/Chaotic_code/decryptionprocess2_CBC.m b/Chaotic_code/decryptionprocess2_CBC.m new file mode 100644 index 0000000..176c956 --- /dev/null +++ b/Chaotic_code/decryptionprocess2_CBC.m @@ -0,0 +1,42 @@ +function dimg =decryptionprocess2_CBC(img,h,RK,Inv_SubBytes,Inv_Pbox,r,IV) +%%%%%%%%%%%%%% convert image to a vector seq +%seq=double(reshape(img,1,[])); +seq_bin=double(byte2bit(uint8(img))) ; +decr_seq_binary=zeros(1,length(seq_bin)); +%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%% +nb= floor(length(seq_bin)/(8*h)); +RKB=byte2bit(RK); +RKB=RKB(1:r*h*8); +%RK_bin=double(reshape(byte2bit(RK),r,[])); +RK_bin=double(reshape(RKB,r,[])); +%RK_bin=double(reshape(byte2bit(RK),r,[])); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%% Convert S-box to binary representation to avoid repeat the +%%%%%%%%%%% byte2bit conversion +Bin_Sbox =double(Convert_Sbox_Bin(Inv_SubBytes)); + +iv=[]; +for i=1:h + iv=[iv byte2bit(IV(i))]; +end +IV=double(iv); + + +%%%%%%%%%%%%%%%%%%%% Applied the confusion process (r rounds) +for it=1:nb + temp=seq_bin((it-1)*8*h+1:it*h*8); + temp_IV=temp; + for itr=r:-1:1 + temp= temp(Inv_Pbox); + temp=reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[]); + temp=bitxor(temp,RK_bin(itr,1:h*8)); + end + temp=bitxor(temp,IV); + decr_seq_binary((it-1)*8*h+1:it*h*8)=temp; + IV= temp_IV; +end + +% Convert to Byte representation + +%valbin=reshape(valbin,Tb/8,8); +dimg=uint8(bit2byte(decr_seq_binary)); \ No newline at end of file diff --git a/Chaotic_code/encryptionprocess2.m b/Chaotic_code/encryptionprocess2.m new file mode 100644 index 0000000..08a8966 --- /dev/null +++ b/Chaotic_code/encryptionprocess2.m @@ -0,0 +1,25 @@ +function eimg =encryptionprocess(img,h,RK,SubBytes,Pbox,r) +%%%%%%%%%%%%%% convert image to a vector seq +%seq=double(reshape(img,1,[])); +seq_bin=double(byte2bit(uint8(img))) ; +encr_seq_binary=zeros(1,length(seq_bin)); +%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%% +nb= floor(length(seq_bin)/(8*h)); +RK_bin=double(reshape(byte2bit(RK),r,[])); +Bin_Sbox =double((Convert_Sbox_Bin(SubBytes))); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%% Applied the confusion process then the bits permutation for r rounds +for it=1:nb + temp=seq_bin((it-1)*8*h+1:it*8*h); + for itr=1:r + temp=bitxor(temp,RK_bin(itr,1:h*8)); + temp= (reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[])); + temp= temp(Pbox); + end + encr_seq_binary((it-1)*8*h+1:it*h*8)=temp; +end + +% Convert to Byte representation + +%valbin=reshape(valbin,Tb/8,8); +eimg=(bit2byte( encr_seq_binary)); \ No newline at end of file diff --git a/Chaotic_code/encryptionprocess2_CBC.m b/Chaotic_code/encryptionprocess2_CBC.m new file mode 100644 index 0000000..c79b7ab --- /dev/null +++ b/Chaotic_code/encryptionprocess2_CBC.m @@ -0,0 +1,39 @@ +function eimg =encryptionprocess2_CBC(img,h,RK,SubBytes,Pbox,r,IV) +%%%%%%%%%%%%%% convert image to a vector seq +%seq=double(reshape(img,1,[])); +seq_bin=double(byte2bit(uint8(img))) ; +encr_seq_binary=zeros(1,length(seq_bin)); +%%%%%%%%%%%%%%%%%%%% calculate the number of blocks%%%%%%%%%%%%%%%%%%%%%%%% +nb= floor(length(seq_bin)/(8*h)); +RKB=byte2bit(RK); +RKB=RKB(1:r*h*8); +%RK_bin=double(reshape(byte2bit(RK),r,[])); +RK_bin=double(reshape(RKB,r,[])); +Bin_Sbox =double((Convert_Sbox_Bin(SubBytes))); +%iv=zeros(1,8*h); +%iv(end-h+1:end)=IV; +%IV=iv; +iv=[]; +for i=1:h + iv=[iv byte2bit(IV(i))]; +end +IV=double(iv); + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%% Applied the confusion process then the bits permutation for r rounds +for it=1:nb + temp=seq_bin((it-1)*8*h+1:it*8*h); + temp=bitxor(temp,IV); + for itr=1:r + temp=bitxor(temp,RK_bin(itr,1:h*8)); + temp= (reshape(Bin_Sbox (double(bit2byte(temp))+1,:)',1,[])); + temp= temp(Pbox); + end + encr_seq_binary((it-1)*8*h+1:it*h*8)=temp; + IV=temp; +end + +% Convert to Byte representation + +%valbin=reshape(valbin,Tb/8,8); +eimg=(bit2byte( encr_seq_binary)); \ No newline at end of file diff --git a/Chaotic_code/inverse_tables.m b/Chaotic_code/inverse_tables.m new file mode 100644 index 0000000..9e0ee28 --- /dev/null +++ b/Chaotic_code/inverse_tables.m @@ -0,0 +1,7 @@ +function inv_perm_tabs= inverse_tables(tab) +le=length(tab); +inv_perm_tabs=zeros(1,le); +for ir = 1 :le + inv_perm_tabs(tab(ir)+1) = ir-1 ; +end + diff --git a/Chaotic_code/inverse_tables2.m b/Chaotic_code/inverse_tables2.m new file mode 100644 index 0000000..802d6ea --- /dev/null +++ b/Chaotic_code/inverse_tables2.m @@ -0,0 +1,8 @@ +function inv_perm_tabs= inverse_tables2(tab) +tab2=reshape(tab,1,[]); +le=length(tab2); +inv_perm_tabs=zeros(1,le); +for ir = 1 :le + inv_perm_tabs(tab2(ir)) = ir ; +end +%inv_perm_tabs=reshape(inv_perm_tabs,size(tab,1),size(tab,2)); diff --git a/Chaotic_code/lena512.bmp b/Chaotic_code/lena512.bmp new file mode 100644 index 0000000..5641e75 Binary files /dev/null and b/Chaotic_code/lena512.bmp differ diff --git a/Chaotic_code/lenna.bmp b/Chaotic_code/lenna.bmp new file mode 100644 index 0000000..d78becd Binary files /dev/null and b/Chaotic_code/lenna.bmp differ diff --git a/Chaotic_code/lenna512.bmp b/Chaotic_code/lenna512.bmp new file mode 100644 index 0000000..d78becd Binary files /dev/null and b/Chaotic_code/lenna512.bmp differ diff --git a/Chaotic_code/main2.m b/Chaotic_code/main2.m new file mode 100644 index 0000000..cc4f3ac --- /dev/null +++ b/Chaotic_code/main2.m @@ -0,0 +1,79 @@ +%clear all +%close all +%clc + + + +%%%%%%%%%%%%%%%% Input data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +[filename, pathname] = uigetfile( {'*.bmp';}, 'Pick a file'); +name=[ pathname filename]; +data=imread(name); +%data=imresize(data,[64 64]) + +h=2;% number of bytes in a block (Tb=h*8) +r=4;% number of rounds +sk=16 % number of byes in a block cipher +strh='SHA-512'; +% mode=0==> ECB, +% mode 1==> CBC + +mode=1 + + + +% calculate the length of data +SI=size(data); +%len=SI(1)*SI(2)*SI(3); +%lenBits=len*8; +%data_line=reshape(double(data),1,len); + +figure() +imshow(uint8(data)); + +%%%%%%%%%%%%%%%%% Key generation%%%%%%%%%%%%%%%%%%%%%%%% +Secretkey=floor(rand(1,sk)*256); +counter=floor(rand(1,sk)*256); +%%%%%%%%%%%%%%%%%%%%%%%%%Creation of cipher layer's%%%%%%%%%%%%%%%%%%%% +[RK,Sbox,Pbox]=Dynamickeygenerationnew(Secretkey,counter,strh,h); + %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Change in data block%%%%%%%%%%%%%%%%%%%%%%%% + + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Encryption Process%%%%%%%%%%%%%%%%%%%%% +tic +if mode==0 +eimg =encryptionprocess2(data,h,RK,Sbox,Pbox,r); +elseif mode==1 +IV=double(floor(rand(1,h)*256)); +eimg =encryptionprocess2_CBC(data,h,RK,Sbox,Pbox,r,IV); +end + +toc +eimg2=reshape(eimg,size(data)); + +figure() +imshow(uint8(eimg2)); + +%%%%%%%%%%%%%%%%%%%%% Decryption Side%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%% calculate the inverse substitution and diffusion +%%%%%%%%%%%%%%%%%%%% layer%%%%%%%%%%%%%%%% + Inv_Sbox= inverse_tables(Sbox); + Inv_Pbox= inverse_tables2(Pbox); + + + if mode==0 + dimg =decryptionprocess2(eimg,h,RK,Inv_Sbox,Inv_Pbox,r); +elseif mode==1 + + dimg =decryptionprocess2_CBC(eimg,h,RK,Inv_Sbox,Inv_Pbox,r,IV); + end + +% tic +% dimg =decryptionprocess2(eimg,h,RK,Inv_Sbox,Inv_Pbox,r); +% toc + dimg=reshape(dimg,size(data)); + figure() + imshow(uint8(dimg)); + diff --git a/Chaotic_code/roundKeyiterations.m b/Chaotic_code/roundKeyiterations.m new file mode 100644 index 0000000..564078a --- /dev/null +++ b/Chaotic_code/roundKeyiterations.m @@ -0,0 +1,18 @@ +function RK=roundKeyiterations (X,P, N,r,Sbox,m) +% DIfferent size of input block +%m=1; +RK=zeros(r,4*m); +for itr=1:r + for itm=1:m + y=skewtententierperturbed(X(itr),P(itr),N) + %Y(i)=double(typecast(y,'uint8')) + op=double(typecast(uint32(y),'uint8')) + opd(1)=bitxor(bitxor(op(1),op(2)),op(3)); + opd(2)=bitxor(bitxor(op(1),op(2)),op(4)); + opd(3)=bitxor(bitxor(op(1),op(4)),op(3)); + opd(4)=bitxor(bitxor(op(4),op(2)),op(3)); + opd=Sbox(opd+1); + RK(itr,(itm-1)*4+1:itm*4)=opd(:); + X(itr)=y; + end +end \ No newline at end of file diff --git a/Chaotic_code/skewtententierperturbed.m b/Chaotic_code/skewtententierperturbed.m new file mode 100644 index 0000000..afacc73 --- /dev/null +++ b/Chaotic_code/skewtententierperturbed.m @@ -0,0 +1,9 @@ +function y=skewtententierperturbed(x,p,Q) +% x : initial condition +% p: control parameter +% N:precision +if x<=p + y=ceil(Q*(x/p)); + else + y=floor(Q*((Q-x)/(Q-p)))+1; +end diff --git a/Chaotic_code/supboxvector2.m b/Chaotic_code/supboxvector2.m new file mode 100644 index 0000000..34acd09 --- /dev/null +++ b/Chaotic_code/supboxvector2.m @@ -0,0 +1,12 @@ +function x=supboxvector2(x,Q,p) +% r=1 for each parameters +y=zeros(1,Q); +for i=1:length(p) +%for w=1:r +dinf=find (x<=p(1,i)); +dsup=find (x>p(1,i)); +y(dinf)=ceil((Q*x(dinf))./p(1,i)); +y(dsup)=floor((Q*(Q-x(dsup)))./(Q-p(1,i)))+1; +x=y; +%end +end diff --git a/toto b/toto deleted file mode 100644 index e69de29..0000000