Caesar Cipher bagian 2, [bruteforce attack]


Setelah sebelum nya kita pernah membahas tentang caesar cipher menggunakan java pada postingan berikut https://ridhobustami.wordpress.com/2015/03/31/caesar-cipher/ sayang nya kita lupa membahas bagaimana mendekripsikan cipher text tersebut, prinsip dekripsi pada caesar cipher tidak sulit, hanya dengan mengurangkan cipher textnya sesuai dengan rotasi nya, selain itu kita juga akan mencoba bruteforce attack di caesar cipher, untuk lebih jelasnya perhatikan script java berikut:

package encryptdecrypt;



public class CaesarCipher {
	
	public static String bacaString(){
        int karakter;
        String str = "";
        boolean selesai =false;
         
        while(!selesai){
            try{
                karakter =System.in.read();
                if (karakter <0 || (char) karakter == '\n')
                    selesai=true;
                else if ((char) karakter != '\r')
                    str = str + (char) karakter;
                }
            catch(java.io.IOException e) {
                System.err.println("ada kesalahan");
                selesai = true;
            }
        }
      return str;
    }
	    
	private static String encrypting(String st, int rtint){
        int pjgstr=st.length();
        char [] ciphertext = new char [pjgstr];
       
        int[] ascist;
        ascist = new int [pjgstr];
 
        int[] cipherasci1;
        cipherasci1 = new int [pjgstr];
        int[] cipherasci2;
        cipherasci2 = new int [pjgstr];
        int salah = 0;
 
        if (rtint >= 26)
            rtint = rtint%25;
      

        for (int a=0; a < pjgstr; a++) {
        ascist[a] = String.valueOf(st).codePointAt(a);

        if (ascist[a] >= 97 && ascist[a] <= 122 ){
            cipherasci1 [a] = ascist[a]+rtint;
            if (cipherasci1 [a] >= 123)
                cipherasci2 [a] = 97 + (cipherasci1 [a] % 123);
            else
                cipherasci2 [a] = cipherasci1 [a];   
        }
        else if (ascist[a] >= 65 && ascist[a] <= 90 ) {
            cipherasci1 [a] = ascist[a]+rtint;
            if (cipherasci1 [a] >= 91)
                cipherasci2 [a] = 65 + (cipherasci1 [a] % 91);
            else
                cipherasci2 [a] = cipherasci1 [a];
        }
        else if (ascist[a] == 32 ) {
            cipherasci2 [a] = 32;
        }
 
        else {
            salah = 1;
        }
        
        ciphertext [a] = (char) cipherasci2 [a];
            }
        
        String Ct = String.valueOf(ciphertext);
     
        if (salah == 1)
            System.out.println("==error== (masukan hanya huruf besar / kecil dan spasi)"); 
        return Ct;
	}
	
	private static String decrypting(String st, int rtint){
        int pjgstr=st.length();
        char [] ciphertext = new char [pjgstr];
      
        int[] ascist;
        ascist = new int [pjgstr];
 
        int[] cipherasci1;
        cipherasci1 = new int [pjgstr];
        int[] cipherasci2;
        cipherasci2 = new int [pjgstr];
        int salah = 0;
 
        if (rtint >= 26)
            rtint = rtint%25;
     
        
        for (int a=0; a < pjgstr; a++) {
        ascist[a] = String.valueOf(st).codePointAt(a);
  
        if (ascist[a] >= 97 && ascist[a] <= 122 ){
        	
            
            cipherasci1 [a] = ascist[a]-rtint;
            if (cipherasci1 [a] <= 96)
                cipherasci2 [a] = -96 + 122 + (cipherasci1 [a] % 97);
            else
                cipherasci2 [a] = cipherasci1 [a]; 
          
        }
       
        else if (ascist[a] >= 65 && ascist[a] <= 90 ) {
            cipherasci1 [a] = ascist[a]-rtint;
            if (cipherasci1 [a] <= 64)
                cipherasci2 [a] = -64 + 90 + (cipherasci1 [a] % 65);
            else
                cipherasci2 [a] = cipherasci1 [a];
        }
        else if (ascist[a] == 32 ) {
            cipherasci2 [a] = 32;
        }
 
        else {
            salah = 1;
        }
        
        ciphertext [a] = (char) cipherasci2 [a];
            }
        
        if (salah == 1)
            System.out.println("==error== (masukan hanya huruf besar / kecil dan spasi)");

        String pt = String.valueOf(ciphertext);
        return pt;
		
	}
	
	private static String [] BruteForce (String st){
		String plainAr [] = new String [26];
		for (int i=0; i < 26; i++){
			String plain = decrypting (st, i);
			plainAr [i]=plain;
		}
		return plainAr;
	}
	
	
	public static void main(String[] args){
	        System.out.print("Plaintext: ");
	        String st = bacaString();
	        System.out.print("ROT: ");
	        String rt = bacaString();
	        int rtint = Integer.parseInt(rt);
	        String cipher = encrypting (st, rtint);
	        System.out.print("Ciphertext: ");
	        System.out.println( cipher);
	        String plain = decrypting (cipher, rtint);
	        System.out.print("Plaintext: ");
	        System.out.println( plain );
	        System.out.println();
	        String [] plainAr = BruteForce (cipher);
	        
	       for (int i =0; i <26; i++){
	    	   	System.out.print("Brute Force attack ke " + i + " : ");
	        	System.out.println( plainAr[i]);
	       }
	        	         
	    }
	 }
	

hasil dari script diatas jika kita mengisi plaintext dengan kalimat when you saw him call me dengan rotasi sebanyak 8 maka di console akan kita dapatkan hasil sebagai berikut

Plaintext: when you saw him call me
ROT: 8
Ciphertext: epmv gwc aie pqu kitt um
Plaintext: when you saw him call me

Brute Force attack ke 0 : epmv gwc aie pqu kitt um
Brute Force attack ke 1 : dolu fvb zhd opt jhss tl
Brute Force attack ke 2 : cnkt eua ygc nos igrr sk
Brute Force attack ke 3 : bmjs dtz xfb mnr hfqq rj
Brute Force attack ke 4 : alir csy wea lmq gepp qi
Brute Force attack ke 5 : zkhq brx vdz klp fdoo ph
Brute Force attack ke 6 : yjgp aqw ucy jko ecnn og
Brute Force attack ke 7 : xifo zpv tbx ijn dbmm nf
Brute Force attack ke 8 : when you saw him call me
Brute Force attack ke 9 : vgdm xnt rzv ghl bzkk ld
Brute Force attack ke 10 : ufcl wms qyu fgk ayjj kc
Brute Force attack ke 11 : tebk vlr pxt efj zxii jb
Brute Force attack ke 12 : sdaj ukq ows dei ywhh ia
Brute Force attack ke 13 : rczi tjp nvr cdh xvgg hz
Brute Force attack ke 14 : qbyh sio muq bcg wuff gy
Brute Force attack ke 15 : paxg rhn ltp abf vtee fx
Brute Force attack ke 16 : ozwf qgm kso zae usdd ew
Brute Force attack ke 17 : nyve pfl jrn yzd trcc dv
Brute Force attack ke 18 : mxud oek iqm xyc sqbb cu
Brute Force attack ke 19 : lwtc ndj hpl wxb rpaa bt
Brute Force attack ke 20 : kvsb mci gok vwa qozz as
Brute Force attack ke 21 : jura lbh fnj uvz pnyy zr
Brute Force attack ke 22 : itqz kag emi tuy omxx yq
Brute Force attack ke 23 : hspy jzf dlh stx nlww xp
Brute Force attack ke 24 : grox iye ckg rsw mkvv wo
Brute Force attack ke 25 : fqnw hxd bjf qrv ljuu vn

pada method BruteForce (cipher) tidak diperlukan nilai rotasi atau kunci namun dengan mengunakan brute force kita mendapatkan 25 kombinasi hasil dekripsi yang hanya pada nilai Brute Force attack ke 8 memiliki arti. Disini kita bisa melihat salah satu kelemahan dari caesar cipher, hanya beberapa millisecond bruteforce attack dapat dilakukan, namun di program ini belum dapat menentukan secara otomatis hasil dekripsi yamg mana yang memiliki arti dalam bahasa.

Advertisements

Author: ridhobustami

orang ngak jelas

2 thoughts on “Caesar Cipher bagian 2, [bruteforce attack]”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s