Listing Program Playfair Elgamal
Form1 :
Public Class Form1
Public Shared Kunci As Integer(,) = New Integer(16, 16) {} Public Shared strKunci As String
Public Shared Sub getKunci(ByVal z0 As Integer) strKunci = ""
Dim z() As Integer = New Integer(256) {} Dim sum As Integer = 0
z(0) = z0
For i = 1 To 255
z(i) = (21 * z(i - 1) + 173) Mod 256 Next
For i = 0 To 15 For j = 0 To 15
Kunci(i, j) = z(sum) sum += 1
strKunci += " " + Kunci(i, j).ToString() + Chr(9) Next
Next
End Sub
Private Sub EnkripsiToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles EnkripsiToolStripMenuItem.Click Dim frm2 As New Form2()
frm2.ShowDialog() End Sub
Private Sub DekripsiToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DekripsiToolStripMenuItem.Click
If (MessageBox.Show("Anda akan masuk ke Form Dekripsi. Apakah yakin Dekripsi Gambar tanpa Enkripsi terlebih dulu?", "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes) Then
Dim frm3 As New Form3() frm3.ShowDialog() End If
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Application.Exit()
Form2 :
Public Class Form2
Dim cipher1, cipher2 As Integer
Dim g, k As Integer
Public x, p As Integer
Dim y, b As BigInteger
Public a As BigInteger
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim openFileDialog1 As New OpenFileDialog() Dim path As String
openFileDialog1.Filter = "bmp Files|*.bmp|png Files|*.png" openFileDialog1.FileName = "*.bmp"
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
Dim gbr_Asli As New Bitmap(openFileDialog1.FileName) If (gbr_Asli.Width Mod 2 = 1) Then
MessageBox.Show("Lebar Pixel Gambar tidak bisa Ganjil.") Exit Sub
End If
PictureBox1.Image = gbr_Asli LCitraAsli.Text = gbr_Asli.Width PCitraAsli.Text = gbr_Asli.Height
path = System.IO.Path.GetFullPath(openFileDialog1.FileName) Dim fileinfo As New System.IO.FileInfo(path)
Dim size As Double = fileinfo.Length / 1024
SizeCitraAsli.Text = (Math.Round(size, 1)).ToString() NamaCitra.Text =
System.IO.Path.GetFileName(openFileDialog1.FileName) End If
End Sub
Private Sub enkripPlayFair(ByVal bigram As Integer(), ByVal Kunci As Integer(,))
Dim limit As Boolean = False
Dim bigramx As Integer() = New Integer(2) {} Dim bigramy As Integer() = New Integer(2) {} For x = 0 To 1
For i = 0 To 15 For j = 0 To 15
If (bigram(x) = Kunci(i, j)) Then
bigramx(x) = i bigramy(x) = j limit = True
If (limit = True And x = 2) Then
Exit For
End If
End If
Next
If (limit = True And x = 2) Then
Exit For
End If
Next
Next
If (bigramx(0) = bigramx(1)) Then
cipher1 = Kunci(bigramx(0), 0)
gCipher2 = cipher2
b1 = gbr_Asli.GetPixel(j, i).B b2 = gbr_Asli.GetPixel(j + 1, i).B bigram(0) = b1
bigram(1) = b2
enkripPlayFair(bigram, Form1.Kunci) bCipher1 = cipher1
bCipher2 = cipher2
gbr_Playfair.SetPixel(j, i, Color.FromArgb(255, rCipher1, gCipher1, bCipher1))
gbr_Playfair.SetPixel(j + 1, i, Color.FromArgb(255, rCipher2, gCipher2, bCipher2))
Next
Next
PictureBox2.Image = gbr_Playfair
LCitraPlayfair.Text = gbr_Playfair.Width PCitraPlayfair.Text = gbr_Playfair.Height sw.Stop()
Dim detik As Double
detik = Math.Round(sw.ElapsedMilliseconds / 1000, 3)
waktuPlayfair.Text += sw.ElapsedMilliseconds.ToString() + " ms / " + detik.ToString() + " s"
MessageBox.Show("Proses Enkripsi telah selesai", "Enkripsi",
MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub
Private Sub SimpanPlayfair_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SimpanPlayfair.Click Dim simpan As New SaveFileDialog() Dim path As String
simpan.Filter = "bmp Files|*.bmp" simpan.FileName = "*.bmp"
If (simpan.ShowDialog() = DialogResult.OK) Then
PictureBox2.Image.Save(simpan.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
NamaCitraPlayfair.Text =
System.IO.Path.GetFileName(simpan.FileName)
path = System.IO.Path.GetFullPath(simpan.FileName) Dim fileinfo As New System.IO.FileInfo(path) Dim size As Double = fileinfo.Length / 1024
SizeCitraPlayfair.Text = (Math.Round(size, 1)).ToString() MessageBox.Show("Gambar telah berhasil disimpan", "Simpan",
MessageBoxButtons.OK, MessageBoxIcon.Information) End If
End Sub
Private Sub MenuUtamaToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuUtamaToolStripMenuItem.Click Me.Dispose()
End Sub
Private Sub DekripsiToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DekripsiToolStripMenuItem.Click Dim frm3 As New Form3()
frm3.ShowDialog() End Sub
Private Sub SimpanElgamal_Click(ByVal sender As System.Object, ByVal e As
Dim path As String
sw.Start()
For i = 0 To h - 1 For j = 0 To w - 1
m = gbr_Asli.GetPixel(j, i).R
bR = (BigInteger.Pow(y, k) * m) Mod p If bR Mod 257 = 256 Then
bR = 255 End If
m = gbr_Asli.GetPixel(j, i).G
bG = (BigInteger.Pow(y, k) * m) Mod p If bG Mod 257 = 256 Then
bG = 255 End If
m = gbr_Asli.GetPixel(j, i).B
bB = (BigInteger.Pow(y, k) * m) Mod p If bB Mod 257 = 256 Then
bB = 255 End If
gbr_Elgamal.SetPixel(j, i, Color.FromArgb(255, bR, bG, bB)) Next
Next
PictureBox3.Image = gbr_Elgamal
LCitraElgamal.Text = gbr_Elgamal.Width PCitraElgamal.Text = gbr_Elgamal.Height sw.Stop()
Dim detik As Double
detik = Math.Round(sw.ElapsedMilliseconds / 1000, 3)
waktuElgamal.Text += sw.ElapsedMilliseconds.ToString() + " ms / " + detik.ToString() + " s"
MessageBox.Show("Proses Enkripsi telah selesai", "Enkripsi",
MessageBoxButtons.OK, MessageBoxIcon.Information) End If
End Sub
Private Sub btnKunci_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnKunci.Click If txtZ0.Text <> "" Then
Dim z0 As Integer = txtZ0.Text Form1.getKunci(z0)
txtKunci.Text = Form1.strKunci Else
MessageBox.Show("Masukkan Kunci Playfair") End If
Form3 :
Public Class Form3
Dim plain1, plain2, x, p As Integer
Dim a As BigInteger
Private Sub MenuUtamaToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuUtamaToolStripMenuItem.Click Me.Dispose()
End Sub
Private Sub EnkripsiToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles EnkripsiToolStripMenuItem.Click Dim frm2 As New Form2()
frm2.ShowDialog() End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim openFileDialog1 As New OpenFileDialog() Dim path As String
openFileDialog1.Filter = "bmp Files|*.bmp|png Files|*.png" openFileDialog1.FileName = "*.bmp"
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
Dim gbr_Playfair As New Bitmap(openFileDialog1.FileName) If (gbr_Playfair.Width Mod 2 = 1) Then
MessageBox.Show("Lebar Pixel Gambar tidak bisa Ganjil.") Exit Sub
End If
PictureBox1.Image = gbr_Playfair
LCitraPlayfair.Text = gbr_Playfair.Width PCitraPlayfair.Text = gbr_Playfair.Height
path = System.IO.Path.GetFullPath(openFileDialog1.FileName) Dim fileinfo As New System.IO.FileInfo(path)
Dim size As Double = fileinfo.Length / 1024
SizeCitraPlayfair.Text = (Math.Round(size, 1)).ToString() NamaCitraPlayfair.Text =
System.IO.Path.GetFileName(openFileDialog1.FileName) dekripsiPlayfair.Enabled = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim openFileDialog1 As New OpenFileDialog() Dim path As String
openFileDialog1.Filter = "bmp Files|*.bmp|png Files|*.png" openFileDialog1.FileName = "*.bmp"
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
Dim gbr_Elgamal As New Bitmap(openFileDialog1.FileName) PictureBox3.Image = gbr_Elgamal
LCitraElgamal.Text = gbr_Elgamal.Width PCitraElgamal.Text = gbr_Elgamal.Height
path = System.IO.Path.GetFullPath(openFileDialog1.FileName) Dim fileinfo As New System.IO.FileInfo(path)
Dim size As Double = fileinfo.Length / 1024
SizeCitraElgamal.Text = (Math.Round(size, 1)).ToString() NamaCitraElgamal.Text =
End If
As System.EventArgs) Handles dekripsiPlayfair.Click If txtKunci.Text = "" Then
MessageBox.Show("Generate Kunci Playfair terlebih dulu") txtZ0.Focus()
Exit Sub
Dim sw As New Stopwatch
Dim gbr_Asli As Bitmap = New Bitmap(PictureBox1.Image) Dim gbr_Playfair As Bitmap = New Bitmap(PictureBox1.Image) Dim x As Integer = gbr_Playfair.Width
Dim y As Integer = gbr_Playfair.Height Dim bigram As Integer() = New Integer(2) {} Dim r1, g1, b1, r2, g2, b2 As Integer
Dim rCipher1, gCipher1, bCipher1, rCipher2, gCipher2, bCipher2 As Integer
waktuPlayfair.Text = "Lama Proses Dekripsi : " sw.Start()
For i = 0 To y - 1
For j = 0 To x - 1 Step 2
r1 = gbr_Playfair.GetPixel(j, i).R r2 = gbr_Playfair.GetPixel(j + 1, i).R bigram(0) = r1
bigram(1) = r2
dekripPlayfair(bigram, Form1.Kunci) rCipher1 = plain1
rCipher2 = plain2
g1 = gbr_Asli.GetPixel(j, i).G g2 = gbr_Asli.GetPixel(j + 1, i).G bigram(0) = g1
bigram(1) = g2
dekripPlayfair(bigram, Form1.Kunci) gCipher1 = plain1
gCipher2 = plain2
b1 = gbr_Asli.GetPixel(j, i).B b2 = gbr_Asli.GetPixel(j + 1, i).B bigram(0) = b1
bigram(1) = b2
dekripPlayfair(bigram, Form1.Kunci) bCipher1 = plain1
bCipher2 = plain2
gbr_Asli.SetPixel(j, i, Color.FromArgb(255, rCipher1, gCipher1, bCipher1))
gbr_Asli.SetPixel(j + 1, i, Color.FromArgb(255, rCipher2, gCipher2, bCipher2))
Next
Next
PictureBox2.Image = gbr_Asli
Dim mse As Double = hitungMSE(PictureBox1.Image, PictureBox2.Image)
LDekrip_Playfair.Text = gbr_Asli.Width PDekrip_Playfair.Text = gbr_Asli.Height MSEplayfair.Text = Math.Round(mse, 4) sw.Stop()
Dim detik As Double
detik = Math.Round(sw.ElapsedMilliseconds / 1000, 3)
waktuPlayfair.Text += sw.ElapsedMilliseconds.ToString() + " ms / " + detik.ToString() + " s"
MessageBox.Show("Proses Dekripsi telah selesai", "Dekripsi",
MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub
Private Sub SimpanPlayfair_Click(ByVal sender As System.Object, ByVal e As
m = gbr_Elgamal.GetPixel(j, i).G bG = m * ax Mod p
If bG Mod 257 = 256 Then
bG = 255 End If
m = gbr_Elgamal.GetPixel(j, i).B bB = m * ax Mod p
If bB Mod 257 = 256 Then
bB = 255 End If
gbr_Asli.SetPixel(j, i, Color.FromArgb(255, bR, bG, bB)) Next
Next
PictureBox4.Image = gbr_Asli
Dim mse As Double = hitungMSE(PictureBox3.Image, PictureBox4.Image) MSEelgamal.Text = Math.Round(mse, 4)
LDekrip_Elgamal.Text = gbr_Asli.Width PDekrip_Elgamal.Text = gbr_Asli.Height sw.Stop()
Dim detik As Double
detik = Math.Round(sw.ElapsedMilliseconds / 1000, 3)
waktuElgamal.Text += sw.ElapsedMilliseconds.ToString() + " ms / " + detik.ToString() + " s"
MessageBox.Show("Proses Dekripsi telah selesai", "Dekripsi",
MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub
Function hitungMSE(ByVal imageAsli As Bitmap, ByVal imageHasil As Bitmap)
As Double
Dim sum As UInt64 = 0
Dim r1, r2, g1, g2, b1, b2 As Double
Dim MSE As Double
For i = 0 To imageAsli.Height - 1 For j = 0 To imageAsli.Width - 1 r1 = imageAsli.GetPixel(j, i).R r2 = imageHasil.GetPixel(j, i).R sum += Math.Pow(r1 - r2, 2) g1 = imageAsli.GetPixel(j, i).G g2 = imageHasil.GetPixel(j, i).G sum += Math.Pow(g1 - g2, 2) b1 = imageAsli.GetPixel(j, i).B b2 = imageHasil.GetPixel(j, i).B sum += Math.Pow(b1 - b2, 2) Next
Next
MSE = sum / (imageAsli.Height * imageAsli.Width) Return MSE
End Function
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load Try
keyp.Text =
Application.OpenForms("Form2").Controls("GroupBox2").Controls("GroupBox4").Cont rols("keyp").Text
keya.Text =
keyx.Text =
Application.OpenForms("Form2").Controls("GroupBox2").Controls("GroupBox4").Cont rols("keyx").Text
Catch ex As Exception
End Try
End Sub
Private Sub btnKunci_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnKunci.Click If txtZ0.Text <> "" Then
Dim z0 As Integer = txtZ0.Text Form1.getKunci(z0)
txtKunci.Text = Form1.strKunci Else
MessageBox.Show("Masukkan Kunci Playfair") End If