Jumat, 15 Februari 2019
Android Kriptograph RSA+AES
Pada Menu Login:
Kode Login:
Button btnLogin= (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user=txtusername.getText().toString();
String pass=txtpassword.getText().toString();
if(user.length()<1){lengkapi("Username");}
else if(pass.length()<1){lengkapi("Password");}
else{
int index=-1;
if(pilih.equalsIgnoreCase("Karyawan")){
for(int i=0;i<jd;i++){
if(user.equalsIgnoreCase(arNik[i]) && pass.equalsIgnoreCase(arNik[i])){
index=i;
break;
}
}
if(index<0){
gagal();
}
else{
key=arPrivate[index]+"#"+arN[index]+"#"+arNama[index]+"#"+arNik[index]+"#";
sukses("Karyawan "+arNama[index]);//
}
}
else{
if(user.equalsIgnoreCase("admin") && pass.equalsIgnoreCase("admin")){sukses("Admin");}
else if(user.equalsIgnoreCase("hrd") && pass.equalsIgnoreCase("hrd")){sukses("HRD");}
else{gagal();}
}
}
}
});
Jadi jika sebagai admin atau hrd akan menuju ke halaman Enkrip dan Pengujian
dan jika sebagai karyawan akan menuju ke halaman Dekrip dan Buka Dokumen
di halaman Index juga tersedia initialisasi dataUji yaitu daftar nama nama karyawan yang bisa menggunakan aplikasi ini (sample contoh):
Inisialisasi dataUji:
void loadkar(){
jd=5;
arNik=new String[jd];
arNama=new String[jd];
arNik[0]="50815317";
arNama[0]="Geum Jan Di";
arNik[1]="24117371";
arNama[1]="Gu Jun Pyo";
arNik[2]="10616173";
arNama[2]="Yoon Ji Hoo";
arNik[3]="19515731";
arNama[3]="Song Woo Bin";
arNik[4]="70114713";
arNama[4]="So Yi Jung";
}
Pada Menu utama Admin/HRD
Kode Saat memilih dokumen (Open file):
txtnama.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try {
Intent gintent = new Intent();
Uri mydir = Uri.parse(mypath);
gintent.setDataAndType(mydir, "*/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gintent, "Select Data"),PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
Kode Saat Button Proses ditekan:
Tergantung Option radiobutton yang dipilih sebelumnya:
btnProcess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
cetak(filePath);
if( statusbaca.equalsIgnoreCase("Encrypt")){
enkrip();
Toast.makeText(Aplikasi.this, "Proses Encrypt "+filePath, Toast.LENGTH_SHORT).show();
}
else if( statusbaca.equalsIgnoreCase("Decrypt")){
dekrip();
Toast.makeText(Aplikasi.this, "Proses Decrypt "+filePath, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Aplikasi.this, "Load Path "+filePath, Toast.LENGTH_SHORT).show();
File x=new File(filePath);
openFile(x);
}
SBB adalah kode Open File sesuai extensi file ybs:
private void openFile(File url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
Uri uri = Uri.fromFile(url);
if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
// Word document
intent.setDataAndType(uri, "application/msword");
} else if (url.toString().contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf");
} else if (url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
// Powerpoint file
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if (url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
// Excel file
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if (url.toString().contains(".zip") || url.toString().contains(".rar")) {
// WAV audio file
intent.setDataAndType(uri, "application/x-wav");
} else if (url.toString().contains(".rtf")) {
// RTF file
intent.setDataAndType(uri, "application/rtf");
} else if (url.toString().contains(".wav") || url.toString().contains(".mp3")) {
// WAV audio file
intent.setDataAndType(uri, "audio/x-wav");
} else if (url.toString().contains(".gif")) {
// GIF file
intent.setDataAndType(uri, "image/gif");
} else if (url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
// JPG file
intent.setDataAndType(uri, "image/jpeg");
} else if (url.toString().contains(".txt")) {
// Text file
intent.setDataAndType(uri, "text/plain");
} else if (url.toString().contains(".3gp") || url.toString().contains(".mpg") ||
url.toString().contains(".mpeg") || url.toString().contains(".mpe") || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
// Video files
intent.setDataAndType(uri, "video/*");
} else {
intent.setDataAndType(uri, "*/*");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application found which can open the file", Toast.LENGTH_SHORT).show();
}
}
Proses Enkrip:
1.Membuat File menjadi byte
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(nf);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];//8192
int bytesRead=0;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
byte bytePath[] = output.toByteArray();
2.data Byte array yang didapatkan di RSA KAN
byte []data=bytePath;
String[] data1 = new String[data.length];
for(int i=0; i < data.length; i++) {
data1[i] = String.valueOf(data[i] & 0xff); //unsigned integer
}
BigInteger[] data2 = new BigInteger[data1.length];
for (int i = 0; i < data1.length; i++) {
data2[i] = (new BigInteger(data1[i])).pow(this.e);
data2[i] = data2[i].mod(this.n);
}
String[] x = new String[data2.length];
for(int i = 0; i < data2.length; i++) {
x[i] = String.valueOf(data2[i]);
}
String data5 = StringUtils.join(x, " ");
3.String data hasil RSA di AES kan
AES128 aes128 = new AES128();
String cipherText = aes128.encrypt(data5,256,passAES);
4.hasil Chipertext RSA+AES ahir disimpan ke dalam file hasil dekrip
try {
String content = dataEncrypt;
File file = new File(this.fileNameEncrypt);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
5.Hasil File bisa di open untuk melihat hasilnya menggunaan kode open file berdasarkan extensi di atas
PROSES DEKRIP:
1.Membuat File menjadi deretan String
BufferedReader be = null;
String current;
String data = "";
be = new BufferedReader(new FileReader(fileName));
while ((current = be.readLine()) != null) {
data += current;
}
2.String data adalah isi daripada file Enkripsi sebelumnya selanjutnya melakukan proses Dekripsi
pertama data String tersebut di dekrip AES
AES128 aes128 = new AES128();
String plainText = aes128.decrypt(data, 256, passAES);
3.String Dekrip AES selanjutnya di Dekrip RSA
String[] data1 = plainText.split(" ");
BigInteger[] data2 = new BigInteger[data1.length];
for (int i=0; i < data1.length; i++){
data2[i] = (new BigInteger(String.valueOf(data1[i]))).pow(this.d);
data2[i] = data2[i].mod(this.n);
}
byte[] data3 = new byte[data2.length];
String[] x = new String[data2.length];
String gab="";
for (int i=0; i < data2.length; i++){
data3[i] = (byte) (data2[i].byteValue());
}
4.Hasil byte array yang di dapat selanjutnay disimpan ke dalam file
File file=new File("Nama File Dekrip");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data3);
fileOutputStream.flush();
fileOutputStream.close();
return file.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
5.Hasil File bisa di open untuk melihat hasilnya menggunaan kode open file berdasarkan extensi di atas
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar