aplikasi invers dalam kriptografi

Download Aplikasi Invers dalam Kriptografi

If you can't read please download the document

Upload: pauline

Post on 11-Jan-2016

17 views

Category:

Documents


6 download

DESCRIPTION

Salah satu aplikasi invers matriks dalam kehidupan kita adalah untuk persandian atau kriptografi. Menggunakan aturan perkalian matriks, kita bisa mengubah pesan menjadi suatu sandi dan menggunakan invers untuk mencari bentuk asalnya. Berikut ini adalah contoh program yang ditulis dalam bahasa Pascal untuk kriptografi menggunakan kunci yang sudah ditentukan. One of the use of matrix in our daily life is in cryptography. Using the rules of matrix multiplication, we can cipher a plain text. To revert it back, we can multiply it with the inverse of the key. The following is a simple program written in Pascal to encipher and decipher using a default key.

TRANSCRIPT

typemat = array[1..2,1..100] of integer;procedure convert(inp:string; var asci:mat;disp:boolean);vari,j,k,l: integer;sp : string[1];beginsp := ' ';if not length(inp) mod 2 = 0 then insert(sp,inp,length(inp)+1);k := length(inp) div 2;l := 0;if disp=true then writeln('Converted message to numbers: ');for i:=1 to 2 dobeginfor j:=1 to k dobegininc(l);if inp[l] = ' ' then asci[i,j]:=0else asci[i,j]:=ord(inp[l])-ord('a')+1;if disp = true then write(asci[i,j], ' ');end;if disp = true then writeln();end;end;procedure encrypt(asci,key:mat;k:integer;disp:boolean);vari,j:integer;code:mat;msg,sp,num:string;beginsp:= ' ';msg:= '';if disp=true then writeln('Encrypted matrix:');for i:= 1 to 2 dobeginfor j:=1 to k dobegincode[i,j]:= key[i,1]*asci[1,j] + key[i,2] * asci[2,j];if disp = true then write(code[i,j],' ');if code[i,j]=0 then insert(sp,msg,length(msg)+1)else beginstr(code[i,j],num);insert(num,msg,length(msg)+1);insert(sp,msg,length(msg)+1);end;end;if disp = true then writeln();end;writeln('Encrypted message: ');writeln(msg);end;procedure decrypt(asci,key:mat;k:integer;disp:boolean);vari,j,det,temp:integer;msg,alp,sp:string;code:mat;begindet:=(key[1,1]*key[2,2]) - (key[1,2]*key[2,1]);if disp=true then writeln('Determinant: ',det);temp:= key[1,1];key[1,1]:=key[2,2];key[2,2]:=temp;key[1,2]:=-1*key[1,2];key[2,1]:=-1*key[2,1];if disp = true then writeln('Inverse of key: ');for i:=1 to 2 dobeginfor j:=1 to 2 dobeginkey[i,j]:=det*key[i,j];if disp = true then write(key[i,j],' ');end;if disp = true then writeln();end;msg:='';sp:=' ';if disp = true then writeln('Decrypted matrix: ');for i:= 1 to 2 dobeginfor j:=1 to k dobegincode[i,j]:= key[i,1]*asci[1,j] + key[i,2] * asci[2,j];if disp = true then write(code[i,j],' ');alp:=chr(code[i,j]+ord('a')-1);if code[i,j]=0 then insert(sp,msg,length(msg)+1)else insert(alp,msg,length(msg)+1);end;if disp = true then writeln();end;writeln('Decrypted message: ');writeln(msg);end;label 1;varinp:string;ans:char;enc,disp,again:boolean;i,j,k,l:integer;asci,key,inpd:mat;beginkey[1,1] := 2;key[1,2] := -5;key[2,1] := -1;key[2,2] := 3;again:=true;while again=true dobeginwrite('Do you want to encrypt or decrypt?(e/d) ');readln(ans);l:=0;if ans = 'e' then enc:=trueelse if ans = 'd' then enc:= falseelse beginwriteln('ERROR. Command not found.');goto 1;end;write('Do you want to display process?(y/n) ');readln(ans);if ans = 'y' then disp:=true else if ans = 'n' then disp:=falseelse beginwriteln('ERROR. Command not found.');goto 1;end;if enc = true then beginwriteln('Enter message: ');readln(inp);if length(inp) mod 2 = 0 then k:=length(inp) div 2else k:=length(inp) div 2 +1;convert(inp, asci,disp);if disp = true thenbeginwriteln('Key:');for i:=1 to 2 dobeginfor j:=1 to 2 dobeginwrite(key[i,j],' ');end;writeln();end;end;encrypt(asci,key,k,disp);endelse beginwrite('How many characters the message has? ');readln(l);writeln('Enter message: ');for i:=1 to l do read(inpd[1,i]);readln();k:=l div 2;l:=0;if disp = true then writeln('Converted message into matrix:');for i:=1 to 2 dobeginfor j:=1 to k dobegininc(l);asci[i,j]:=inpd[1,l];if disp = true then write(asci[i,j],' ');end;if disp = true then writeln();end;if disp = true thenbeginwriteln('Key:');for i:=1 to 2 dobeginfor j:=1 to 2 dobeginwrite(key[i,j],' ');end;writeln();end;end;decrypt(asci,key,k,disp);end;1:writeln('Try again?(y/n) ');readln(ans);if ans='y' then again:=trueelse if ans='n' then again:=falseelse beginwriteln('ERROR. Command not found.');goto 1;end;end;end.