clc;
clear;
key1=20;
key2=90;
key3=160;
key4=55;
key5=110;



ix=imread('crypt.jpg');
ix=rgb2gray(ix);
im=double(ix);
iln=size(im,1);
jln=size(im,2);

%% Pembangkitan Vigenere Key
totalpx=iln*jln;
keys=[];
loopkey=1;
for k=1:totalpx
    
     
       if (loopkey==1)
       key=key1;
       elseif (loopkey==2)
       key=key2;
       elseif (loopkey==3)
       key=key3;
       elseif (loopkey==4)
       key=key4;
       elseif (loopkey==5)
       key=key5; 
       elseif (mod(loopkey,6)==0)
       loopkey=1;
       key=key1;
       
       end
       
       keys=[keys key];
       loopkey=loopkey+1;
             
end





%% Enkripsi

cipher=[];
y=[];
loopkey=1;

for i=1:iln
for j=1:jln
    m= im(i,j)+keys(1,loopkey); 
    x=mod(m,256);
    y=[y x];
    loopkey=loopkey+1;
    
end
cipher=[cipher;y];
y=[];
end
cipherx=uint8(cipher);


%%===== Dekripsi

plain=[];
y=[];
loopkey=1;
for i=1:iln
for j=1:jln
    
    m= cipher(i,j)-keys(1,loopkey); 
    x=mod(m,256);
    y=[y x];
    loopkey=loopkey+1;
    
end
plain=[plain;y];
y=[];
end
plainx=uint8(plain);
