Listing Program
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.IO;
using System.Drawing.Imaging;
namespace Playfair {
public partial class Form2 : Form {
Bitmap newBitmap; Image file;
string cipher = ""; public string aa; int ab = 0;
char[] alpabet = new char[26];
public Form2() {
InitializeComponent();
}
private void
enkripsiDanEmbedPesanToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 fr2 = new Form2(); fr2.ShowDialog();
}
private void
eKstraksiDanDekripsiPesanToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form3 fr3 = new Form3(); fr3.ShowDialog();
}
private void keluarToolStripMenuItem_Click(object sender, EventArgs e)
{
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form4 fr4 = new Form4(); fr4.ShowDialog();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5(); fr5.ShowDialog();
}
private void button6_Click(object sender, EventArgs e) {
this.Hide();
Form1 fr1 = new Form1(); fr1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e) {
Stream mystream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((mystream = openFileDialog1.OpenFile()) != null)
{
string strfilename = openFileDialog1.FileName; string filetext =
File.ReadAllText(strfilename);
textBox1.Text = filetext; }
}
}
int count = 0;
private void textBox1_TextChanged(object sender, EventArgs e)
{
String inputString = textBox1.Text; int count = 0;
char[] ArrayChar = new char[inputString.Length]; ArrayChar = inputString.ToCharArray();
foreach (char temp in ArrayChar) {
if (char.IsLetter(temp) || !(char.IsLetterOrDigit(temp)))
{
} }
textBox5.Text = count.ToString();
}
private void button3_Click(object sender, EventArgs e) {
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Bitmaps|*.Bmp";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK) {
file = Image.FromFile(openFileDialog1.FileName); newBitmap = new Bitmap(openFileDialog1.FileName); pictureBox1.Image = file;
label9.Text = file.Width.ToString(); label10.Text = file.Height.ToString(); string path =
Path.GetFullPath(openFileDialog1.FileName); //MessageBox.Show(path);
FileInfo fileInfo = new FileInfo(path); double fileSizeKB = fileInfo.Length / 1024; double fileSizeMB = fileInfo.Length / (1024 * 1024);
label29.Text = fileSizeKB.ToString(); }
if (file.Width < count) {
MessageBox.Show(" Pesan Teks Anda Melebihi Kapasitas Panjang Gambar");
this.textBox1.Text = ""; }
}
private void button5_Click(object sender, EventArgs e) {
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Image Files(*.bmp)|*.bmp";
if (DialogResult.OK == saveFileDialog1.ShowDialog()) {
pictureBox2.Image.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
string path =
Path.GetFullPath(saveFileDialog1.FileName); //MessageBox.Show(path);
FileInfo fileInfo = new FileInfo(path); double fileSizeKB = fileInfo.Length / 1024;
//double fileSizeMB = fileInfo.Length / (1024 * 1024); label26.Text = fileSizeKB.ToString();
}
{
if (huruf == 'j') huruf = 'i';
for (int i = 0; i < ab; i++) {
if (alpabet[i] == huruf) return false;
}
alpabet[ab] = huruf; ab++;
return true; }
private void putarKunci1(char[,] matrix, char huruf) {
bool limit = false; char temp = ' ';
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i, j] == huruf) {
if (j == 4 && i == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[0, j]; matrix[0, j] = matrix[0, 0]; matrix[0, 0] = matrix[i, 0]; matrix[i, 0] = temp;
limit = true; break;
}
else if (i == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[0, j]; matrix[0, j] = matrix[0, j + 1]; matrix[0, j + 1] = matrix[i, j + 1]; matrix[i, j + 1] = temp;
limit = true; break;
}
else if (j == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i + 1, j]; matrix[i + 1, j] = matrix[i + 1, 0]; matrix[i + 1, 0] = matrix[i, 0]; matrix[i, 0] = temp;
limit = true; break;
{
temp = matrix[i, j];
matrix[i, j] = matrix[i + 1, j]; matrix[i + 1, j] = matrix[i + 1, j + 1];
matrix[i + 1, j + 1] = matrix[i, j + 1];
matrix[i, j + 1] = temp; limit = true;
break; }
} }
if (limit) break; }
string key = "";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
key += matrix[i, j]; }
key += "\r\n"; }
//textBox3.Text += "\r\n\n" + key; }
private void putarKunci2(char[,] matrix, char huruf) {
bool limit = false; char temp = ' ';
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i, j] == huruf) {
if (i == 4 && j == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i, 0]; matrix[i, 0] = matrix[0, 0]; matrix[0, 0] = matrix[0, j]; matrix[0, j] = temp;
limit = true; break;
}
else if (i == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i, j + 1]; matrix[i, j + 1] = matrix[0, j + 1]; matrix[0, j + 1] = matrix[0, j]; matrix[0, j] = temp;
break; }
else if (j == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i, 0]; matrix[i, 0] = matrix[i + 1, 0]; matrix[i + 1, 0] = matrix[i + 1, j]; matrix[i + 1, j] = temp;
limit = true; break;
} else {
temp = matrix[i, j];
matrix[i, j] = matrix[i, j + 1]; matrix[i, j + 1] = matrix[i + 1, j + 1];
matrix[i + 1, j + 1] = matrix[i + 1, j];
matrix[i + 1, j] = temp; limit = true;
break; }
} }
if (limit) break; }
string key = "";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
key += matrix[i, j]; }
key += "\r\n"; }
//textBox3.Text += "\r\n\n" + key; }
private void button2_Click(object sender, EventArgs e) {
string kunci = textBox2.Text; int PKunci = kunci.Length; int a = 0, asci = 97;
char[,] Matrik = new char[5,5]; for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (a < PKunci) {
if (cekKunci(kunci[a]) == true) Matrik[i, j] = kunci[a]; else
a++; }
else {
if (cekKunci((char)asci) == true) Matrik[i, j] = (char)asci; else
j--; asci++; }
} }
string key = "";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
key += Matrik[i, j]; }
key += "\r\n"; }
//textBox3.Text = key;
string plain = textBox1.Text; plain = plain.Replace(" ", ""); plain = plain.Replace("j", "i"); //MessageBox.Show(plain);
string bigram = "";
if (plain.Length % 2 != 0) plain += "x";
int x = 0;
bigram = plain.Substring(x, 2); x += 2;
enkripsi(bigram, Matrik);
for (int i = 0; i < plain.Length - 2; i++) {
if (i % 2 == 0)
putarKunci1(Matrik, plain[i]); else
{
putarKunci2(Matrik, plain[i]); bigram = plain.Substring(x, 2); x += 2;
enkripsi(bigram, Matrik); }
} }
private void enkripsi(string bigram, char[,] Matrik) {
bool limit = false;
int[] bigramy = new int[2]; for (int x = 0; x < 2; x++) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (bigram[x] == Matrik[i, j]) {
bigramx[x] = i; bigramy[x] = j; limit = true;
if (limit == true && x == 2) break; }
}
if (limit == true && x == 2) break; }
}
//MessageBox.Show(bigramx[0].ToString() + bigramy[0].ToString() + " " + bigramx[1].ToString() + bigramy[1].ToString());
//cipher = "";
if (bigramx[0] == bigramx[1]) {
if (bigramy[0] == 4)
cipher += Matrik[bigramx[0], 0].ToString() + Matrik[bigramx[1], bigramy[1] + 1].ToString();
else if (bigramy[1] == 4)
cipher += Matrik[bigramx[0], bigramy[0] + 1].ToString() + Matrik[bigramx[1], 0].ToString();
else
cipher += Matrik[bigramx[0], bigramy[0] +
1].ToString() + Matrik[bigramx[1], bigramy[1] + 1].ToString(); }
else if (bigramy[0] == bigramy[1]) {
if (bigramx[0] == 4)
cipher += Matrik[0, bigramy[0]].ToString() + Matrik[bigramx[1] + 1, bigramy[1]].ToString();
else if (bigramx[1] == 4)
cipher += Matrik[bigramx[0] + 1,
bigramy[0]].ToString() + Matrik[0, bigramy[1]].ToString(); else
cipher += Matrik[bigramx[0] + 1, bigramy[0]].ToString() + Matrik[bigramx[1] + 1, bigramy[1]].ToString();
} else {
cipher += Matrik[bigramx[0], bigramy[1]].ToString() + Matrik[bigramx[1], bigramy[0]].ToString();
}
string ascii = "", s = textBox3.Text; foreach (char c in s)
{
ascii += System.Convert.ToInt32(c) + " "; }
textBox4.Text = ascii;
}
private void button4_Click(object sender, EventArgs e) {
Bitmap image = new Bitmap(pictureBox1.Image);
Bitmap image_hasil = new Bitmap(pictureBox1.Image); int x, y, r, g, b;
string text = ""; x = image.Width; y = image.Height;
string[] temp = textBox4.Text.Split(' ');
// MessageBox.Show("panjang = " + (temp.Length - 1).ToString());
string[] ciphertext = new string[(temp.Length - 1) + (temp.Length - 1)];
int idx = 0;
for (int k = 0; k < temp.Length - 1; k++) {
ciphertext[idx] += temp[k]; idx++;
if (idx < ((temp.Length - 1) * 2) - 1) {
ciphertext[idx] += "32"; idx++;
} else
ciphertext[idx] += "0"; }
for (int l = 0; l < ciphertext.Length; l++) {
// MessageBox.Show(ciphertext[l]); }
for (int m = 0; m < ciphertext.Length - 1; m++) {
text += ubahBiner(Convert.ToInt32(ciphertext[m])); }
//MessageBox.Show(text);
string[] cipherbin = new string[text.Length]; for (int n = 0; n < cipherbin.Length; n++) {
cipherbin[n] = text[n].ToString(); }
idx = 0;
// string binR = ""/*, binG = "", binB = ""*/; //MessageBox.Show(ciphertext.Length.ToString()); for (int i = 0; i < y; i++)
{
{
if (idx == ciphertext.Length) goto selesai;
r = Convert.ToInt32(ciphertext[idx]); idx++;
if (idx == ciphertext.Length) {
g = image.GetPixel(j, i).G; b = image.GetPixel(j, i).B; image_hasil.SetPixel(j, i, Color.FromArgb(255, r, g, b));
goto selesai; }
g = Convert.ToInt32(ciphertext[idx]); idx++;
if (idx == ciphertext.Length) {
b = image.GetPixel(j, i).B; image_hasil.SetPixel(j, i, Color.FromArgb(255, r, g, b));
goto selesai; }
b = Convert.ToInt32(ciphertext[idx]); idx++;
image_hasil.SetPixel(j, i, Color.FromArgb(255, r, g, b));
pictureBox2.Image = file;
label19.Text = file.Width.ToString();
label20.Text = file.Height.ToString(); }
} selesai:
pictureBox2.Image = image_hasil;
string str1 = "", str2 = ""; for (int i = 0; i < 1; i++) {
for (int j = 0; j < x; j++) {
str1 += image.GetPixel(j, i).R.ToString() + " " + image.GetPixel(j, i).G.ToString() + " " + image.GetPixel(j, i).B.ToString() + "\r\n";
str2 += image_hasil.GetPixel(j,
i).R.ToString() + " " + image_hasil.GetPixel(j, i).G.ToString() + " " + image_hasil.GetPixel(j, i).B.ToString() + "\r\n";
}
} }
{
string s = "";
for (int k = 0; k < 8; k++) {
s += (bin & 0x80) > 0 ? "1" : "0"; bin <<= 1;
}
return s; }
private void button7_Click(object sender, EventArgs e) {
this.Hide();
Form2 fr2 = new Form2(); fr2.ShowDialog();
} }
}
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.IO;
using System.Drawing.Imaging;
namespace Playfair {
public partial class Form3 : Form {
Bitmap newBitmap; Image file;
// Boolean opened = false; string plain = "";
int ab = 0;
char[] alpabet = new char[26];
public Form3() {
InitializeComponent();
}
private void
enkripsiDanEmbedPesanToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 fr2 = new Form2(); fr2.ShowDialog();
private void
eKstraksiDanDekripsiPesanToolStripMenuItem_Click_1(object sender, EventArgs e)
{
this.Hide();
Form3 fr3 = new Form3(); fr3.ShowDialog();
}
private void keluarToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit(); }
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form4 fr4 = new Form4(); fr4.ShowDialog();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5(); fr5.ShowDialog();
}
private void button5_Click(object sender, EventArgs e) {
this.Hide();
Form1 fr1 = new Form1(); fr1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e) {
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
using (Stream s =
File.Open(saveFileDialog1.FileName, FileMode.CreateNew)) using (StreamWriter sw = new StreamWriter(s)) {
sw.Write(textBox1.Text); }
} }
private void button3_Click(object sender, EventArgs e) {
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK) {
file = Image.FromFile(openFileDialog1.FileName); newBitmap = new Bitmap(openFileDialog1.FileName); pictureBox1.Image = file;
// opened = true;
label9.Text = file.Width.ToString(); label10.Text = file.Height.ToString(); string path =
Path.GetFullPath(openFileDialog1.FileName);
FileInfo fileInfo = new FileInfo(path); double fileSizeKB = fileInfo.Length / 1024; label11.Text = fileSizeKB.ToString();
} }
bool cekKunci(char huruf) {
if (huruf == 'j') huruf = 'i';
for (int i = 0; i < ab; i++) {
if (alpabet[i] == huruf) return false;
}
alpabet[ab] = huruf; ab++;
return true; }
private void putarKunci1(char[,] matrix, char huruf) {
bool limit = false; char temp = ' ';
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i, j] == huruf) {
if (j == 4 && i == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[0, j]; matrix[0, j] = matrix[0, 0]; matrix[0, 0] = matrix[i, 0]; matrix[i, 0] = temp;
limit = true; break;
else if (i == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[0, j]; matrix[0, j] = matrix[0, j + 1]; matrix[0, j + 1] = matrix[i, j + 1]; matrix[i, j + 1] = temp;
limit = true; break;
}
else if (j == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i + 1, j]; matrix[i + 1, j] = matrix[i + 1, 0]; matrix[i + 1, 0] = matrix[i, 0]; matrix[i, 0] = temp;
limit = true; break;
} else {
temp = matrix[i, j];
matrix[i, j] = matrix[i + 1, j]; matrix[i + 1, j] = matrix[i + 1, j + 1];
matrix[i + 1, j + 1] = matrix[i, j + 1];
matrix[i, j + 1] = temp; limit = true;
break; }
} }
if (limit) break; }
string key = "";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
key += matrix[i, j]; }
key += "\r\n"; }
//textBox5.Text += "\r\n\n" + key; }
private void putarKunci2(char[,] matrix, char huruf) {
bool limit = false; char temp = ' ';
for (int i = 0; i < 5; i++) {
{
if (matrix[i, j] == huruf) {
if (i == 4 && j == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i, 0]; matrix[i, 0] = matrix[0, 0]; matrix[0, 0] = matrix[0, j]; matrix[0, j] = temp;
limit = true; break;
}
else if (i == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i, j + 1]; matrix[i, j + 1] = matrix[0, j + 1]; matrix[0, j + 1] = matrix[0, j]; matrix[0, j] = temp;
limit = true; break;
}
else if (j == 4) {
temp = matrix[i, j];
matrix[i, j] = matrix[i, 0]; matrix[i, 0] = matrix[i + 1, 0]; matrix[i + 1, 0] = matrix[i + 1, j]; matrix[i + 1, j] = temp;
limit = true; break;
} else {
temp = matrix[i, j];
matrix[i, j] = matrix[i, j + 1]; matrix[i, j + 1] = matrix[i + 1, j + 1];
matrix[i + 1, j + 1] = matrix[i + 1, j];
matrix[i + 1, j] = temp; limit = true;
break; }
} }
if (limit) break; }
string key = "";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
key += "\r\n"; }
//textBox5.Text += "\r\n\n" + key; }
private void button2_Click(object sender, EventArgs e) {
string kunci = textBox2.Text; int PKunci = kunci.Length; int a = 0, asci = 97;
char[,] Matrik = new char[5, 5]; for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (a < PKunci) {
if (cekKunci(kunci[a]) == true) Matrik[i, j] = kunci[a]; else
j--; a++; }
else {
if (cekKunci((char)asci) == true) Matrik[i, j] = (char)asci; else
j--; asci++; }
} }
string key = "";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
key += Matrik[i, j]; }
key += "\r\n"; }
//textBox5.Text += key;
string cipher = textBox3.Text; string bigram = "";
int x = 0;
bigram = cipher.Substring(x, 2); x += 2;
dekripsi(bigram, Matrik);
for (int i = 0; i < cipher.Length - 2; i++) {
if (i % 2 == 0)
{
putarKunci2(Matrik, plain[i]); bigram = cipher.Substring(x, 2); x += 2;
dekripsi(bigram, Matrik); }
} }
private void dekripsi(string bigram, char[,] Matrik) {
bool limit = false;
//MessageBox.Show(bigram[0].ToString() + bigram[1]); int[] bigramx = new int[2];
int[] bigramy = new int[2]; for (int x = 0; x < 2; x++) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (bigram[x] == Matrik[i, j]) {
bigramx[x] = i; bigramy[x] = j; limit = true;
if (limit == true && x == 2) break; }
}
if (limit == true && x == 2) break; }
}
//MessageBox.Show(bigramx[0].ToString() + bigramy[0].ToString() + " " + bigramx[1].ToString() + bigramy[1].ToString());
if (bigramx[0] == bigramx[1]) {
if (bigramy[0] == 0)
plain += Matrik[bigramx[0], 4].ToString() + Matrik[bigramx[1], bigramy[1] - 1].ToString();
else if (bigramy[1] == 0)
plain += Matrik[bigramx[0], bigramy[0] - 1].ToString() + Matrik[bigramx[1], 4].ToString();
else
plain += Matrik[bigramx[0], bigramy[0] - 1].ToString() + Matrik[bigramx[1], bigramy[1] - 1].ToString(); }
else if (bigramy[0] == bigramy[1]) {
if (bigramx[0] == 0)
plain += Matrik[4, bigramy[0]].ToString() + Matrik[bigramx[1] - 1, bigramy[1]].ToString();
plain += Matrik[bigramx[0] - 1,
bigramy[0]].ToString() + Matrik[4, bigramy[1]].ToString(); else
plain += Matrik[bigramx[0] - 1, bigramy[0]].ToString() + Matrik[bigramx[1] - 1, bigramy[1]].ToString();
} else {
plain += Matrik[bigramx[0], bigramy[1]].ToString() + Matrik[bigramx[1], bigramy[0]].ToString();
}
//MessageBox.Show(cipher); int index = plain.Length; if (plain[index - 1] == 'x')
plain = plain.Remove(index - 1, 1); textBox1.Text = plain;
/* string ascii = "", s = textBox3.Text; foreach (char c in s)
{
ascii += System.Convert.ToInt32(c) + " "; }
textBox4.Text = ascii;*/
}
private void button4_Click(object sender, EventArgs e) {
int r, g, b;
string plaintext = "";
Bitmap image = new Bitmap(pictureBox1.Image); int x = image.Width, y = image.Height;
for (int i = 0; i < 1; i++) {
for (int j = 0; j < x; j++) {
r = image.GetPixel(j, i).R; g = image.GetPixel(j, i).G; b = image.GetPixel(j, i).B;
plaintext += r.ToString() + " " + g.ToString() + " " + b.ToString() + " ";
} }
int index = plaintext.IndexOf(" 0 "); plaintext = plaintext.Substring(0, index); textBox4.Text = plaintext;
string cipher = ASCIItoNormal(plaintext); cipher = cipher.Replace(" ", "");
textBox3.Text = cipher; }
private string ASCIItoNormal(string ascii) {
string normal = "";
foreach (string w in words) {
if (w == "") continue; normal +=
char.ConvertFromUtf32(Convert.ToInt32(w)); }
return normal; }
private void button6_Click(object sender, EventArgs e) {
this.Hide();
Form3 fr3 = new Form3(); fr3.ShowDialog();
} }