LISTING PROGRAM
1. Mainform
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace Skripsi_1 { /// <summary> /// Description of MainForm. /// </summary>public partial class MainForm : Form {
public char[,] kunci = new char[1000,1000];
public int[] barisw = new int[30];
public int[] cipherkunci = new int[1000];
public int bilm,biln,n;
public int kolkey, barkey;
public int nteks;
public MainForm() { InitializeComponent(); }
void ToolStripButton1Click(object sender, EventArgs e) {
}
void ToolStripButton2Click(object sender, EventArgs e) {
Form tentang = new About(); tentang.Show();
this.Hide(); }
void ENKRIPSIToolStripMenuItemClick(object sender, Eve ntArgs e)
{
Form enkrip = new Enkripsi(); enkrip.Show();
this.Hide(); }
void DEKRIPSIToolStripMenuItemClick(object sender, Eve ntArgs e)
{
Dekripsi dekrip = new Dekripsi(); dekrip.bilm = bilm;
dekrip.n = n;
for (int i=0; i<n; i++){
dekrip.cipherkunci[i] = cipherkunci[i]; }
for (int i=0; i<7; i++){
dekrip.barisw = barisw; }
dekrip.kolkey = kolkey; dekrip.barkey = barkey;
for (int i=0; i<barkey; i++){
for (int j=0; j<kolkey; j++){
dekrip.kunci[i,j] = kunci[i,j]; } } dekrip.nteks = nteks; dekrip.Show(); this.Hide(); }
void ToolStripButton3Click(object sender, EventArgs e) {
Form bantuan = new Help(); bantuan.Show();
this.Hide(); }
void ToolStripButton4Click(object sender, EventArgs e) {
if (MessageBox.Show("Apakah Anda benar ingin
keluar dari Aplikasi?","konfirmasi",
MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResul t.Yes)
Application.Exit(); }
void MainFormLoad(object sender, EventArgs e) {
} }
2. Enkripsi plainteks menggunakan Algoritma ADFGVX Cipher
string[] arrkunci1= new string[] {"A", "D", "F", "G", "V", "X"}; string[,] arrkunci2
= new string[6,6] {{"F","E","D","C","B","A"}, {"G","H","I","J"," K","L"}, {"R","Q","P","O","N","M"}, {"S","T","U","V","W","X"}, { "3","2","1","0","Z","Y"}, {"4","5","6","7","8","9"}};
string tekss = richTextBox1.Text.ToUpper().ToString();
char[] teks = tekss.ToCharArray();
string[] cipher = new string[10000]; nteks = tekss.Length;
int l = 0;
Stopwatch timer = new Stopwatch(); timer.Start();
for (int i = 0; i < nteks; i++){
if (char.IsLetter(teks[i]) == true){
for (int j =0; j<6; j++){ for (int k =0; k<6; k++){ if (teks[i].ToString() == arrkunci2[j,k]){ cipher[l] = arrkunci1[j]; cipher[l+1] = arrkunci1[k]; l+=2; } } } } else{ cipher[l] = teks[i].ToString(); l+=1; } } double n = Convert.ToDouble(l)/Convert.ToDouble(textBox3.TextLength);
char[] txtkunci = textBox3.Text.ToUpper().ToCharArray();
barkey = (Convert.ToInt16( Math.Ceiling(n)))+2; kolkey = textBox3.TextLength;
for (int i = 0; i < kolkey; i++){ kunci[0,i] = txtkunci[i]; }
for (int i = 0; i <kolkey; i++){
kunci[1,i] = Convert.ToChar((i+1).ToString()); }
int x = 0;
for(int i = 2; i <barkey; i++){
if (i%2 == 0){
for(int j = 0; j<kolkey; j++){
if(cipher[x] == null){
kunci[i,j] = 'x';
else{ kunci[i,j] = Convert.ToChar(cipher[x]); x++; } } } else{
for(int k = kolkey-1; k>=0; k--){
if(cipher[x] == null){ kunci[i,k] = 'x'; } else{ kunci[i,k] = Convert.ToChar(cipher[x]); x++; } } } }
char[] tempkey = new char[kolkey];
char[] tempkey1 = new char[10000];
for (int i=0; i<kolkey; i++){ tempkey[i] = kunci[0,i]; }
Array.Sort(tempkey);
for (int i=0; i<kolkey; i++){
for (int j=i; j<kolkey; j++){
if (tempkey[i] == kunci[0,j]){
for(int k=0; k<barkey; k++){
tempkey1[k] = kunci[k,i]; kunci[k,i] = kunci[k,j]; kunci[k,j] = tempkey1[k]; } break; } } }
for (int i=0; i<kolkey; i++){
for (int j=2; j<barkey; j++){
textBox4.Text+= kunci[j,i].ToString(); }
}
timer.Stop();
textBox2.Text = timer.Elapsed.TotalSeconds.ToString();
3.
Acak Kunci Algoritma Knapsack
public int gcd(int p2, int n2){ if (n2 == 0){
return p2; }
return gcd(n2, p2 % n2); }
public bool iscoprime(int p1, int n1){ if (gcd(p1, n1) == 1){
return true; }
return false; }
void Button4Click(object sender, EventArgs e) {
nW = textBox3.TextLength;
int jumlah;
int n=1;
Random r = new Random(); textBox5.Text = textBox3.Text; barisanw[0] = r.Next(1,3); while (n<7){
jumlah = 0;
for(int j=0; j<=n-1; j++){
jumlah = jumlah+ barisanw[j]; }
barisanw[n] = jumlah + r.Next(1,5); n++;
}
int jum_elemen = 0;
for (int i=0; i<7; i++){
jum_elemen = jum_elemen + barisanw[i]; }
for (int i=0; i<7; i++){
textBox7.Text += barisanw[i].ToString()+ " "; } bilm= r.Next(jum_elemen+1,10000); biln=r.Next(255); while(iscoprime(bilm,biln) == false){ bilm=r.Next(jum_elemen+1,10000); biln=r.Next(255); } textBox8.Text = bilm.ToString(); textBox9.Text = biln.ToString(); for (int i=0; i<7; i++){
publik[i] = (barisanw[i]*biln) % bilm;
textBox11.Text += publik[i].ToString()+" ";
}
char[] txtkunci = textBox5.Text.ToCharArray(); string tempbiner;
char[] biner = new char[nW];
nW = textBox3.TextLength;
Stopwatch timer = new Stopwatch(); timer.Start();
for (int i=0; i<nW; i++){ tempbiner =
Convert.ToString((int)txtkunci[i],2).PadLeft(7,'0');
biner = tempbiner.ToCharArray();
for (int j=0; j<7; j++){
cipherkey[i] +=
Convert.ToInt16(char.GetNumericValue(biner[j])) * publik[j];
} textBox10.Text += cipherkey[i].ToString()+ " "; } timer.Stop(); textBox6.Text = timer.Elapsed.TotalSeconds.ToString();
5.
Dekripsi cipherkey menggunakan Algoritma Knapsack
int invmodulo = Convert.ToInt16(textBox4.Text);
string plainkey = "";
Stopwatch timer = new Stopwatch(); timer.Start();
for (int i=0; i<n; i++){
tempplain = (cipherkunci[i]*invmodulo) % bilm;
for (int j=6; j>=0; j--){ if (tempplain >= barisw[j]){ tempplain -= barisw[j]; binerplain[i,j] += "1"; } else{ binerplain[i,j] += 0; } } }
for (int i=0; i<n; i++){
plainkey="";
for (int j=0; j<7; j++){
plainkey += binerplain[i,j]; }
textBox5.Text += (char)Convert.ToInt16(plainkey,2);
} timer.Stop();
6.
Dekripsi Cipherteks Menggunakan Algoritma ADFGVX Cipher
string[] arrkunci1= new string[] {"A", "D", "F", "G", "V", "X"}; string[,] arrkunci2
= new string[6,6] {{"F","E","D","C","B","A"}, {"G","H","I","J" ,"K","L"},{"R","Q","P","O","N","M"}, {"S","T","U","V","W","X"} , {"3","2","1","0","Z","Y"}, {"4","5","6","7","8","9"}};
char[] tempkey = textBox5.Text.ToUpper().ToCharArray();
char[] tempkey1 = new char[1000];
string tempplainteks="";
Stopwatch timer = new Stopwatch(); timer.Start();
for (int i=0; i<kolkey; i++){
for (int j=i; j<kolkey; j++){
if (tempkey[i]==kunci[0,j] && Char.GetNumericValue(k unci[1,j]) <= i+1){
for(int k=0; k<barkey; k++){
tempkey1[k] = kunci[k,i]; kunci[k,i] = kunci[k,j]; kunci[k,j] = tempkey1[k]; } break; } } }
for (int i=2; i<barkey; i++){ if (i%2 == 0){
for (int j=0; j<kolkey; j++){
tempplainteks+= kunci[i,j].ToString(); }
} else{
for (int j=kolkey-1; j>=0; j--){
tempplainteks+= kunci[i,j].ToString(); }
} }
char[] tempplainteks1 = tempplainteks.ToCharArray(); int x = 0;
while (x<textBox7.TextLength){
if (char.IsLetter(tempplainteks1[x]) == true && temppl
ainteks1[x] != 'x'){
textBox8.Text +=arrkunci2[Array.IndexOf(arrkunci1, (te mpplainteks1[x]).ToString()), Array.IndexOf(arrkunci1, (temppl ainteks1[x+1].ToString()))];
x+=2; }
break; } else{ textBox8.Text += tempplainteks1[x].ToString(); x+=1; } } timer.Stop(); textBox10.Text = timer.Elapsed.TotalMilliseconds.ToString();