My project is, i will take 128 bit plain text and i have to produce 128 bit cipher text by applying two functions in the middle.they are F and G through 16 rounds.
user enters plain text of alphabets and numbers in first textbox and he will get cipher text in numbers in second text box.16th round result is cipher text.i want to display output of each round in second text box.
my problem is i am getting output for 2 rounds only.in the last for loop of this button, i have to pass value to function F. in the first round, it is giving result. from second round,it is giving zeros for remaining rounds.
this is the code i have written.
Public Class Encryption
Shared countf As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ip As String
Dim ch As Char
Dim asci As Integer
Dim bin As String = Nothing
Dim n As Decimal
Dim iplen As Integer
Dim ip2 As String = Nothing
Dim result As String
Dim ch1 As Char
Dim bin1 As String = Nothing
Dim op(3) As Decimal
Dim lc As Integer = 0
Dim z(3) As Integer
Dim k(31) As Decimal
Dim s As String = Nothing
Dim l(15) As Decimal
Dim r(15) As Decimal
Dim t As Decimal
ip = TextBox1.Text
'Converting of text in textbox into binary number
Try
For i As Integer = 0 To (TextBox1.Text.Length) - 1
ch = ip(i)
asci = CInt(Asc(ch))
bin = Convert.ToString(asci, 2)
bin1 += bin
Next
Catch
End Try
'validation of bits of input text
If bin1.Length = 129 Then
ip2 = bin1
ElseIf bin1.Length < 129 Then
iplen = CInt(bin1.Length)
ip2 = Nothing
For i As Integer = 1 To (129 - iplen)
ip2 = "0" + ip2
Next
ip2 += bin1
MessageBox.Show("text in bits" + ip2, "bits")
Try
'here i m dividing total bits into 64 bit blocks
For i As Integer = 0 To ip2.Length - 1 Step 64
result = Nothing
For j As Integer = i To i + 63
ch1 = ip2(j)
result = result + ch1
ch1 = Nothing
Next
'converting of each of 64 bits block to decimal number
n = (CDec(Convert.ToUInt64(result, 2)))
If lc = 0 Then
l(0) = Decimal.Parse(n.ToString)
Else
r(0) = Decimal.Parse(n.ToString)
End If
lc = lc + 1
Next
Catch
End Try
MessageBox.Show("input text by dividing 2 parts " + l(0).ToString + " " + r(0).ToString, "Input")
End If
Try
For b As Integer = 0 To 14
t = Decimal.Parse(r(b).ToString)
r(b + 1) = Decimal.Parse((l(b) And F(r(b))).ToString)
l(b + 1) = Decimal.Parse(t.ToString)
Next
Catch
End Try
For c As Integer = 0 To 15
TextBox2.Text = TextBox2.Text + " " + l(c).ToString + " " + r(c).ToString '+ " " + l(1).ToString + " " + r(1).ToString + +" " + l(2).ToString + " " + r(2).ToString
Next
End Sub
Function F(ByRef ip As Decimal) As Decimal
Dim bin As String
Dim n As Decimal
Dim ip2 As String
Dim result As String
Dim ch As Char
Dim num As Integer
Dim c As Long
Dim d As Long
Dim c1 As Long
Dim d1 As Long
Dim a As Long
Dim b As Long
Dim f1 As Long
Dim z(3) As Integer
Dim s As String = Nothing
Dim k() As Long = {2146785105, 2121609512, 2132724685, 2110962159, 1777609125, 1777484065, 1777528775, 2124059416, 2103940113, 1712236890, 1712351197, 2112959283, 1712249125, 1691240437, 915140092, 1823351197, 2145030523, 1614960196, 1695141303, 1895138305, 2093158212, 1822051116, 2113158213, 2103830027, 1416142340, 851487675, 761688697, 754031940, 2078714880, 2101806081, 1679907081, 2137483649}
Dim l As Decimal
Dim res As String = Nothing
ip2 = Nothing
bin = Nothing
n = CDec(ip)
bin = Binary(n)
MessageBox.Show("in function F bin::" + bin)
l = CDec(bin.Length)
'checking of data coming to function f wheather it is 64 bit or not
Try
If l = 65 Then
ip2 = bin
MessageBox.Show("Binary number of input::::" + ip2)
ElseIf l < 65 Then
ip2 = Nothing
For i As Integer = 1 To (65 - l)
ip2 = "0" + ip2
Next
ip2 += bin
Else
MessageBox.Show("Too large Numberrrrrrr")
End If
Dim count As Integer = 0
Try
For i As Integer = 0 To ip2.Length Step 32
result = Nothing
For j As Integer = i To i + 31
ch = ip2(j)
result += ch
Next
num = CDec(Convert.ToUInt64(result, 2))
If count = 0 Then
c = Long.Parse(num.ToString)
Else
d = Long.Parse(num.ToString)
End If
count = count + 1
Next
Catch
End Try
a = (c Xor k(countf))
b = (d Xor k(countf + 1))
f1 = (a Xor b)
'here 32 keys will be available.in each round,2 keys should be used.i declare countf as global variable and each time function F was called,its value will be incremented by 2.so in each round corresponding keys only will be used
c1 = ((G((G((G(f1) + a) Mod 2147483648)) + (G(f1)) Mod 2147483648)) + (G((G(f1) + a) Mod 2147483648)) Mod 2147483648)
MessageBox.Show("c1=" + (c1.ToString))
d1 = G((G(((((c Xor k(countf)) Xor (d Xor k(countf + 1)))) + (c Xor k(countf)) Mod 2147483648)) + (G((c Xor k(countf)) Xor (d Xor k(countf + 1))))) Mod 2147483648)
MessageBox.Show("d1=" + (d1.ToString))
Catch
End Try
countf = countf + 2
res = (Binary(CDec(c1))) + Binary(CDec(d1))
Dim ret As Decimal = Decimal.Parse((Convert.ToUInt64(res, 2)).ToString)
Return ret
End Function
Function G(ByRef n1 As Decimal) As Decimal
Dim bin As String
Dim n As Decimal
Dim ip2 As String
Dim iplen As Integer
Dim result As String
Dim ch As Char
Dim num As Integer
Dim x(3) As Decimal
Dim l As Integer = 0
Dim z(3) As Decimal
Dim resG As String = Nothing
Dim m() As Decimal = {252, 243, 207, 63}
Dim s1() As Decimal = {169, 133, 214, 211, 84, 29, 172, 37, 93, 67, 24, 30, 81, 252, 202, 99, 40, 68, 32, 157, 224, 226, 200, 23, 165, 143, 3, 123, 187, 19, 210, 238, 112, 140, 63, 168, 50, 221, 246, 116, 236, 149, 11, 87, 92, 91, 189, 1, 36, 28, 115, 152, 16, 204, 242, 217, 44, 231, 114, 131, 155, 209, 134, 201, 96, 80, 163, 235, 13, 182, 158, 79, 183, 90, 198, 120, 166, 18, 175, 213, 97, 195, 180, 65, 82, 125, 141, 8, 31, 153, 0, 25, 4, 83, 247, 225, 253, 118, 47, 39, 176, 139, 14, 171, 162, 110, 147, 77, 105, 124, 9, 10, 191, 239, 243, 197, 135, 20, 254, 100, 222, 46, 75, 26, 6, 33, 107, 102, 2, 245, 146, 138, 12, 179, 126, 208, 122, 71, 150, 229, 38, 128, 173, 223, 161, 48, 55, 174, 54, 21, 34, 56, 244, 167, 69, 76, 129, 233, 132, 151, 53, 203, 206, 60, 113, 17, 199, 137, 117, 251, 218, 248, 148, 89, 130, 196, 255, 73, 57, 103, 192, 207, 215, 184, 15, 142, 66, 35, 145, 108, 219, 164, 52, 241, 72, 194, 111, 61, 45, 64, 190, 62, 188, 193, 170, 186, 78, 85, 59, 220, 104, 127, 156, 216, 74, 86, 119, 160, 237, 70, 181, 43, 101, 250, 227, 185, 177, 159, 94, 249, 230, 178, 49, 234, 109, 95, 228, 240, 205, 136, 22, 58, 88, 212, 98, 41, 7, 51, 232, 27, 5, 121, 144, 106, 42, 154}
Dim s2() As Decimal = {56, 232, 45, 166, 207, 222, 179, 184, 175, 96, 85, 199, 68, 111, 107, 91, 195, 98, 51, 181, 41, 160, 226, 167, 211, 145, 17, 6, 28, 188, 54, 75, 239, 136, 108, 168, 23, 196, 22, 244, 194, 69, 225, 214, 63, 61, 142, 152, 40, 78, 246, 62, 165, 249, 13, 223, 216, 43, 102, 122, 39, 47, 241, 114, 66, 212, 65, 192, 115, 103, 172, 139, 247, 173, 128, 31, 202, 44, 170, 52, 210, 11, 238, 233, 93, 148, 24, 248, 87, 174, 8, 197, 19, 205, 134, 185, 255, 125, 193, 49, 245, 138, 106, 177, 209, 32, 215, 2, 34, 4, 104, 113, 7, 219, 157, 153, 97, 190, 230, 89, 221, 81, 144, 220, 154, 163, 171, 208, 129, 15, 71, 26, 227, 236, 141, 191, 150, 123, 92, 162, 161, 99, 35, 77, 200, 158, 156, 58, 12, 46, 186, 110, 159, 90, 242, 146, 243, 73, 120, 204, 21, 251, 112, 117, 127, 53, 16, 3, 100, 109, 198, 116, 213, 180, 234, 9, 118, 25, 254, 64, 18, 224, 189, 5, 250, 1, 240, 42, 94, 169, 86, 67, 133, 20, 137, 155, 176, 229, 72, 121, 151, 252, 30, 130, 33, 140, 27, 95, 119, 84, 178, 29, 37, 79, 0, 70, 237, 88, 82, 235, 126, 218, 201, 253, 48, 149, 101, 60, 182, 228, 187, 124, 14, 80, 57, 38, 50, 132, 105, 147, 55, 231, 36, 164, 203, 83, 10, 135, 217, 76, 131, 143, 206, 59, 74, 183}
ip2 = Nothing
bin = Nothing
Try
n = n1
bin = Binary(CDec(n1))
Catch
End Try
Try
If bin.Length = 32 Then
ip2 = bin
ElseIf bin.Length < 32 Then
iplen = CInt(bin.Length)
ip2 = Nothing
For i As Integer = 1 To (32 - iplen)
ip2 = "0" + ip2
Next
ip2 += bin
Else
MsgBox("Number is Too large", MsgBoxStyle.Information, "Info")
End
End If
Catch
End Try
Try
For i As Integer = 0 To ip2.Length - 1 Step 8
result = Nothing
For j As Integer = i To i + 7
ch = ip2(j)
result += ch
Next
num = CInt(Convert.ToInt32(result, 2))
x(l) = Decimal.Parse(num.ToString)
l = l + 1
Next
Catch
End Try
z(0) = CDec(s1(x(0)) And m(0) Xor s2(x(1) And m(1)) Xor s1(x(2) And m(2)) Xor s2(x(3) And m(3)))
z(1) = CDec(s1(x(0)) And m(1) Xor s2(x(1) And m(2)) Xor s1(x(2) And m(3)) Xor s2(x(3) And m(0)))
z(2) = CDec(s1(x(0)) And m(2) Xor s2(x(1) And m(3)) Xor s1(x(2) And m(0)) Xor s2(x(3) And m(1)))
z(3) = CDec(s1(x(0)) And m(3) Xor s2(x(1) And m(0)) Xor s1(x(2) And m(1)) Xor s2(x(3) And m(2)))
resG = Binary(CDec(z(0))) + Binary(CDec(z(1))) + Binary(CDec(z(2))) + Binary(CDec(z(3)))
Dim Glng As Decimal = Decimal.Parse((Convert.ToInt32(resG, 2)).ToString)
Return Glng
End Function
Function Binary(ByVal Num As Decimal) As String
Binary = ""
Try
Do
Binary = IIf(Num / 2 <> Int(Num / 2), 1, 0) & Binary
Num = Int(Num / 2)
Loop Until Num = 0
Catch
End Try
Return Binary
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim ip As String
Dim ch As Char
Dim asci As Integer
Dim bin As String
Dim bin1 As String = Nothing
Dim l As Integer = 0
ip = TextBox1.Text
Try
For i As Integer = 0 To (TextBox1.Text.Length) - 1
ch = ip(i)
asci = CInt(Asc(ch))
bin = Convert.ToString(asci, 2)
bin1 += bin.ToString
Next
Catch
End Try
Try
l = CInt(bin1.Length)
If l > 128 Then
MessageBox.Show("Too Large Text", "Info")
TextBox1.Clear()
TextBox1.Focus()
End If
Catch
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
End Class