Implementasi Kombinasi Algoritma Vigenere Cipher dan Myszkowski Transposition Pada Aplikasi Penyandian Pesan Berbasis Android

A-1

LISTING PROGRAM

1. MainActivity.java
package com.example.ilmukomputer.mycipher;
import
import
import
import
import
import
import
import
import
import
import
import
import

android.content.Intent;

android.graphics.Color;
android.os.Bundle;
android.support.v7.app.AppCompatActivity;
android.text.InputType;
android.view.View;
android.widget.CheckBox;
android.widget.CompoundButton;
android.widget.EditText;
android.widget.ImageButton;
android.widget.TabHost;
android.widget.TextView;
android.widget.Toast;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tab = (TabHost) findViewById(R.id.tabHost);

tab.setup();
TabHost.TabSpec spec1 = tab.newTabSpec("Pengamanan");
spec1.setIndicator("PENGAMANAN");
spec1.setContent(R.id.layout1);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("Pengungkapan");
spec2.setIndicator("PENGUNGKAPAN");
spec2.setContent(R.id.layout2);
tab.addTab(spec2);
for(int i=0;i 0 && key1.length() >= 2 && key2.length() >= 2) {
if(pesan.length() >= key1.length() && pesan.length() >= key2.length()) {
for(i=0; i 126){
return false;
}
}
for(i=0; i 126){
return false;
}

Universitas Sumatera Utara


A-2

}
for(i=0; i 57){
return false;
}
}
}
else{
return false;
}
}
else{
return false;
}
return true;
}
private void pengamanan(){
final EditText et_plaintext_en = (EditText)

findViewById(R.id.et_plaintext_en);
final EditText et_key1_en = (EditText) findViewById(R.id.et_key1_en);
final EditText et_key2_en = (EditText) findViewById(R.id.et_key2_en);
final TextView tv_ciphertext_en = (TextView)
findViewById(R.id.tv_ciphertext_en);
ImageButton b_encrypt = (ImageButton) findViewById(R.id.b_encrypt);
b_encrypt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String plaintext = et_plaintext_en.getText().toString();
String key1 = et_key1_en.getText().toString();
String key2 = et_key2_en.getText().toString();
if (checkInput(plaintext, key1, key2)) {
String key = "", ciphertext1 = "", ciphertext2 = "";
int i, j, k, pesan, kecil = 0, tmp = 0;
//enkripsi vigenere
for (i = 0; i < plaintext.length(); i++) {
j = i % key1.length();
key += key1.charAt(j);
}

for (i = 0; i < plaintext.length(); i++) {
j = (int) plaintext.charAt(i);
k = (int) key.charAt(i);
pesan = ((j - 32) + (k - 32)) % 95;
pesan += 32;
ciphertext1 += (char) pesan;
}
//ciphertext2 = ciphertext1;
//ciphertext1 = plaintext;
//enkripsi myszkowski
int kol = key2.length(), bar = (int) Math.ceil((double)
ciphertext1.length() / kol);
char[][] matrix = new char[bar][kol];
for (i = 0; i < bar; i++) {
for (j = 0; j < kol; j++) {
if (tmp < ciphertext1.length()) {
matrix[i][j] = ciphertext1.charAt(tmp);
tmp++;
} else {
matrix[i][j] = Character.MIN_VALUE;

}
}
}
tmp = -1;
while (ciphertext2.length() < ciphertext1.length()) {
for (i = 0; i < kol; i++) {
if (Integer.parseInt(key2.charAt(i) + "") > tmp) {
kecil = Integer.parseInt(key2.charAt(i) + "");

Universitas Sumatera Utara

A-3

break;
}
}
for (i = 0; i < kol; i++) {
if (Integer.parseInt(key2.charAt(i) + "") > tmp &&
Integer.parseInt(key2.charAt(i) + "") < kecil) {
kecil = Integer.parseInt(key2.charAt(i) + "");

}
}
for (i = 0; i < bar; i++) {
for (j = 0; j < kol; j++) {
if (Integer.parseInt(key2.charAt(j) + "") == kecil &&
matrix[i][j] != Character.MIN_VALUE) {
ciphertext2 += matrix[i][j];
}
}
}
tmp = kecil;
}
tv_ciphertext_en.setText(ciphertext2);
Toast.makeText(MainActivity.this, "Pengamanan Sukses",
Toast.LENGTH_LONG).show();
}
else{
tv_ciphertext_en.setText("");
Toast.makeText(MainActivity.this, "Periksa Kembali Inputan Anda",
Toast.LENGTH_LONG).show();

}
}
});
}
private void pengungkapan(){
final EditText et_ciphertext_de = (EditText)
findViewById(R.id.et_ciphertext_de);
final EditText et_key1_de = (EditText) findViewById(R.id.et_key1_de);
final EditText et_key2_de = (EditText) findViewById(R.id.et_key2_de);
final TextView tv_plaintext_de = (TextView)
findViewById(R.id.tv_plaintext_de);
ImageButton b_decrypt = (ImageButton) findViewById(R.id.b_decrypt);
b_decrypt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String ciphertext2 = et_ciphertext_de.getText().toString();
String key1 = et_key1_de.getText().toString();
String key2 = et_key2_de.getText().toString();
if (checkInput(ciphertext2, key1, key2)) {
String key="", ciphertext1="", plaintext="";

int i, j, k=0, pesan, kecil=0, tmp=-1;
//dekripsi myszkowski
int kol = key2.length(), bar = (int) Math.ceil((double)
ciphertext2.length()/kol);
char[][] matrix = new char[bar][kol];
while(k < ciphertext2.length()) {
for(i=0; i tmp) {
kecil = Integer.parseInt(key2.charAt(i) + "");
break;
}
}
for(i=0; i tmp &&
Integer.parseInt(key2.charAt(i) + "") < kecil) {
kecil = Integer.parseInt(key2.charAt(i) + "");
}
}
for(i=0; i