LihatPesan.java Implementasi Algoritma Hill Cipher 3x3 dan Rabin Public Key pada Pengiriman Short Message Service (SMS)

3. LihatPesan.java

import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.nio.charset.Charset; import java.util.Random; public class LihatPesan extends Activity { private static final Charset UTF8_CHARSET = Charset.forNameUTF-8; final int tipe_enkripsi=128; private long selisihhill,waktuawalhill,waktuakhirhill,selisihrabin,waktuawalrabin,waktuakhirrab in,selisihtotal; TextView number, date, msg,hasilDekripsi; Button forward, hapus,dekripsi,dekripsikunci,btndekriphill; EditText kunci_p_dek,kunci_q_dek,nilai_k,kuncihill_Dek,hasildekkuncihill; public boolean pil_rad=true; Override protected void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState; setContentViewR.layout.lihatpesan; this.setTitleDecrypt Message; number = TextView findViewByIdR.id.tvNumber; date = TextView findViewByIdR.id.tvDate; msg = TextView findViewByIdR.id.tvMsg; hasilDekripsi = TextView findViewByIdR.id.hasildekrip; dekripsi = Button findViewByIdR.id.dekripsiBtn; forward = Button findViewByIdR.id.btFrd; btndekriphill=Button findViewByIdR.id.btnDekripkunci; hapus = Button findViewByIdR.id.hapus; kunci_p_dek = EditText findViewByIdR.id.kunci_p_dek; kunci_q_dek = EditText findViewByIdR.id.kunci_q_dek; hasildekkuncihill=EditText findViewByIdR.id.hasildekkuncihill; kuncihill_Dek = EditText findViewByIdR.id.kuncihill_dek; nilai_k = EditTextfindViewByIdR.id.k_values; kuncihill_Dek.setTextSimpanKunci.getCipherkey; nilai_k.setTextSimpanKunci.getKkey; btndekriphill.setOnClickListenernew OnClickListener { Override public void onClickView arg0 { TODO Auto-generated method stub int a_inv = 0; int flag = 0; waktuawalrabin=System.currentTimeMillis; String ambilkata2 = kuncihill_Dek.getText.toString; String kunci_dek = kunci_p_dek.getText.toString; String temp=; String hasil2 = null; try { String keyx = SimpanKunci.getCipherkey; String p = kunci_p_dek.getText.toString; String q = kunci_q_dek.getText.toString; String k = SimpanKunci.getKkey; if .equalskeyx { kuncihill_Dek.setErrorYour Cipherkey is Empty; } else if .equalsp { kunci_p_dek.setErrorPrivate Keys cannot Empty; Universitas Sumatera Utara } else if .equalsq { kunci_q_dek.setErrorPrivate Keys cannot Empty; } else if p.equalsq { kunci_p_dek.setErrorPrivate Keys must be Different; } else if p.matches[0-9] { kunci_p_dek.setErrorPrivate Keys must be a Number; } else if q.matches[0-9] { kunci_q_dek.setErrorPrivate Keys must be a Number; } else if fermatInteger.parseIntp { kunci_p_dek.setErrorPrivate Key must be a Prime Number; } else if fermatInteger.parseIntq { kunci_q_dek.setErrorPrivate Key must be a Prime Number; } else if Integer.parseIntp 4 = 3 { kunci_p_dek.setErrorPrivate Keys must have modulus 3 when devide by 4; } else if Integer.parseIntq 4 = 3 { kunci_q_dek.setErrorPrivate Keys must have modulus 3 when devide by 4; } else if .equalsk { nilai_k.setErrorK cannot Empty; }else { long startTime=System.currentTimeMillis; int uvw = Integer.parseIntp; int xyz = Integer.parseIntq; int n = uvw xyz; int P = uvw + 1 4; int Q = xyz + 1 4; int vals[] = gcduvw, xyz; int Yp = vals[1]; int Yq = vals[2]; int r, s, t, u, R, S, T, U; String[] cipherteks = keyx.split ; int[] arrayCiptext = new int[cipherteks.length]; String[] Kvalues = k.split ; int[] arrayK = new int[Kvalues.length]; for int i = 0; i cipherteks.length; i++ { try { arrayCiptext[i] = Integer.parseIntcipherteks[i]; arrayK[i] = Integer.parseIntKvalues[i]; } catch NumberFormatException nfe { } int cipher = arrayCiptext[i]; int K = arrayK[i]; double mp = modExpcipher, P, uvw; double mq = modExpcipher, Q, xyz; int a1 = int Yp uvw mq; int a2 = int Yq xyz mp; r = a1 + a2 n + n n; R = K n + r; String Rx = toBinaryR; s = a1 - a2 n + n n; S = K n + s; String Sx = toBinaryS; t = -a1 + a2 n + n n; T = K n + t; String Tx = toBinaryT; u = -a1 - a2 n + n n; U = K n + u; String Ux = toBinaryU; tabelEnc tabelEnc= new tabelEnc; char hsl; if checkRx { String RR = singlebinRx; long Rfinal = Long.parseLongRR; long Rfin = toDecimalRfinal; hsl=tabelEnc.getCharintRfin; hasildekkuncihill.setTexthasildekkuncihill.getText.toString + hsl; } else if checkSx { String SS = singlebinSx; Universitas Sumatera Utara long Sfinal = Long.parseLongSS; long Sfin = toDecimalSfinal; hsl=tabelEnc.getCharintSfin; hasildekkuncihill.setTexthasildekkuncihill.getText.toString + hsl; } else if checkTx { String TT = singlebinTx; long Tfinal = Long.parseLongTT; long Tfin = toDecimalTfinal; hsl=tabelEnc.getCharintTfin; hasildekkuncihill.setTexthasildekkuncihill.getText.toString + hsl; } else if checkUx { String UU = singlebinUx; long Ufinal = Long.parseLongUU; long Ufin = toDecimalUfinal; hsl=tabelEnc.getCharintUfin; hasildekkuncihill.setTexthasildekkuncihill.getText.toString + hsl; } else { hasildekkuncihill.setText; } } DekripsiAutokeyCiphermsgx, key.getText.toString; long endTime=System.currentTimeMillis; long duration = endTime - startTime; SimpanKunci.Reset; Toast.makeTextgetApplicationContext, Decryption Time: +duration+ms, Toast.LENGTH_LONG.show; } } catch Exception e { TODO Auto-generated catch block e.printStackTrace; } hasildekkunciaes.setTexttemp; waktuakhirrabin=System.currentTimeMillis; selisihrabin=waktuakhirrabin-waktuawalrabin; Toast.makeTextLihatPesan.this, lama dekripsi rabin +Long.toStringselisihrabin,Toast.LENGTH_SHORT.show; } }; dekripsi.setOnClickListenernew OnClickListener { Override public void onClickView arg0 { int a_inv = 0; int flag = 0; String ambilKata = msg.getText.toString; String deKata = ; try { if .equalshasildekkuncihill.getText.toString { waktuawalhill=System.currentTimeMillis; String hasil2e=null; String keyaes_dek = hasildekkuncihill.getText.toString; hillCipher obj1 = new hillCipher; double sq = Math.sqrtkeyaes_dek.length; System.out.printlnini double +sq+ ini long +longsq+ ini int +intsq; if sq = long sq System.out.printlnpanjang kunci salah; 0+3TLeT9[I.MV5hk1zz mOTRQBU else { int s = int sq; if obj1.checkkeyaes_dek, s { System.out.printlnResult:; obj1.cofactobj1.keymatrix, s; System.out.printinversi key +obj1.invkey; keyaes_dek=obj1.invkey; Universitas Sumatera Utara } } double sqt = Math.sqrtkeyaes_dek.length; System.out.printlnini double +sq+ ini long +longsq+ ini int +intsq; if sqt = long sqt System.out.printlnpanjang kunci salah; 0+3TLeT9[I.MV5hk1zz mOTRQBU else { int s = int sqt; if obj1.checkkeyaes_dek, s { System.out.printlnResult:; obj1.divideambilKata, s; pakai kunci ASCII dari 32-126 System.out.printlnini ya wee :+obj1.hasilenkripsi; deKata=obj1.hasilenkripsi; obj1.cofactobj1.keymatrix, s; System.out.printinversi key +obj1.invkey; } } waktuakhirhill=System.currentTimeMillis; hasilDekripsi.setTextdeKata; selisihhill=waktuakhirhill-waktuawalhill; selisihtotal=selisihhill+selisihrabin; Toast.makeTextLihatPesan.this, waktu dekrips Hill Cipher +Long.toStringselisihtotal,Toast.LENGTH_SHORT.show; } else{ Toast.makeTextLihatPesan.this, PlainText Kunci Hill masih kosong,Toast.LENGTH_SHORT.show; } } catch Exception e { Log.iError, e.getMessage;hasilDekripsi.setTextkunci tidak memenuhi GCD atau tidak memenuhi syarat inversi modulo ;} } }; } Override protected void onStart { super.onStart; Intent i = getIntent; number.setTexti.getStringExtrano; date.setTexti.getStringExtradate; msg.setTexti.getStringExtramsg; forward.setOnClickListenernew OnClickListener { Override public void onClickView v { Intent click = new IntentLihatPesan.this, BuatPesan.class; click.putExtramessage, msg.getText; startActivityclick; } }; hapus.setOnClickListenernew OnClickListener { Override public void onClickView v { Dialogs.showConfirmationLihatPesan.this, R.string.hapuspesan_dialog, new DialogInterface.OnClickListener { public void onClickDialogInterface dialog, int which { Intent i = getIntent; String id_pesan_hapus = i .getStringExtraidpesan; Universitas Sumatera Utara String id_thread_hapus = i .getStringExtraidthread; hapus pesan Uri deleteUri = Uri.parsecontent:sms; getContentResolver .deletedeleteUri, thread_id=? and _id=?, new String[] { String.valueOfid_thread_hapus, String.valueOfid_pesan_hapus }; finish; Toast.makeTextLihatPesan.this, Pesan Terhapus, Toast.LENGTH_SHORT .show; redirect data pesan onBackPressed; } }; } }; } public String toBinarylong n { if n == 0 { return 0; } String binary = ; while n 0 { long rem = n 2; binary = rem + binary; n = n 2; } return binary; } public long toDecimallong binary{ long decimal = 0; long power = 0; whiletrue{ ifbinary == 0{ break; } else { long temp = binary10; decimal += tempMath.pow2, power; binary = binary10; power++; } } return decimal; } public static int[] gcdint p, int q { if q==0 return new int[] { p, 1, 0 }; int[] vals = gcdq, p q; int d = vals[0]; int a = vals[2]; int b = vals[1] - p q vals[2]; return new int[] { d, a, b }; } public static int modExpint a, int b, int n { if b == 0 return 1; long t = modExpa, b2, n; long c = ttn; if b2 == 1 c = can; Universitas Sumatera Utara return intc; } public boolean checkString x{ boolean hsl = true; int a = Math.roundx.length2; forint i=0; ia; ++i if x.charAti=x.charAti+a hsl=false; return hsl; } public boolean fermatint p{ boolean hasil=true; Random rand = new Random; int a=rand.nextInt20+1; if ap==0{ hasil=false; } else if modExpa,p-1,p=1{ hasil=false; } return hasil; } public String singlebinString x{ String n=; forint y=0; yMath.floorx.length2; y++{ n= n+x.charAty; } return n; } Override public void onBackPressed { Intent link = new IntentLihatPesan.this, DataPesan.class; Intent i = getIntent; link.putExtratipepesan, i.getStringExtraasal; startActivitylink; }}

4. MainActivity.java