Listing Program
A. Hardware
void main(void) {
PORTA.0=0;
//Tampilan Home lcd_gotoxy(0,0);
lcd_puts("Doorlock System"); lcd_gotoxy(0,1);
lcd_puts("One Time Pad"); delay_ms(3000);
goto awal; awal:;
//Tampilan Status DoorLock lcd_clear();
lcd_gotoxy(0,0); if(flag==0){
PORTA.0=0;
lcd_puts("--LOCKED--"); delay_ms(5000);
}else if(flag==1){ PORTA.0=1;
lcd_puts("--UNLOCKED--"); delay_ms(5000);
}
delay_ms(100); while (1)
{
scanf("%s",req);
if(strcmp(req, "lock")==0){ //lock();
lcd_clear(); lcd_gotoxy(0,0); lcd_puts("Key :"); lcd_gotoxy(0,1);
lcd_puts("Password :"); delay_ms(1000);
//Input Key dan Password scanf("%s", kunci); scanf("%s", pass); lcd_clear();
//Tampilkan Key dan Password lcd_gotoxy(0,0);
lcd_puts(kunci); lcd_gotoxy(0,1); lcd_puts(pass); delay_ms(1000);
//Simpan password ke EEPROM for(i=0;i<sizeof(pass);i++){
password[i]= pass[i];
lcd_buff[i]= (char) password[i]; }
//Tampilkan Passsword dari EEPROM lcd_clear();
lcd_gotoxy(0,0); lcd_puts(lcd_buff); delay_ms(1000); */
//Simpan Key ke EEPROM
for(i=0;i<sizeof(kunci);i++){ key[i]= kunci[i];
lcd_buff[i]= (char) key[i]; }
/*
//Tampilkan Key dari EEPROM lcd_clear();
lcd_gotoxy(0,0); lcd_puts(lcd_buff); delay_ms(1000); */
//Status Lock flag=0;
goto awal;
}else if(strcmp(req, "unlock")==0){ //unlock();
lcd_gotoxy(0,0);
lcd_puts("Password :"); delay_ms(1000);
scanf("%s",cipher); lcd_puts(cipher); delay_ms(1000); lcd_clear();
//Ambil Key dari EEPROM for(i=0;i<10;i++){
kunci[i] = (char) key[i]; lcd_buff[i]=kunci[i]; }
/*lcd_gotoxy(1,0); lcd_puts(lcd_buff); delay_ms(3000); lcd_clear();*/
//Dekripsi Cipherteks for(i=0;i<10;i++){
temp=0;
if(cipher[i]<126){
temp= cipher[i]+126-31; }
plain[i] =(char) (temp - key[i]); lcd_buff[i]=plain[i];
}
//Hasil Dekripsi lcd_gotoxy(0,0); lcd_puts(lcd_buff); delay_ms(3000); lcd_clear();
for(i=0;i<10;i++){
pass[i] = (char )password[i]; lcd_buff[i]=pass[i];
} /*
lcd_gotoxy(0,0); lcd_puts(lcd_buff); delay_ms(3000); */
//Cek Kecocokan Password for(i=0;i<10;i++){
if(plain[i]== pass[i]){ stat=1;
}else{ stat=0; break; }
}
if (stat==1){
//Password Benar flag=1;
}else{
//Salah Password lcd_clear();
lcd_puts("Wrong Password"); delay_ms(1000);
}
goto awal; }else{
//Salah Menu lcd_gotoxy(0,0);
lcd_puts("Tidak ada pilihan"); delay_ms(500);
} } }
B. Android
public class LockActivity extends Activity {
//SecureRandom randomGenerator = new SecureRandom(); BBSprng BBS = new BBSprng();
//BigInteger rand = BBS.generatorBBS(2); //keytxt.setText(rand.toString());
int bbs=0;
//Random randomGenerator = new Random(); for (int i = 1; i<=10; i++)
{
/*long seed = System.nanoTime();
long randomInt = (seed * seed *
randomGenerator.nextInt(10000)); if(randomInt<0){
randomInt = - randomInt; }*/
if(bbs < 0){ bbs = -bbs; }
int y = bbs % 26;
key= key + (char) (65 + y); }
//plaintxt.setText(Integer.toString(bbs)); keytxt.setText(key);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null)
{
Label.setText("No bluetooth adapter available"); }
if(!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0); }
}
// On Start Activity @Override
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "ON START"); //Open Connection
ButtonOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
SearchBT(); OpenBT();
sendData("lock"); }
catch (IOException ex) { } }
});
//Send plain and Key
ButtonLocked.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
//Encriypt teks
String key = keytxt.getText().toString(); String plain = plaintxt.getText().toString(); if(plain.length()<=10)
{
DatabaseHandler db = new
db.addKey(cipher, key); sendData(key);
sendData(plain); }else{
Toast.makeText(getApplicationContext(), "Panjang PlainTeks Harus 10", Toast.LENGTH_LONG).show();
} }
catch (Exception ex) { } }
});
//Close Connection
ButtonClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
closeBT(); }
catch (IOException ex) { } }
}); }
void SearchBT() {
if(D) Log.d(TAG, "Cari BT");
Set<BluetoothDevice> pairedDevices =
mBluetoothAdapter.getBondedDevices(); if(pairedDevices.size() > 0) {
for(BluetoothDevice device : pairedDevices) {
if(device.getName().equals("HC-05")) {
mmDevice = device;
Label.setText("Slave Client Found"); //break;
} else {
Label.setText("Slave Client Not Found"); }
// Register the BroadcastReceiver
/*IntentFilter filter = new
IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy*/
} } }
// Untuk memulai melakukan koneksi dengan client bluetooth void OpenBT() throws IOException
{
if(D) Log.d(TAG, "Buka BT");
UUID uuid =
SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); ListenForData();
Label.setText("Bluetooth Opened");
//Toast.makeText(this, "Bluetooth Dibuka", Toast.LENGTH_LONG).show();
}
// Background thread untuk menerima data dari client bluetooth void ListenForData()
{
if(D) Log.d(TAG, "Listen Data");
final byte delimiter = 13; //atau Carriage Return (CR), di gunakan untuk mendeteksi akhir dari text
stopWorker = false; readBufferPosition = 0; Buffer = new byte[1024];
Thread workerThread = new Thread(new Runnable() {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
//Do work try
{
int bytesAvailable = mmInputStream.available(); if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable]; mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++) {
byte b = packetBytes[i]; if(b == delimiter)
{
byte[] encodedBytes = new
byte[readBufferPosition];
System.arraycopy(Buffer, 0,
encodedBytes, 0, encodedBytes.length);
final String data = new
String(encodedBytes, "US-ASCII");
readBufferPosition = 0; Message m = new Message(); Bundle bund = new Bundle(); bund.putString("My Key", data); m.setData(bund);
h.sendMessage(m); }
else {
Buffer[readBufferPosition++] = b; }
}
catch (IOException ex) {
stopWorker = true; }
} } });
workerThread.start(); }
// Untuk mengirim data ke client bluetooth void sendData(String m) throws IOException {
if(D) Log.d(TAG, "Kirim Data"); m += "\n\r";
mmOutputStream.write(m.getBytes()); Label.setText("Data Sent");
//Toast.makeText(this, “Data Terkirim“,
Toast.LENGTH_LONG).show(); }
//Untuk Ekripsi One Time Pad
void Enkripsi(String key, String plain) throws IOException {
cipher=""; int temp=0; char c; //int sum;
for(int i= 0; i<key.length();i++) {
temp = key.charAt(i) + plain.charAt(i); if(temp>126){
temp=(temp-126)+31; }
c= (char) temp; cipher=cipher + c;
//Toast.makeText(getApplicationContext(), cipher ,Toast.LENGTH_LONG).show();
}
/*sum = 67 ^ 68; c= (char) sum; cipher=cipher+ c;*/ }
}
public class UnlockActivity extends Activity { try{
DatabaseHandler db = new
DatabaseHandler(getApplicationContext());
String key = db.getDetails().get("key"); String plain = db.getDetails().get("plain"); String cipher="";
Enkripsi(key, plain);
cipher=Ciphertxt.getText().toString(); Dekripsi(key, cipher);
}
//Send Cipher
ButtonUnlocked.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
DatabaseHandler db = new
DatabaseHandler(getApplicationContext());
String key = db.getDetails().get("key");
String cipher = db.getDetails().get("cipher"); if(cipher.length()>=10)
{
Ciphertxt.setText(cipher); //Dekripsi(key, cipher); //Send Cipher
sendData(cipher); }else{
Toast.makeText(getApplicationContext(), "Panjang Cipherteks Harus 10", Toast.LENGTH_LONG).show();
} }
catch (IOException ex) { } }
}); }
// Mencari Client Bluetooth yg sudah paired dengan nama “nutscientist“ void SearchBT()
{
if(D) Log.d(TAG, "Cari BT");
Set<BluetoothDevice> pairedDevices =
mBluetoothAdapter.getBondedDevices(); if(pairedDevices.size() > 0) {
for(BluetoothDevice device : pairedDevices) {
if(device.getName().equals("HC-05")) {
mmDevice = device;
Label.setText("Slave Client Found"); //break;
} else {
Label.setText("Slave Client Not Found"); }
// Register the BroadcastReceiver
/*IntentFilter filter = new
IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy*/
} } }
{
if(D) Log.d(TAG, "Buka BT");
UUID uuid =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); ListenForData();
Label.setText("Bluetooth Opened");
//Toast.makeText(this, "Bluetooth Dibuka", Toast.LENGTH_LONG).show();
}
// Background thread untuk menerima data dari client bluetooth void ListenForData()
{
if(D) Log.d(TAG, "Listen Data");
final byte delimiter = 13; //atau Carriage Return (CR), di gunakan untuk mendeteksi akhir dari text
stopWorker = false; readBufferPosition = 0; Buffer = new byte[1024];
Thread workerThread = new Thread(new Runnable() {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
//Do work try
{
int bytesAvailable = mmInputStream.available(); if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable]; mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++) {
byte b = packetBytes[i]; if(b == delimiter)
{
byte[] encodedBytes = new
byte[readBufferPosition];
System.arraycopy(Buffer, 0,
encodedBytes, 0, encodedBytes.length);
final String data = new
String(encodedBytes, "US-ASCII");
readBufferPosition = 0; Message m = new Message(); Bundle bund = new Bundle(); bund.putString("My Key", data); m.setData(bund);
h.sendMessage(m); }
else {
} } } }
catch (IOException ex) {
stopWorker = true; }
} } });
workerThread.start(); }
// Untuk mengirim data ke client bluetooth void sendData(String m) throws IOException {
if(D) Log.d(TAG, "Kirim Data"); m += "\n\r";
mmOutputStream.write(m.getBytes()); Label.setText("Data Sent");
//Toast.makeText(this, “Data Terkirim“,
Toast.LENGTH_LONG).show(); }
//Untuk Ekripsi One Time Pad
//www.life0fpentester.blogspot.com/2014/03/one-time-pad-encryptio n-decryption.html
void Enkripsi(String key, String plain) throws IOException {
String cipher=""; int temp=0;
char c; //int sum;
for(int i= 0; i<key.length();i++) {
temp = key.charAt(i) + plain.charAt(i); if(temp>126){
temp=(temp-126)+31; }
c= (char) temp; cipher=cipher + c;
//Toast.makeText(getApplicationContext(), cipher ,Toast.LENGTH_LONG).show();
}
/*sum = 67 ^ 68; c= (char) sum; cipher=cipher+ c;*/
Ciphertxt.setText(cipher); }
void Dekripsi(String key, String cipher) throws IOException {
String plain=""; int temp=0; char c; //int sum;
{
temp = cipher.charAt(i) - key.charAt(i); if(temp<126){
temp=(temp+126)-31; }
c= (char) temp; plain=plain + c;
//Toast.makeText(getApplicationContext(), cipher ,Toast.LENGTH_LONG).show();
}
/*sum = 67 ^ 68; c= (char) sum; cipher=cipher+ c;*/ Label.setText(plain); }