Listing Program
Pembangkit Kunci.VB
Public NotInheritable Class LUC
Private Sub New() End Sub
Private Shared rng As New RNGCryptoServiceProvider()
Public Shared e As BigInteger, N As BigInteger, tN As BigInteger, sN As
BigInteger
Public Shared d As BigInteger
Public Shared p As BigInteger, q As BigInteger
'public static BigInteger [] enk_char;
Private Shared rnd As Byte() = New Byte(3) {} Public Shared Sub hasil_kunci()
Do While p = q
p = generate_bil_prima() q = generate_bil_prima() Loop
N = CType(p * q, BigInteger) tN = generate_tN(p, q)
sN = lcm(New BigInteger() {CType(p, BigInteger) - 1, CType(q,
BigInteger) - 1, CType(p, BigInteger) + 1, CType(q, BigInteger) + 1}) e = must_gdc_with(tN, sN)
d = InverseModular(e, sN)
System.Diagnostics.Debug.WriteLine(GCD(e, sN)) System.Diagnostics.Debug.WriteLine((d * e) Mod sN) End Sub
Private Shared Function must_gdc_with(p As BigInteger, n As BigInteger) As
UInt16
Dim c_e As UInt16
Do
rng.GetBytes(rnd)
c_e = BitConverter.ToUInt16(rnd, 2)
Loop While GCD(c_e, p) <> 1 OrElse c_e >= p OrElse c_e >= n Return c_e
End Function
Private Shared Function generate_bil_prima() As UInt16
Dim c_prima As UInt16
Do
rng.GetBytes(rnd)
c_prima = CType(BitConverter.ToUInt16(rnd, 0) Mod 150, UInt16) Loop While c_prima < 85 OrElse Not Lehmann(c_prima)
Return c_prima End Function
Private Shared Function GCD(a As BigInteger, b As BigInteger) As BigInteger
If a Mod b = 0 Then Return b
End If
Private Shared Function generate_tN(p As BigInteger, q As BigInteger) As
BigInteger
Return (p + 1) * (p - 1) * (q + 1) * (q - 1) End Function
Private Shared Function Lehmann(p As BigInteger) As Boolean Dim tester As BigInteger, i As BigInteger = 0
'System.Diagnostics.Debug.WriteLine(p, "p"); Do
rng.GetBytes(rnd)
tester = (BitConverter.ToUInt16(rnd, 2) Mod p) tester = BigInteger.ModPow(tester, (p - 1) / 2, p) 'System.Diagnostics.Debug.WriteLine(tester, "tester"); If tester <> 1 AndAlso tester - p <> -1 Then
Return False End If
'System.Diagnostics.Debug.WriteLine(i, "i"); i += 1
Loop While i < 5 Return True End Function
Private Shared Function lcm(ParamArray empat As BigInteger()) As BigInteger
Dim hasil As BigInteger = 1
For Each bil As BigInteger In empat hasil = lcm(hasil, bil)
Next
Return hasil End Function
Private Shared Function lcm(a As BigInteger, b As BigInteger) As BigInteger
Return CType((a * b) / GCD(a, b), BigInteger) End Function
Public Shared Function InverseModular(a As BigInteger, b As BigInteger) As
BigInteger
Dim x1 As BigInteger, y1 As BigInteger, x2 As BigInteger, y2 As
BigInteger, q As BigInteger, temp As BigInteger, _ modulo As BigInteger = b
If a < b Then
x1 = InlineAssignHelper(y2, 1) x2 = InlineAssignHelper(y1, 0) Else
x1 = InlineAssignHelper(y2, 0) x2 = InlineAssignHelper(y1, 1) End If
While b <> 0 q = a / b temp = b
b = a - (q * b) a = temp
temp = x2
x2 = x1 - (q * x2) x1 = temp
temp = y2
y2 = y1 - (q * y2) y1 = temp
End While
FormEnk.VB
Public Class EnkripsiForm
'Private Shared luc As lucClass = New lucClass Private Shared strText As String
Private Shared enkripsiResult As String Private Shared sb As New StringBuilder()
Private Sub BrowseFileEnkripsiButton_Click(sender As Object, e As
EventArgs) Handles BrowseFileEnkripsiButton.Click HasilEnkripsiTextBox.Text = ""
SaveFileEnkripsi.Enabled = False Dim filePath As String
If (OpenFileEnkripsiDialog.ShowDialog = DialogResult.OK) Then filePath = OpenFileEnkripsiDialog.FileName.ToString
Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8,
Encoding.Default.GetBytes(s)))
Private Sub EnkripsiButton_Click(sender As Object, e As EventArgs) Handles EnkripsiButton.Click
SaveFileEnkripsi.Enabled = True LUC.hasil_kunci()
Dim time As New System.Diagnostics.Stopwatch() time.Start()
Tampung.luc_c_kuncia = LUC.enkripsi(FileContentEnkripsiTextBox.Text) time.Stop()
waktu.Text = time.ElapsedMilliseconds.ToString() & " ms"
pTextBox.Text = LUC.p.ToString()
End Sub
Private Shared Sub hasilenk(val As BigInteger) EnkripsiForm.sb.Append(val)
EnkripsiForm.sb.Append("-") End Sub
Private Sub SaveFileEnkripsi_Click(sender As Object, e As EventArgs) Handles SaveFileEnkripsi.Click
If SaveFileEnkripsiDialog.ShowDialog = DialogResult.OK Then Dim path As String = SaveFileEnkripsiDialog.FileName File.WriteAllText(path, sb.ToString())
'Dim fs As FileStream = File.Create(path) 'Dim info As Byte() = New
UTF8Encoding(True).GetBytes(LUC.d.ToString & " " & LUC.N.ToString & " " & Me.enkripsiResult)
'fs.Write(info, 0, info.Length) 'fs.Close()
MessageBox.Show("File tersimpan!") End If
End Sub
Private Sub FileContentEnkripsiTextBox_TextChanged(sender As Object, e As
EventArgs) Handles FileContentEnkripsiTextBox.TextChanged End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) End Sub
Private Sub pTextBox_TextChanged(sender As Object, e As EventArgs) Handles pTextBox.TextChanged
End Sub
Private Sub waktu_TextChanged(sender As Object, e As EventArgs) Handles waktu.TextChanged
End Sub
EnkripDekrip.VB
Public Shared Function enkripsi(x As String) As BigInteger() Dim chars As Integer() = pecah_string(x)
Dim new_char As BigInteger() = New BigInteger(chars.Length - 1) {} Dim i As Integer = 0
For Each c_p As Integer In chars
new_char(i) = barisan_LUC(c_p, 1, e, N) i += 1
Next
Return new_char End Function
Public Shared Function dekripsi(enk As BigInteger()) As String Dim chars As Integer() = New Integer(enk.Length - 1) {} Dim i As Integer = 0
For Each c_e As BigInteger In enk
Next
Return gabung_string(chars) End Function
Private Shared Function pecah_string(x As String) As Integer() Dim chars As New List(Of Integer)() (Asc(spasi) - 31).ToString().PadLeft(2, "0"c)
Dim char_a As Integer = Integer.Parse(char_gab) chars.Add(char_a)
End If
Return chars.ToArray() End Function
Private Shared Function gabung_string(c_d As Integer()) As String 'int i = 0;
Private Shared Function barisan_LUC(P As BigInteger, Q As BigInteger, e As
BigInteger, N As BigInteger) As BigInteger
Dim Vold As BigInteger = 2
Return InlineAssignHelper(Vnew, If(Vnew > 0, Vnew, N + Vnew)) End Function
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value Return value End Function End Class
FormDekrip.VB
Public Class DekripsiForm
'Private Shared luc As lucClass = New lucClass Private Shared chiperText As String
Private Shared hasil As String
Private Sub BrowseFileDekripsiButton_Click(sender As Object, e As
EventArgs) Handles BrowseFileDekripsiButton.Click
If OpenFileDekripsiDialog.ShowDialog = DialogResult.OK Then Try
' Open the file using a stream reader.
Using sr As New StreamReader(OpenFileDekripsiDialog.FileName) ' Read the stream to a string and write the string to the console.
chiperText = sr.ReadToEnd() ChiperTextBox.Text = chiperText TextBox3.Text = LUC.N.ToString() TextBox7.Text = LUC.d.ToString() End Using
Catch ex As Exception
MessageBox.Show("The file could not be read:") Console.WriteLine(ex.Message)
End Try End If End Sub
Private Sub DekripsiButton_Click(sender As Object, e As EventArgs) Handles DekripsiButton.Click
Dim time As New System.Diagnostics.Stopwatch() time.Start()
hasil = LUC.dekripsi(Tampung.luc_c_kuncia) time.Stop()
waktu.Text = time.ElapsedMilliseconds.ToString() & " ms"
TextBox10.Text = hasil End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'If SaveFileEnkripsiDialog.ShowDialog = DialogResult.OK Then 'Dim path As String = SaveFileEnkripsiDialog.FileName
If SaveFileDialog1.ShowDialog = DialogResult.OK Then Dim pdf As New PdfSharp.Pdf.PdfDocument()
Dim pdfPage As PdfSharp.Pdf.PdfPage
Dim graph As PdfSharp.Drawing.XGraphics
Dim XFont As New PdfSharp.Drawing.XFont("Times New Roman", 12, PdfSharp.Drawing.XFontStyle.Regular)
graph = PdfSharp.Drawing.XGraphics.FromPdfPage(pdfPage)
graph.DrawString(hasil, XFont, PdfSharp.Drawing.XBrushes.Black, New PdfSharp.Drawing.XRect(0, 0,
pdfPage.Width.Point, pdfPage.Height.Point),
PdfSharp.Drawing.XStringFormat.TopLeft) pdf.Save(SaveFileDialog1.FileName)
pdf.Close()
End If
'Dim fs As FileStream = File.Create(path)
'Dim info As Byte() = New UTF8Encoding(True).GetBytes(LUC.d.ToString & " " & LUC.N.ToString & " " & Me.enkripsiResult)
'fs.Write(info, 0, info.Length) 'fs.Close()
MessageBox.Show("File tersimpan!") 'End If
End Sub End Class
FormKomp.VB
Public Class KompresiForm
Private Sub KompresiButton_Click(sender As Object, e As EventArgs) Handles KompresiButton.Click
Dim sb As New Text.StringBuilder
Tampung.hasil_kompresi =
GoldBach.kompresi(System.Text.Encoding.Unicode.GetBytes(Tampung.hasil_enkripsi) )
For Each x As Byte In Tampung.hasil_kompresi sb.Append(x)
sb.Append("-") Next
If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then System.IO.File.WriteAllText(SaveFile.FileName, sb.ToString()) End If
FileContentKompresiTextBox.Text = sb.ToString() End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles FileContentKompresiTextBox.TextChanged
End Sub
Private Function HasilKompresiTextBox() As Object Throw New NotImplementedException
End Function
Private Function SaveFileKompresi() As Object Throw New NotImplementedException
End Function
End Sub
Private Sub HasilKompresi_Click(sender As Object, e As EventArgs) Handles HasilKompresi.Click
If OpenFileKompresiDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
FilePathKompresiTextBox.Text = OpenFileKompresiDialog.FileName End If
End Sub
Private Sub ResetButton_Click(sender As Object, e As EventArgs) End Sub
End Class
KompDekomp.VB
Public NotInheritable Class GoldBach
Private Sub New() End Sub
Private Shared l_char As New Dictionary(Of Byte, Integer)() Private Shared Bil_Prima As New List(Of Integer)()
Private Shared eg_char As List(Of KeyValuePair(Of Byte, Integer)) Private Shared eg_encoding As New Dictionary(Of Byte, String)() Private Shared values As Byte()
Private Shared flag As Integer Private Shared Sub susun_char() l_char.Clear()
For Each chars As Byte In values
If Not l_char.ContainsKey(chars) Then l_char.Add(chars, 1)
Else
l_char(chars) += 1 End If
Next
eg_char = l_char.ToList()
eg_char.Sort(Function(pair1, pair2) pair2.Value.CompareTo(pair1.Value)) End Sub
Private Shared Sub encoding() eg_encoding.Clear() Bil_Prima.Clear() Bil_Prima.Add(3) Dim i As Integer = 1
For Each pair As KeyValuePair(Of Byte, Integer) In eg_char eg_encoding.Add(pair.Key, e_goldbach(2 * (i + 3))) i += 1
Next End Sub
Private Shared Function e_goldbach(i As Integer) As String Dim sb As New StringBuilder()
Dim bil As Integer = 0, bil2 As Integer = 0 Dim bil_akhir As Integer = Bil_Prima.Last() For j As Integer = bil_akhir + 1 To i If lehmann(j) Then
End If
Public Shared Function kompresi(s_values As Byte()) As Byte() flag = 0
Public Shared Function dekompresi(s_values As Byte()) As Byte() Dim hasil As New List(Of Byte)()
Dim i As Integer = 0
Dim sb As New StringBuilder() For i = 0 To s_values.Length - 2
sb.Append(Convert.ToString(s_values(i), 2).PadLeft(8, "0"c)) Next
If flag = 1 Then
sb.Append(Convert.ToString(s_values(i), 2).Remove(0, 1)) Else
sb.Append(Convert.ToString(s_values(i), 2).PadLeft(8, "0"c)) End If
decoding(hasil, sb) Return hasil.ToArray() End Function
Private Shared Sub decoding(hasil As List(Of Byte), sb As StringBuilder) Dim bin As New StringBuilder()
Dim n As Integer = 0, i As Integer = 0 While i < sb.Length
If sb(i) = "1"c Then n += 1
End If
bin.Append(sb(i)) If n = 2 Then
hasil.Add(eg_encoding.FirstOrDefault(Function(x) x.Value = bin.ToString()).Key)
bin.Clear() n = 0 End If i += 1 End While End Sub
Private Shared Function lehmann(i As Integer) As Boolean Dim a As New Random()
Dim coba As Double = 0 For j As Integer = 1 To 10 coba = a.[Next](2, i - 1)
coba = CDbl(BigInteger.ModPow(CType(coba, BigInteger), (i - 1) \ 2, i))
If coba <> 1 AndAlso coba - i <> -1 Then Return False
End If Next
Return True End Function End Class
FormDekomp.VB
Public Class DekompresiForm
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Tampung.hasil_dekompresi = GoldBach.dekompresi(Tampung.hasil_kompresi) Dim action As New Action(Of BigInteger)(AddressOf hasilenk)
Array.ForEach(Tampung.luc_c_kuncia, action)
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then System.IO.File.WriteAllText(SaveFileDialog1.FileName,
sb.ToString()) End If
TextBox2.Text =
System.Text.Encoding.Unicode.GetString(Tampung.hasil_dekompresi) End Sub
Private Shared Sub hasilenk(val As BigInteger) sb.Append(val)
sb.Append("-") End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then TextBox1.Text = OpenFileDialog1.FileName
End If End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint