Sabtu, 27 September 2014
Algortima Chipper dalam java
import java.util.LinkedHashSet;
import java.util.Set;
public class Chiper {
//baris alphabet acuan
private String alp =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
wxyz0123456789 .,?!@()-_=+";//,./';[]!@#$%^&*()_+-=<>?\"{}`~\\
//key bawaan aplikasi, digunakan di inisialisasi jika key yang digunakan bukan bawaan aplikasi
private String key = "<oGHsoienJGDdop90?>";
//panjang alphabet, digunakan untuk penggeseran dan modulus alphabet
private int alen = this.alp.length();
//constructor umum
public Chiper(){
this.adjustKeyWord();
}
public Chiper(String phone){
if(!phone.equals(""))
this.key = this.mixKeyword(phone);
this.adjustKeyWord();
}
//constructor dengan keyword custom
public Chiper(String phone,String keyword){
if(!phone.equals(""))
this.key = this.mixKeyword(phone);
if(!keyword.equals(""))
this.key = this.mixKeyword(keyword);
this.adjustKeyWord();
}
//constructor dengan baris abjad custom dan keyword custom
public Chiper(String phone,String keyword,String alphabet){
this.alp = alphabet;
this.alen = this.alp.length();
if(!phone.equals(""))
this.key = this.mixKeyword(phone);
if(!keyword.equals(""))
this.key = this.mixKeyword(keyword);
this.adjustKeyWord();
}
//fungsi mixing 2 string keyword
private String mixKeyword(String keyword) {
String rkey = "";
int lbk = this.key.length();
int lik = keyword.length();
int tnk = lbk>lik ? lbk : lik ;
for(int i = 0;i<tnk;i++){
if(i<lbk)
rkey += this.key.charAt(i);
if(i<lik)
rkey += keyword.charAt(i);
}
return rkey;
}
private void adjustKeyWord(){
this.key = adjustKeyWord(this.key);
}
//fungsi adjustment key memastikan 1 buah char/g ad yg sama
private String adjustKeyWord(String keyword) {
String kw = "";
Set<Character> keyChars = new
LinkedHashSet<Character>(keyword.length());
for(char c : keyword.toCharArray()){
if((alp.indexOf(c) != -1) &&
keyChars.add(c)){
kw += String.valueOf(c);
}
}
//Log.i("keyword",kw);
return kw;
}
//fungsi fibonanci key
private void getFibonanciKey(int llen,int klen,String line){
//selisih panjang kalimat dengan panjang key
int loss = llen - klen;
//Log.i("loss",String.valueOf(loss));
//rumus fibonanci
int mt = 1;
for (char c : this.key.toCharArray()){
mt += this.alp.indexOf(c);
}
mt %= klen;
//Log.i("loss",String.valueOf(mt));
//END rumus fibonanci
String temkey = this.key;
while(temkey.length()<llen){
temkey += this.key;
}
//Log.i("repeater",temkey);
String holder = "";
int tmp2;
for(int i=0;i<loss;i++){
tmp2 = this.alp.indexOf(temkey.charAt(i)) + this.alp.indexOf(temkey.charAt(i+mt));
tmp2 += this.alen;
tmp2 %= this.alen;
holder += this.alp.charAt(tmp2);
}
this.key = mixKeyword(holder);
//Log.i("keywordFibo",this.key);
//return this.key;
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar