ToolTip1.HideMe.TriGrid End If
End Sub Private Sub TriGrid_KeyDownByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs Handles TriGrid.KeyDown If e.KeyCode = Keys.Escape Then
isLarge = False UltraGroupBox4.Dock = DockStyle.None
TriGrid.Dock = DockStyle.None End If
End Sub Private Sub DecryptControl_MouseLeaveByVal sender As Object, ByVal e As System.EventArgs
Handles Me.MouseLeave isLarge = True
End Sub Private Sub DecryptControl_LoadByVal sender As System.Object, ByVal e As System.EventArgs
Handles MyBase.Load alpha = 0
b = 0 alphaA = 0
plaintext = 0 ciphertext = 0
txtPrime.Text = primeNumber.ToString txtA.Text = a.ToString
End Sub End Class
4. Listing Program untuk Class ElGamal
Imports System.Numerics Imports System.IO
Public Class Elgamal Public Shared isInputPrime As Boolean
Private Shared primitiveRoots As New ArrayList Dim r As BigInteger
Private Shared Rnd As Random = New Random Private Shared primitiveRootsTotal As Integer
Private Shared KeyLimit As Integer Public Shared Function FermatByVal prime As BigInteger As Boolean
Dim a As BigInteger = 0 For i = 0 To prime.ToString.Length
a = Randomprime If Not BigInteger.ModPowa, prime - 1, prime.IsOne Then
Return False
Universitas Sumatera Utara
End If Next
Return True End Function
Public Shared Function RandomByVal prime As BigInteger As BigInteger Dim bigVal As BigInteger
Dim limit As Integer = Rnd.Next2, prime.ToString.Length Do
Dim strVal As String = If prime.ToString.Length = 1 Then
For i = 0 To prime.ToString.Length - 1 Dim val = Rnd.Next2, 10
strVal += val.ToString Next
bigVal = BigInteger.ParsestrVal Else
Dim val As Integer For i = 0 To limit - 1
If i = 0 Then val = Rnd.Next0, Convert.ToInt32prime.ToString.Substring0, 1
strVal += val.ToString Else
val = Rnd.Next0, 10 strVal += val.ToString
End If Next
bigVal = BigInteger.ParsestrVal End If
Loop While bigVal 2 Or bigVal = prime Return bigVal
End Function Public Shared Function orderNumberByVal prime As Integer, ByVal n As Integer As BigInteger
Dim hasil As BigInteger = 0 Dim i As Integer = 1
While hasil 1 hasil = BigInteger.ModPown, i, prime
i += 1 End While
Return i - 1 End Function
Public Shared Function getTotienByVal value As Integer As Integer Dim retValue As Integer = 0
For i = 1 To value If BigInteger.GreatestCommonDivisori, value.IsOne Then
retValue += 1 End If
Next
Universitas Sumatera Utara
Return retValue End Function
Public Shared Function getPrimitiveRootByVal prime As BigInteger As Integer Dim totienP As Integer = prime - 1
Dim leastPrimitive As Integer = 0 Dim hasil As BigInteger = 0
Dim powers As Integer = 2 Dim factor As ListOf Long = FindFactorsprime - 1
Dim nilaiRandom As Integer leastPrimitive = powerTestfactor
primitiveRoots.AddleastPrimitive If prime 9999 Then
For i = leastPrimitive To prime - 1 If BigInteger.GreatestCommonDivisori, prime - 1.IsOne Then
primitiveRoots.Addi End If
Next Dim ordNum As BigInteger
Do nilaiRandom = Rnd.Next0, primitiveRoots.Count - 1
ordNum = orderNumberprime, Integer.ParseprimitiveRootsnilaiRandom.ToString If totienP = ordNum Then
hasil = BigInteger.ParseprimitiveRootsnilaiRandom.ToString End If
Loop While totienP ordNum Else
While prime hasil If BigInteger.GreatestCommonDivisorpowers, prime - 1.IsOne Then
hasil = Math.PowleastPrimitive, powers primitiveRoots.Addhasil
End If powers += 1
End While primitiveRoots.RemoveAtprimitiveRoots.Count - 1
nilaiRandom = Rnd.Next0, primitiveRoots.Count - 1 If BigInteger.ModPowBigInteger.ParseprimitiveRootsnilaiRandom.ToString,
BigInteger.ParsetotienP.ToString, prime.IsOne Then hasil = BigInteger.ParseprimitiveRootsnilaiRandom.ToString
End If End If
Return hasil End Function
Public Shared Function getPrimeByVal bit As Integer As BigInteger
Universitas Sumatera Utara
Dim isPrime As Boolean = False Dim prime As BigInteger = 0
While Not isPrime Dim value As BigInteger = Rnd.Next100, bit
If Fermatvalue Then isPrime = True
prime = value.ToString End If
End While Return prime
End Function Public Shared Sub GenerateKeyByVal prime As BigInteger
primitiveRoots.Clear primeNumber = prime
If primeNumber 9999 Then KeyLimit = 9999
Else KeyLimit = primeNumber
End If If FermatprimeNumber Then
alpha = getPrimitiveRootprimeNumber a = Rnd.Next2, KeyLimit - 1
alphaA = BigInteger.ModPowalpha, a, primeNumber isInputPrime = True
Else MessageBox.ShowThe Number is not Prime, Generate Key, MessageBoxButtons.OK,
MessageBoxIcon.Warning isInputPrime = False
End If End Sub
Public Shared Function EncryptByVal plain As String As String Dim cipher1 As BigInteger
Dim cipher2 As BigInteger Dim m As Integer
plaintext = plain Dim idx As Integer = 0
cipher1 = New BigIntegerplaintext.Length {} cipher2 = New BigIntegerplaintext.Length {}
Dim rnd As New Random For Each c In plaintext
m = System.Convert.ToInt32c b = rnd.Next2, KeyLimit - 1
cipher1idx = BigInteger.ModPowalpha, b, primeNumber cipher2idx = m BigInteger.PowalphaA, b Mod primeNumber
idx += 1 Next
For i = 0 To cipher1.Length - 2
Universitas Sumatera Utara
ciphertext = cipher1i.ToString Next
ciphertext += For i = 0 To cipher2.Length - 2
ciphertext = cipher2i.ToString Next
Return ciphertext End Function
Public Shared Function DecryptByVal cipher As String As String Dim cipher1 As BigInteger
Dim cipher2 As BigInteger Dim ciphertext = cipher.Split
Dim m1, m2 As BigInteger Dim plaintext As String =
Dim cip1 = ciphertext0.Split cipher1 = New BigIntegercip1.Length - 2 {}
For i = 0 To cip1.Length - 2 cipher1i = cip1i
Next Dim cip2 = ciphertext1.Split
cipher2 = New BigIntegercip2.Length - 2 {} For i = 0 To cip2.Length - 2
cipher2i = cip2i Next
For i = 0 To cipher1.Length - 1 m1 = BigInteger.ModPowcipher1i, primeNumber - 1 - a, primeNumber
m2 = m1 cipher2i Mod primeNumber plaintext = Char.ConvertFromUtf32m2
Next Return plaintext
End Function Public Shared Function FindFactorsByVal num As Long As ListOf Long
Dim result As ListOf Long = New ListOf Long Do While num Mod 2 = 0
result.Add2 num \= 2
Loop Dim factor As Long = 3
Do While factor factor = num If num Mod factor = 0 Then
result.Addfactor num \= factor
Else factor += 2
Universitas Sumatera Utara
End If Loop
If num 1 Then result.Addnum Dim flag As Integer = 0
For i = 1 To result.Count - 1 If resultflag = resulti Then
resulti = 0 Else
flag = i End If
Next For i = 1 To result.Count - 1
result.Remove0 Next
Return result End Function
Public Shared Function powerTestByVal factors As ListOf Long As Integer Dim hasil As Integer = 1
Dim value As Integer = 2 Dim powertes As ListOf Long = New ListOf Long
For i = 0 To factors.Count - 1 powertes.AddprimeNumber - 1 factorsi
Next While hasil = 1
For i = 0 To powertes.Count - 1 hasil = BigInteger.ModPowvalue, powertesi, primeNumber
If hasil = 1 Then Exit For
End If Next
value += 1 End While
Return value - 1 End Function
End Class
5. Listing Program untuk Class Transpose