caesar cipher


okey langsung saja pada intinya cara kerja caesar cipher

jika pergerseran 1 maka
a menjadi b
b menjadi c
dst
z menjadi a

ika pergerseran 3 maka
a menjadi d
b menjadi e
dst
z menjadi c

untuk implementasi di pemograman yg perlu dipahami dasar2 sebagai berikut

1. operasi mod dan penambahan
2. table ascii dan bagaimana merubah serta membalikannya dari dan ke string
3. dasar-dasar pemograman

kali ini saya mencoba untuk membuat pemograman sederhana via java


public class caesarciph {
	public static void main(String[] args){
		System.out.print("Plaintext: ");
		String st = boardbaca.bacaString();
		System.out.print("ROT: ");
		String rt = boardbaca.bacaString();
		int rtint = Integer.parseInt(rt);

		int pjgstr=st.length();
		int[] ascist;
		ascist = new int [pjgstr];

		int[] cipherasci1;
		cipherasci1 = new int [pjgstr];
		int[] cipherasci2;
		cipherasci2 = new int [pjgstr];
		int salah = 0;
		System.out.print("cipher: ");

		if (rtint >= 26)
			rtint = rtint%25;
		else
			rtint = rtint ;

		for (int a=0; a < pjgstr; a++) {
		ascist[a] = String.valueOf(st).codePointAt(a);
		//System.out.println([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];
			System.out.print(Character.toString ((char) cipherasci2 [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];
			System.out.print(Character.toString ((char) cipherasci2 [a]));
		}
		else if (ascist[a] == 32 ) {
			cipherasci2 [a] = 32;
			System.out.print(Character.toString ((char) cipherasci2 [a]));
		}

		else {
			salah = 1;
		}
			}
		 System.out.println("");
		if (salah == 1)
			System.out.println("==error== (masukan hanya huruf besar / kecil dan spasi)");
		else
		    System.out.println("==selesai==");
	}

}

untuk membaca data dari keybord maka diperlukan script tambahan sebagai berikut



import java.io.*;

public class boardbaca {
	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;
	}
}

hasilnya jika file jar dijalankan di terminal linux sebagai berikut
Screenshot from 2015-03-30 21:19:06

Advertisements

Author: ridhobustami

orang ngak jelas

One thought on “caesar cipher”

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