LISTING PROGRAM
1.
Listing Program untuk Mainform
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Infragistics.Win.AppStyling.StyleManager.Load("MyStyleSet3.isl") Timer1.Start()
End Sub
Private Sub UltraToolbarsManager1_ToolClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) Handles UltraToolbarsManager1.ToolClick If (e.Tool.Key = "Encryption") Then
Dim ctrl As New EncryptControl
UltraPanel1.ClientArea.Controls.Clear() UltraPanel1.ClientArea.Controls.Add(ctrl) UltraStatusBar1.Panels(0).Text = e.Tool.Key ElseIf (e.Tool.Key = "Decryption") Then Dim ctrl As New DecryptControl UltraPanel1.ClientArea.Controls.Clear() UltraPanel1.ClientArea.Controls.Add(ctrl) UltraStatusBar1.Panels(0).Text = e.Tool.Key ElseIf (e.Tool.Key = "Help") Then
System.Diagnostics.Process.Start(Application.StartupPath & "\HelpSystem.chm") ElseIf (e.Tool.Key = "About") Then
Dim ctrl As New AboutForm ctrl.ShowDialog()
UltraStatusBar1.Panels(0).Text = e.Tool.Key ElseIf (e.Tool.Key = "Light") Then
Infragistics.Win.AppStyling.StyleManager.Load("MyStyleSet2.isl") ElseIf (e.Tool.Key = "Dark") Then
Infragistics.Win.AppStyling.StyleManager.Load("MyStyleSet.isl") ElseIf (e.Tool.Key = "Purple") Then
Infragistics.Win.AppStyling.StyleManager.Load("MyStyleSet3.isl") ElseIf (e.Tool.Key = "Exit") Then
Me.Close() End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
UltraStatusBar1.Panels(1).Text = Date.Now.ToString("dddd, d-MMMM-yyyy hh:mm:ss tt") End Sub
If (MessageBox.Show("Exit Application?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No) Then
e.Cancel = True Else
Dim frm = Application.OpenForms("SplashScreen") frm.Close()
End If End Sub End Class
2.
Listing Program untuk Form Encrypt
Imports System.Numerics Imports System.IO
Imports Infragistics.Win.UltraWinGrid Imports System.Diagnostics
Public Class EncryptControl
Private isLarge As Boolean = False
Private Sub txtPlaintext_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPlaintext.ValueChanged
lblChar.Text = ": " & txtPlaintext.TextLength & " Char" End Sub
Private Function checkKey() As Boolean Dim retValue As Boolean = True
For Each ctrl In UltraGroupBox3.Controls If TypeName(ctrl) = "UltraTextEditor" Then If ctrl.Text = "" Then
retValue = False End If
End If Next
Return retValue End Function
Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
If (txtPlaintext.Text = "") Then
MessageBox.Show("Input your Plaintext!", "Encrypt Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtPlaintext.Focus() Exit Sub
ElseIf (Not checkKey()) Then
MessageBox.Show("Your Key is missing!", "Encrypt Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Dim EncryptTime As Stopwatch = New Stopwatch() Dim time As String = ""
EncryptTime.Start() ciphertext = ""
Dim trans As Transpose = New Transpose
txtTransCipher.Text = trans.TransposeEncrypt(txtPlaintext.Text) TriGrid.DataSource = trans.getTriangle()
TriGrid.DisplayLayout.Override.DefaultColWidth = 25
TriGrid.DisplayLayout.Override.HeaderClickAction = HeaderClickAction.Select TriGrid.DisplayLayout.Override.SelectTypeCol = SelectType.None
TriGrid.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect
txtCiphertext.Text = Elgamal.Encrypt(txtTransCipher.Text) EncryptTime.Stop()
If EncryptTime.ElapsedMilliseconds < 1000 Then
time = EncryptTime.ElapsedMilliseconds.ToString & " miliseconds" ElseIf EncryptTime.ElapsedMilliseconds < 60000 Then
time = Math.Round((EncryptTime.ElapsedMilliseconds / 1000), 3).ToString & " seconds" Else
time = Math.Round((EncryptTime.ElapsedMilliseconds / 60000), 3).ToString & " minutes" End If
lblTimeEncrypt.Text = "Time to Encrypt File : " & time End Sub
Private Sub btnGetPrime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPrime.Click
txtPrime.Text = Elgamal.getPrime(optBit.Value).ToString() End Sub
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
If txtPrime.Text = "" Then
MessageBox.Show("Choose Prime Number First", "Generate Key", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtPrime.Focus() Exit Sub
End If
Dim GenerateTime As Stopwatch = New Stopwatch() GenerateTime.Start()
Elgamal.GenerateKey(BigInteger.Parse(txtPrime.Text)) If Elgamal.isInputPrime = True Then
txtAlpha.Text = alpha txtA.Text = a
txtAlphaA.Text = alphaA Else
txtAlphaA.Text = "" txtPrime.Focus() End If
GenerateTime.Stop() Dim time As String = ""
If GenerateTime.ElapsedMilliseconds < 1000 Then
time = GenerateTime.ElapsedMilliseconds.ToString & " miliseconds" ElseIf GenerateTime.ElapsedMilliseconds < 60000 Then
time = Math.Round((GenerateTime.ElapsedMilliseconds / 1000), 3).ToString & " seconds" Else
time = Math.Round((GenerateTime.ElapsedMilliseconds / 60000), 3).ToString & " minutes" End If
lblTimeGenerate.Text = "Time to Generate Key : " & time
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim open As OpenFileDialog = New OpenFileDialog() open.Filter = "txt Files|*.txt"
open.FileName = "*.txt"
If (open.ShowDialog() = DialogResult.OK) Then
Dim fstream As FileStream = New FileStream(open.FileName, FileMode.Open, FileAccess.ReadWrite)
Dim sreader As StreamReader = New StreamReader(fstream) sreader.BaseStream.Seek(0, SeekOrigin.Begin)
txtPlaintext.Text = sreader.ReadToEnd() sreader.Close()
lblFilename.Text = ": " & Path.GetFileName(open.FileName) lblLocation.Text = ": " & Path.GetFullPath(open.FileName) End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim saveFileDialog1 As SaveFileDialog = New SaveFileDialog saveFileDialog1.Filter = "txt files |*.txt|All files |*.*"
saveFileDialog1.Title = "Save File" saveFileDialog1.FileName = "*.txt"
If (saveFileDialog1.ShowDialog() = DialogResult.OK) Then Dim filename As String = saveFileDialog1.FileName
Dim fstream As FileStream = New FileStream(filename, FileMode.OpenOrCreate) Dim sw As StreamWriter = New StreamWriter(fstream)
Dim seekorigin As SeekOrigin = New SeekOrigin() sw.BaseStream.Seek(0, seekorigin)
sw.WriteLine(txtCiphertext.Text) sw.Flush()
End Sub
Private Sub TriGrid_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TriGrid.MouseHover
If TriGrid.Dock = DockStyle.None Then
ToolTip1.Show("Double Click to Enlarge", Me.TriGrid, 1000) Else
ToolTip1.Hide(Me.TriGrid) End If
End Sub
Private Sub TriGrid_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TriGrid.DoubleClick
If TriGrid.Dock = DockStyle.Fill Then isLarge = False
UltraGroupBox4.Dock = DockStyle.None TriGrid.Dock = DockStyle.None
Else
isLarge = True
UltraGroupBox4.Dock = DockStyle.Fill TriGrid.Dock = DockStyle.Fill
UltraGroupBox4.BringToFront() TriGrid.BringToFront()
EscSplash.Show()
ToolTip1.Hide(Me.TriGrid) End If
End Sub
Private Sub TriGrid_KeyDown(ByVal 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 TriGrid_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TriGrid.MouseLeave
isLarge = True End Sub
Private Sub EncryptControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
primeNumber = 0 alpha = 0
End Sub
Private Sub txtPrime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrime.ValueChanged
If txtPrime.Text <> "" Then primeNumber = txtPrime.Text End If
End Sub
End Class
3.
Listing Program untuk Form Decrypt
Imports System.Numerics Imports System.IO
Imports Infragistics.Win.UltraWinGrid Public Class DecryptControl
Private isLarge As Boolean = False
Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
If (txtCiphertext.Text = "") Then
MessageBox.Show("Input your Ciphertext", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtCiphertext.Focus() Exit Sub
End If
If (txtPrime.Text = "") Then
MessageBox.Show("Prime number is missing", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtPrime.Focus() Exit Sub
End If
If (txtA.Text = "") Then
MessageBox.Show("Private Key is missing", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtA.Focus() Exit Sub End If
If (Not (Elgamal.Fermat(primeNumber))) Then
MessageBox.Show("Number is not Prime", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtPrime.Focus() Exit Sub
End If
Dim time As String = "" EncryptTime.Start()
txtPlaintext.Text = Elgamal.Decrypt(txtCiphertext.Text)
Dim trans As Transpose = New Transpose
txtTransPlain.Text = trans.TransposeDecrypt(txtPlaintext.Text) TriGrid.DataSource = trans.getTriangle()
TriGrid.DisplayLayout.Override.DefaultColWidth = 25
TriGrid.DisplayLayout.Override.HeaderClickAction = HeaderClickAction.Select TriGrid.DisplayLayout.Override.SelectTypeCol = SelectType.None
TriGrid.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect
EncryptTime.Stop()
If EncryptTime.ElapsedMilliseconds < 1000 Then
time = EncryptTime.ElapsedMilliseconds.ToString & " miliseconds" ElseIf EncryptTime.ElapsedMilliseconds < 60000 Then
time = Math.Round((EncryptTime.ElapsedMilliseconds / 1000), 3).ToString & " seconds" Else
time = Math.Round((EncryptTime.ElapsedMilliseconds / 60000), 3).ToString & " minutes" End If
lblTimeDecrypt.Text = "Time to Decrypt File : " & time End Sub
Private Sub txtPrime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrime.ValueChanged
If (txtPrime.Text <> "") Then
primeNumber = BigInteger.Parse(txtPrime.Text) End If
End Sub
Private Sub txtA_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtA.ValueChanged
If txtA.Text <> "" Then
a = BigInteger.Parse(txtA.Text) End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim saveFileDialog1 As SaveFileDialog = New SaveFileDialog saveFileDialog1.Filter = "txt files |*.txt|All files |*.*"
saveFileDialog1.Title = "Save File" saveFileDialog1.FileName = "*.txt"
If (saveFileDialog1.ShowDialog() = DialogResult.OK) Then Dim filename As String = saveFileDialog1.FileName
Dim fstream As FileStream = New FileStream(filename, FileMode.OpenOrCreate) Dim sw As StreamWriter = New StreamWriter(fstream)
sw.BaseStream.Seek(0, seekorigin) sw.WriteLine(txtTransPlain.Text) sw.Flush()
sw.Close() End If End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim open As OpenFileDialog = New OpenFileDialog() open.Filter = "txt Files|*.txt"
open.FileName = "*.txt"
If (open.ShowDialog() = DialogResult.OK) Then
Dim fstream As FileStream = New FileStream(open.FileName, FileMode.Open, FileAccess.ReadWrite)
Dim sreader As StreamReader = New StreamReader(fstream) sreader.BaseStream.Seek(0, SeekOrigin.Begin)
txtCiphertext.Text = sreader.ReadToEnd() sreader.Close()
lblFilename.Text = ": " & Path.GetFileName(open.FileName) lblLocation.Text = ": " & Path.GetFullPath(open.FileName) End If
End Sub
Private Sub txtCiphertext_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCiphertext.ValueChanged
lblChar.Text = ": " & txtCiphertext.Text.Split(" ").Length - 1 & " Char" End Sub
Private Sub TriGrid_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TriGrid.MouseHover
If TriGrid.Dock = DockStyle.None Then
ToolTip1.Show("Double Click to Enlarge", Me.TriGrid, 1000) Else
ToolTip1.Hide(Me.TriGrid) End If
End Sub
Private Sub TriGrid_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TriGrid.DoubleClick
If TriGrid.Dock = DockStyle.Fill Then isLarge = False
UltraGroupBox4.Dock = DockStyle.None TriGrid.Dock = DockStyle.None
Else
isLarge = True
UltraGroupBox4.Dock = DockStyle.Fill TriGrid.Dock = DockStyle.Fill
UltraGroupBox4.BringToFront() TriGrid.BringToFront()
ToolTip1.Hide(Me.TriGrid) End If
End Sub
Private Sub TriGrid_KeyDown(ByVal 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_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
isLarge = True End Sub
Private Sub DecryptControl_Load(ByVal 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 Fermat(ByVal prime As BigInteger) As Boolean Dim a As BigInteger = 0
For i = 0 To prime.ToString().Length a = Random(prime)
End If Next Return True End Function
Public Shared Function Random(ByVal prime As BigInteger) As BigInteger Dim bigVal As BigInteger
Dim limit As Integer = Rnd.Next(2, 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.Next(2, 10)
strVal += val.ToString() Next
bigVal = BigInteger.Parse(strVal) Else
Dim val As Integer For i = 0 To limit - 1 If (i = 0) Then
val = Rnd.Next(0, Convert.ToInt32(prime.ToString().Substring(0, 1))) strVal += val.ToString()
Else
val = Rnd.Next(0, 10) strVal += val.ToString() End If
Next
bigVal = BigInteger.Parse(strVal) End If
Loop While (bigVal < 2 Or bigVal >= prime) Return bigVal
End Function
Public Shared Function orderNumber(ByVal prime As Integer, ByVal n As Integer) As BigInteger Dim hasil As BigInteger = 0
Dim i As Integer = 1 While hasil <> 1
hasil = BigInteger.ModPow(n, i, prime) i += 1
End While
Return i - 1 End Function
Public Shared Function getTotien(ByVal value As Integer) As Integer Dim retValue As Integer = 0
For i = 1 To value
If (BigInteger.GreatestCommonDivisor(i, value).IsOne) Then retValue += 1
Return retValue End Function
Public Shared Function getPrimitiveRoot(ByVal 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 List(Of Long) = FindFactors(prime - 1) Dim nilaiRandom As Integer
leastPrimitive = powerTest(factor) primitiveRoots.Add(leastPrimitive)
If prime < 9999 Then
For i = leastPrimitive To prime - 1
If (BigInteger.GreatestCommonDivisor(i, prime - 1).IsOne) Then primitiveRoots.Add(i)
End If Next
Dim ordNum As BigInteger Do
nilaiRandom = Rnd.Next(0, primitiveRoots.Count - 1)
ordNum = orderNumber(prime, Integer.Parse(primitiveRoots(nilaiRandom).ToString())) If totienP = ordNum Then
hasil = BigInteger.Parse(primitiveRoots(nilaiRandom).ToString()) End If
Loop While totienP <> ordNum
Else
While prime > hasil
If (BigInteger.GreatestCommonDivisor(powers, prime - 1).IsOne) Then hasil = Math.Pow(leastPrimitive, powers)
primitiveRoots.Add(hasil) End If
powers += 1 End While
primitiveRoots.RemoveAt(primitiveRoots.Count - 1)
nilaiRandom = Rnd.Next(0, primitiveRoots.Count - 1)
If (BigInteger.ModPow(BigInteger.Parse(primitiveRoots(nilaiRandom).ToString()), BigInteger.Parse(totienP.ToString()), prime).IsOne) Then
hasil = BigInteger.Parse(primitiveRoots(nilaiRandom).ToString()) End If
End If
Return hasil End Function
Dim isPrime As Boolean = False Dim prime As BigInteger = 0 While (Not isPrime)
Dim value As BigInteger = Rnd.Next(100, bit) If (Fermat(value)) Then
isPrime = True
prime = value.ToString() End If
End While
Return prime End Function
Public Shared Sub GenerateKey(ByVal prime As BigInteger) primitiveRoots.Clear()
primeNumber = prime If primeNumber > 9999 Then KeyLimit = 9999
Else
KeyLimit = primeNumber End If
If (Fermat(primeNumber)) Then
alpha = getPrimitiveRoot(primeNumber) a = Rnd.Next(2, KeyLimit - 1)
alphaA = BigInteger.ModPow(alpha, a, primeNumber) isInputPrime = True
Else
MessageBox.Show("The Number is not Prime", "Generate Key", MessageBoxButtons.OK, MessageBoxIcon.Warning)
isInputPrime = False End If
End Sub
Public Shared Function Encrypt(ByVal 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 BigInteger(plaintext.Length) {} cipher2 = New BigInteger(plaintext.Length) {} Dim rnd As New Random
For Each c In plaintext
m = System.Convert.ToInt32(c) b = rnd.Next(2, KeyLimit - 1)
cipher1(idx) = BigInteger.ModPow(alpha, b, primeNumber)
cipher2(idx) = (m * (BigInteger.Pow(alphaA, b))) Mod primeNumber idx += 1
Next
ciphertext &= cipher1(i).ToString() & " " Next
ciphertext += "/"
For i = 0 To cipher2.Length - 2
ciphertext &= cipher2(i).ToString() & " " Next
Return ciphertext End Function
Public Shared Function Decrypt(ByVal 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 = ciphertext(0).Split(" ")
cipher1 = New BigInteger(cip1.Length - 2) {} For i = 0 To cip1.Length - 2
cipher1(i) = cip1(i) Next
Dim cip2 = ciphertext(1).Split(" ")
cipher2 = New BigInteger(cip2.Length - 2) {} For i = 0 To cip2.Length - 2
cipher2(i) = cip2(i) Next
For i = 0 To cipher1.Length - 1
m1 = BigInteger.ModPow(cipher1(i), (primeNumber - 1 - a), primeNumber) m2 = (m1 * cipher2(i)) Mod primeNumber
plaintext &= Char.ConvertFromUtf32(m2) Next
Return plaintext End Function
Public Shared Function FindFactors(ByVal num As Long) As List(Of Long) Dim result As List(Of Long) = New List(Of Long)()
Do While (num Mod 2 = 0) result.Add(2)
num \= 2 Loop
Dim factor As Long = 3
Do While (factor * factor <= num) If (num Mod factor = 0) Then result.Add(factor)
num \= factor Else
End If Loop
If (num > 1) Then result.Add(num) Dim flag As Integer = 0
For i = 1 To result.Count - 1 If (result(flag) = result(i)) Then result(i) = 0
Else flag = i End If Next
For i = 1 To result.Count - 1 result.Remove(0)
Next
Return result End Function
Public Shared Function powerTest(ByVal factors As List(Of Long)) As Integer Dim hasil As Integer = 1
Dim value As Integer = 2
Dim powertes As List(Of Long) = New List(Of Long)()
For i = 0 To factors.Count - 1
powertes.Add(((primeNumber - 1) / factors(i))) Next
While hasil = 1
For i = 0 To powertes.Count - 1
hasil = BigInteger.ModPow(value, powertes(i), 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
Public Class Transpose
Public ciphertext As String = "" Public plaintext As String = "" Dim pola As String = "" Dim textLength As Integer = 0 Dim j As Integer = 1
Dim limit As Integer = 1 Dim row As Integer = 1 Dim col As Integer = 0 Dim triangle As String(,) #End Region
Public Function TransposeEncrypt(ByVal plain As String) As String plaintext = plain
textLength = plain.Length limit = 1
row = 1 j = 1
For i = 1 To textLength If textLength > limit Then j += 2
limit += j row += 1 End If Next
j = 1 col = 0
For i = 1 To row col += j j = 2 Next row -= 1 col -= 1
triangle = New String(row, col) {}
Dim mid As Integer = Math.Floor((col + 1) / 2) Dim idx As Integer = 0
For i = 0 To row
For j = (mid - i) To (mid + i) If (textLength <= idx) Then triangle(i, j) = Chr(191) Else
triangle(i, j) = plaintext(idx) idx += 1
End If
Next Next
For j = 0 To row
If (triangle(j, i) <> "") Then ciphertext += triangle(j, i) End If
Next Next
Return ciphertext End Function
Public Function TransposeDecrypt(ByVal cipher As String) As String Dim plain As String = ""
ciphertext = cipher
Dim textLength As Integer = cipher.Length j = 1
limit = 1 row = 1 pola = ""
For i = 1 To textLength If textLength > limit Then j += 2
limit += j row += 1 End If Next
j = 1 col = 0
For i = 1 To row col += j j = 2 Next row -= 1 col -= 1
triangle = New String(row, col) {} Dim mid As Integer = col / 2 Dim idx As Integer = 0
For i = 0 To col If i > mid Then
For j = i - row To row
triangle(j, i) = ciphertext(idx) idx += 1
Next Else
For j = (row - i) To row
triangle(j, i) = ciphertext(idx) idx += 1
Next
For i = 0 To row For j = 0 To col
If (triangle(i, j) <> "") Then plain += triangle(i, j) End If
Next Next
plaintext = plain.Trim(Chr(191))
Return plaintext End Function
Public Function getTriangle() As DataTable Dim dt As DataTable = New DataTable() For i = 0 To col
dt.Columns.Add(New DataColumn(i + 1)) Next
For i = 0 To row dt.Rows.Add() Next
For i = 0 To row For j = 0 To col
dt.Rows(i)(j) = triangle(i, j) Next
Next
Return dt End Function