LISTING PROGRAM
Kode Program Form Home (MainActivity.java) :
package com.example.simpleencryption;
import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.Intent;
import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//pindah ke main menu
Button main_menu = (Button) findViewById(R.id.btn_menu); main_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View menu) {
Intent myIntent = new Intent(menu.getContext(), MenuActivity.class);
startActivityForResult(myIntent, 0); }
}); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); return true;
}
//pilihan menu item @Override
public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){
Intent helpIntent = new Intent(MainActivity.this, HelpActivity.class);
startActivityForResult(helpIntent, 0); break;
case R.id.itemAbout:
Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
startActivityForResult(aboutIntent, 0); break;
case R.id.itemExit: exit(); break; }
return false; }
//fungsi alert dialog button exit
public void exit(){
AlertDialog.Builder keluar = new AlertDialog.Builder(this); keluar.setMessage("Are you sure want to
exit?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) { MainActivity.this.finish();
} })
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) { dialog.cancel();
} }).show(); }
//fungsi alert dialog ketika tombol back android ditekan
public boolean onKeyDown(int keyCode, KeyEvent event){ if (keyCode== KeyEvent.KEYCODE_BACK) {
exit(); }
return super.onKeyDown(keyCode, event); }
}
Kode Program Form Main Menu (MenuActivity.java) :
package com.example.simpleencryption;
import android.view.MenuItem; import android.view.View; import android.widget.Button;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
getActionBar().setDisplayHomeAsUpEnabled(true);
//pindah ke menu enkripsi
Button encryption_menu = (Button) findViewById(R.id.btn_menuEnkripsi);
encryption_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View encryption_menu) { Intent myIntent = new
Intent(encryption_menu.getContext(), EncryptionActivity.class); startActivityForResult(myIntent, 0); }
});
//pindah ke menu dekripsi
Button decryption_menu = (Button) findViewById(R.id.btn_menuDekripsi);
decryption_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View decryption_menu) { Intent myIntent = new
Intent(decryption_menu.getContext(), DecryptionActivity.class); startActivityForResult(myIntent, 0); }
}); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu); return true;
}
//pilihan menu item @Override
public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){
case R.id.itemHelpMenu:
Intent helpIntent = new Intent(MenuActivity.this, HelpActivity.class);
case R.id.itemAboutMenu:
Intent aboutIntent = new Intent(MenuActivity.this, AboutActivity.class);
startActivityForResult(aboutIntent, 0); break;
}
return false; }
}
Kode Program Form Menu Encryption (EncryptionActivity.java) :
package com.example.simpleencryption;
import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View;
import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.content.Intent;
public class EncryptionActivity extends Activity {
EditText plaintext, kunci_XOR, kunci_RC4, ciphertext; TextView txt_counter, keyXOR_counter, keyRC4_counter;
String plainteks, key_XOR, key_RC4, cipherteks_XOR, cipherteks_RC4; int counter, counter2, counter3;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.encryption_menu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
plaintext = (EditText) findViewById(R.id.plaintext); kunci_XOR = (EditText) findViewById(R.id.key_XOR); kunci_RC4 = (EditText) findViewById(R.id.key_RC4); ciphertext = (EditText) findViewById(R.id.ciphertext); txt_counter = (TextView) findViewById(R.id.text_counter); keyXOR_counter = (TextView) findViewById(R.id.keyXOR_counter); keyRC4_counter = (TextView) findViewById(R.id.keyRC4_counter);
//menghitung jumlah karakter plaintext
plaintext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
txt_counter.setText(plaintext.length() + " "); counter = count;
}
@Override
public void afterTextChanged(Editable s) {
}
});
//menghitung jumlah karakter kunci XOR
kunci_XOR.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s2, int start2, int count2, int after2) {
}
@Override
public void onTextChanged(CharSequence s2, int start2, int before2, int count2) {
keyXOR_counter.setText(kunci_XOR.length() + " "); counter2 = count2;
}
@Override
public void afterTextChanged(Editable s2) {
} });
//menghitung jumlah karakter kunci RC4
kunci_RC4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s3, int start3, int count3, int after3) {
}
@Override
public void onTextChanged(CharSequence s3, int start3, int before3, int count3) {
keyRC4_counter.setText(kunci_RC4.length() + " "); counter3 = count3;
}
@Override
public void afterTextChanged(Editable s3) {
//button enkripsi
Button btn_encryption = (Button) findViewById(R.id.btn_encryption);
btn_encryption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
plainteks = plaintext.getText().toString(); key_XOR = kunci_XOR.getText().toString(); key_RC4 = kunci_RC4.getText().toString();
String plainteks_check = plainteks; if(plainteks_check.equals("")){
Toast.makeText(getApplicationContext(), "Input Plaintext!", Toast.LENGTH_SHORT).show();
} else {
try {
//fungsi jika panjang key_XOR > plainteks
if(key_XOR.length() > plainteks.length()){
Toast.makeText(getApplicationContext(), "Key XOR Length > Plaintext Length!", Toast.LENGTH_SHORT).show();
ciphertext.setText(""); } else {
//start time
long start_time = System.currentTimeMillis();
//fungsi jika panjang key_XOR < plainteks
if(key_XOR.length() < plainteks.length()) { String tempkey_XOR = "";
for(int i=0; i<plainteks.length(); i++)
tempkey_XOR = tempkey_XOR + key_XOR.substring(0); key_XOR = tempkey_XOR;
}
cipherteks_XOR = encryptionXOR(plainteks); //enkripsi dgn algoritma XOR
RC4 rc4 = new RC4(key_RC4); //set key_RC4
char[] result = rc4.encryptionRC4(cipherteks_XOR.toCharArray()); //enkripsi dgn algotima RC4
cipherteks_RC4 = new String(result);
ciphertext.setText(cipherteks_RC4);
ciphertext.setFocusable(true);
ciphertext.requestFocus();
//end time
Toast.makeText(getApplicationContext(), "Encryption Succes! Time: " + duration + " ms", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) { e.printStackTrace();
Toast.makeText(getApplicationContext(), "Complete The Blank Form Above!", Toast.LENGTH_SHORT).show();
} }
} });
//button clear
Button btn_clear = (Button) findViewById(R.id.btn_clear); btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { clear();
} });
//button share
Button btn_share = (Button) findViewById(R.id.btn_share); btn_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { shareCiphertext(); }
}); }
//fungsi enkripsi XOR
public String encryptionXOR(String str){
int plain_code[] = new int[plainteks.length()]; int key_code[] = new int[key_XOR.length()];
int cipher_code[] = new int[plainteks.length()]; char cipher_char[] = new char[plainteks.length()];
for (int i = 0; i < plainteks.length(); i++) {
plain_code[i] = turnToNumber(plainteks.charAt(i)); key_code[i] = turnToNumber(key_XOR.charAt(i)); cipher_code[i] = plain_code[i] ^ key_code[i]; cipher_char[i] = turnToChar(cipher_code[i]); }
String cipher_XOR = new String(cipher_char);
return cipher_XOR; }
//fungsi button share
private void shareCiphertext(){
Intent share = new Intent(android.content.Intent.ACTION_SEND); share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, ciphertext.getText().toString());
startActivity(Intent.createChooser(share, "Share Ciphertext Via"));
}
//fungsi button clear
public void clear(){
plaintext.setText(""); kunci_XOR.setText(""); kunci_RC4.setText(""); ciphertext.setText(""); }
//fungsi mengubah char ke int
public static int turnToNumber(char str){
String karakterlist = "
!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvw
xyz{|}~€ … ˆ‰’ ¦Ž •–—
˜™š œž²¡ ¤ ¦ ¨© «¬®¯°± ´µ •¸¹ » ¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖפ‟ÚÛÜݧß{|}~
åæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"+"\n";
for (int i=0; i<karakterlist.length(); i++){ if(str==karakterlist.charAt(i))
return i; }
return 0; }
//fungsi mengubah int ke char
public static char turnToChar(int str){
String karakterlist = "
!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvw
xyz{|}~€ … ˆ‰’ ¦Ž •–—
˜™š œž²¡ ¤ ¦ ¨© «¬®¯°± ´µ •¸¹ » ¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖפ‟ÚÛÜݧß{|}~
åæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"+"\n";
for (int i=0; i<karakterlist.length(); i++){ if(str==i)
return karakterlist.charAt(i); }
return 0; }
Kode Program Form Menu Decryption (DecryptionActivity.java) :
package com.example.simpleencryption;
import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View;
import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.content.Intent;
public class DecryptionActivity extends Activity {
EditText plaintext_dec, kunci_XORdec, kunci_RC4dec, ciphertext_dec; TextView txtDec_counter, keyXORdec_counter, keyRC4dec_counter; String plainteks, key_XOR, key_RC4, cipherteks_XOR, cipherteks_RC4; int counter, counter2, counter3;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.decryption_menu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
ciphertext_dec = (EditText) findViewById(R.id.ciphertext_dec); kunci_XORdec = (EditText) findViewById(R.id.key_XORdec); kunci_RC4dec = (EditText) findViewById(R.id.key_RC4dec); plaintext_dec = (EditText) findViewById(R.id.plaintext_dec); txtDec_counter = (TextView) findViewById(R.id.textDec_counter); keyXORdec_counter = (TextView) findViewById(R.id.keyXORdec_counter); keyRC4dec_counter = (TextView) findViewById(R.id.keyRC4dec_counter);
//menghitung jumlah karakter ciphertext
ciphertext_dec.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
txtDec_counter.setText(ciphertext_dec.length() + " "); counter = count;
}
@Override
}
});
//menghitung jumlah karakter kunci XOR
kunci_XORdec.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s2, int start2, int count2, int after2) {
}
@Override
public void onTextChanged(CharSequence s2, int start2, int before2, int count2) {
keyXORdec_counter.setText(kunci_XORdec.length() + " ");
counter2 = count2; }
@Override
public void afterTextChanged(Editable s2) {
} });
//menghitung jumlah karakter kunci RC4
kunci_RC4dec.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s3, int start3, int count3, int after3) {
}
@Override
public void onTextChanged(CharSequence s3, int start3, int before3, int count3) {
keyRC4dec_counter.setText(kunci_RC4dec.length() + " ");
counter3 = count3; }
@Override
public void afterTextChanged(Editable s3) {
} });
//button dekripsi
Button btn_decryption = (Button) findViewById(R.id.btn_decryption);
btn_decryption.setOnClickListener(new View.OnClickListener() {
@Override
cipherteks_RC4 = ciphertext_dec.getText().toString();
key_XOR = kunci_XORdec.getText().toString(); key_RC4 = kunci_RC4dec.getText().toString();
String cipherteks_check = cipherteks_RC4; if(cipherteks_check.equals("")){
Toast.makeText(getApplicationContext(), "Input Ciphertext!", Toast.LENGTH_SHORT).show();
} else {
try {
RC4 rc4 = new RC4(key_RC4); //set keyRC4
char[] result = rc4.encryptionRC4(cipherteks_RC4.toCharArray()); //dekripsi dgn algoritma RC4
cipherteks_XOR = new String(result);
//fungsi jika panjang key_XOR > cipherteksXOR
if(key_XOR.length() > cipherteks_XOR.length()){
Toast.makeText(getApplicationContext(), "Key XOR Length > Ciphertext Length!", Toast.LENGTH_SHORT).show();
plaintext_dec.setText(""); } else {
//start time
long start_time = System.currentTimeMillis();
//fungsi jika panjang keyXOR < cipherteksXOR
if(key_XOR.length() < cipherteks_XOR.length()){
String tempkey_XOR = "";
for(int i=0; i<cipherteks_XOR.length(); i++)
tempkey_XOR = tempkey_XOR + key_XOR.substring(0); key_XOR = tempkey_XOR;
}
plainteks = decryptionXOR(cipherteks_XOR); //dekripsi dgn algoritma XOR
plaintext_dec.setText(plainteks);
plaintext_dec.setFocusable(true);
plaintext_dec.requestFocus();
//end time
long end_time = System.currentTimeMillis(); long duration = (end_time - start_time);
Toast.makeText(getApplicationContext(), "Decryption Succes! Time: " + duration + " ms", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "Complete The Blank Form Above!", Toast.LENGTH_SHORT).show();
} }
} });
//button clear
Button btn_clear = (Button) findViewById(R.id.btn_clearDec); btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { clear();
} });
//button share
Button btn_share = (Button) findViewById(R.id.btn_shareDec); btn_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { sharePlaintext(); }
});
}
//fungsi dekripsi XOR
public String decryptionXOR(String str){
int cipher_code[] = new int[cipherteks_XOR.length()]; int key_code[] = new int[key_XOR.length()];
int plain_code[] = new int[cipherteks_XOR.length()]; char plain_char[] = new char[cipherteks_XOR.length()];
for (int i = 0; i < cipherteks_XOR.length(); i++) {
cipher_code[i] = turnToNumber(cipherteks_XOR.charAt(i)); key_code[i] = turnToNumber(key_XOR.charAt(i));
plain_code[i] = cipher_code[i] ^ key_code[i]; plain_char[i] = turnToChar(plain_code[i]); }
String plain_XOR = new String(plain_char);
return plain_XOR; }
//fungsi button share
private void sharePlaintext(){
Intent share = new Intent(android.content.Intent.ACTION_SEND); share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); share.putExtra(Intent.EXTRA_TEXT,
startActivity(Intent.createChooser(share, "Share Plaintext Via"));
}
//fungsi button clear
public void clear(){
plaintext_dec.setText(""); kunci_XORdec.setText(""); kunci_RC4dec.setText(""); ciphertext_dec.setText(""); }
//fungsi mengubah char ke int
public static int turnToNumber(char str){
String karakterlist = "
!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvw
xyz{|}~€ … ˆ‰’ ¦Ž •–—
˜™š œž²¡ ¤ ¦ ¨© «¬®¯°± ´µ •¸¹ » ¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖפ‟ÚÛÜݧß{|}~
åæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"+"\n";
for (int i=0; i<karakterlist.length(); i++){ if(str==karakterlist.charAt(i))
return i; }
return 0; }
//fungsi mengubah int ke char
public static char turnToChar(int str){
String karakterlist = "
!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvw
xyz{|}~€ … ˆ‰’ ¦Ž •–—
˜™š œž²¡ ¤ ¦ ¨© «¬®¯°± ´µ •¸¹ » ¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖפ‟ÚÛÜݧß{|}~
åæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"+"\n";
for (int i=0; i<karakterlist.length(); i++){ if(str==i)
return karakterlist.charAt(i); }
return 0; }
}
Kode Program Form Menu Help (HelpActivity.java) :
package com.example.simpleencryption;
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView;
import android.text.method.ScrollingMovementMethod;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.help_menu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
TextView txt_help = (TextView) findViewById(R.id.text_help); txt_help.setMovementMethod(new ScrollingMovementMethod()); txt_help.setText("--> Untuk membuat pesan atau informasi menjadi rahasia/dienkripsi"
+ " silahkan pilih Menu kemudian pilih menu Encryption. Lalu input pesan/plainteks awal"
+ " pada form Input Plaintext Kemudian input kunci XOR dan Kunci RC4."
+ " Kedua kunci ini merupakan kunci rahasia yang harus anda" + " jaga kerahasiannya. Lalu pilih tombol Encryption"
+ " dan aplikasi akan menampilkan pesan yang telah dirahasiakan/cipherteks. Kemudian"
+ " pilih tombol Share jika anda ingin membagi/mengirim cipherteks tersebut via aplikasi"
+ " pihak ketiga seperti pesan, Gmail, WhatsApp, dll." + "\n\n--> Untuk mengubah cipherteks menjadi pesan asli/didekripsi silahkan pilih"
+ " Menu kemudian pilih menu Decryption. Lalu input pesan yang telah terenkripsi"
+ " /cipherteks pada form Input Ciphertext. Input kunci XOR dan kunci RC4 sesuai dengan"
+ " proses enkripsi sebelumnya. Lalu pilih tombol Decryption dan aplikasi akan"
+ " menampilkan pesan asli/plainteks. Kemudian pilih tombol Share jika anda ingin membagi"
+ "/mengirim plainteks tersebut via aplikasi pihak ketiga seperti pesan, Gmail, WhatsApp, dll."
+ "\n\n--> Saran kreator bedakan penggunaan kunci antara kunci XOR dengan kunci RC4"
+ " sehingga cipherteks tidak mudah dipecahkan");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.help, menu); return true;
}
}
Kode Program Form Menu About (AboutActivity.java) :
package com.example.simpleencryption;
import android.os.Bundle; import android.app.Activity;
import android.view.Menu; import android.widget.TextView;
public class AboutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.about_menu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
TextView txt_about = (TextView) findViewById(R.id.text_about); txt_about.setMovementMethod(new ScrollingMovementMethod()); txt_about.setText("This Application Created By \nTanta Aditya Pranata a.k.a Zero" +
"\nCopyright ©2015" + "\n\nSpecial thanks to:" + "\n\n-Allah SWT yang telah memberikan kreator kekuatan, kesehatan dan ilmu pengetahuan untuk mengerjakan aplikasi ini" +
"\n\n-Keluarga yang merupakan sumber inspirasi dan motivasi terbesar untuk mengerjakan aplikasi ini" +
"\n\n-Pak Poltak dan Bu Amel sebagai dosen pembimbing yang telah membimbing dan mengarahkan kreator sehingga dapat menyelesaikan aplikasi ini" +
"\n\n-Nurhayati Lubis, S.Kom (Cabe) yang telah memberi dukungan dan semangat kepada kreator sehingga kreator rajin mengerjakan program" +
"\n\n-4TRA, Nandes, dan Anandini yang telah memberi inovasi code sehingga kreator dapat membuat source code sesuai dengan penelitian" +
"\n\n-Laptop yang telah berjuang T_T meskipun kondisi mu sudah tak sanggup lagi menjalankan program2 jaman sekarang" +
"\n\n-Stackoverflow dan Stackoverchange sebagai situs sumber pengetahuan kreator ketika banyak eror terjadi" +
"\n\n-Serta teman-teman yang secara langsung maupun tidak langsung turut membantu dalam pembuatan aplikasi ini");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about, menu); return true;
}
}
Kode Program Algoritma RC4 (RC4.java) :
package com.example.simpleencryption;
import java.security.InvalidKeyException;
public class RC4 {
private int[] SBox;
private static final int sboxLength = 256;
public RC4(String key) throws InvalidKeyException { setKey(key);
}
public char[] encryptionRC4(final char[] plain) { SBox = initSBox(key);
char[] cipher = new char[plain.length]; int i = 0;
int j = 0;
//PRGA
for (int n = 0; n < plain.length; n++) { i = (i + 1) % sboxLength;
j = (j + SBox[i]) % sboxLength; Swap(i, j, SBox);
int rand = SBox[(SBox[i] + SBox[j]) % sboxLength]; cipher[n] = (char) (rand ^ (int) plain[n]);
}
return cipher; }
//KSA
private int[] initSBox(char[] key) { int[] SBox = new int[sboxLength]; int j = 0;
//Sbox awal
for (int i = 0; i < sboxLength; i++) { SBox[i] = i;
}
//Sbox akhir
for (int i = 0; i < sboxLength; i++) {
j = (j + SBox[i] + key[i % key.length]) % sboxLength; Swap(i, j, SBox);
}
return SBox; }
private void Swap(int i, int j, int[] SBox) { int temp = SBox[i];
SBox[i] = SBox[j]; SBox[j] = temp; }
public void setKey(String key) throws InvalidKeyException { this.key = key.toCharArray();
}