· 7 years ago · Sep 18, 2018, 09:42 PM
1Imports System.Security.Cryptography
2Imports System.IO
3
4Public Class WebForm2
5 Inherits System.Web.UI.Page
6
7 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
8 End Sub
9
10 Private Sub btnCreateCookie_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateCookie.Click
11 Dim apaIni As String
12 apaIni = "YEInIGikIgaWsQtMoLPlYEUW5H88eoIRhdkMEmM0YsDHfKEng5xHsLkk4N4mIQfJGBX4prFcJ9NtHRQ4uR/FdfS/E4Y/y0r608BlO7+tBWI+p4ERxxLw0/PEkD6vwmye+bUKPC07+L+74SOchBg+/itkDWPBJjfAby2Tj/QARjo="
13 Dim decodedBytes As Byte()
14 decodedBytes = Convert.FromBase64String(apaIni)
15 Encoding.UTF8.GetString(decodedBytes)
16 lbl.Text &= "Decoded Data: " & Encoding.UTF8.GetString(decodedBytes) & "<br />"
17
18 Dim secret_key As String = "bc3ec20fa4f82ae68bc467d1efcccf102b097fb8"
19 Dim iv As String = Left(secret_key, 16)
20 Dim key As String = Left(secret_key, 32)
21
22 Using myRijndael = Rijndael.Create()
23
24
25 Dim roundtrip As String = DecryptStringFromBytes(decodedBytes, New System.Text.UTF8Encoding().GetBytes(key), _
26 New System.Text.UTF8Encoding().GetBytes(iv))
27 lbl.Text &= "Round Trip: " & roundtrip & "<br />"
28 End Using
29
30 'Response.Cookies("MyCookie")("Data") = "test"
31 ''Response.Cookies("MyCookie").Domain = "firstmedia.com"
32 'Response.Cookies("MyCookie").Expires = DateAdd(DateInterval.Day, 1, Now())
33 'MsgBox(Response.Cookies("MyCookie").Value.ToString)
34 End Sub
35
36 Private Sub btnCheckCookie_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckCookie.Click
37 Dim respond As String = ""
38 Dim newCookie As HttpCookie
39 If Request.Cookies("fmweb-test[User]") Is Nothing Then
40 respond = "My Cookie not found"
41 Else
42 newCookie = Request.Cookies("fmweb-test[User]")
43 respond = "My Cookie Found <br /> As of " & Format(Now, "ddMMyyyy hh:mm:ss") & " <br />"
44 respond &= "Data: " & newCookie.Values.ToString & "<br />"
45 End If
46 lbl.Text = respond
47 End Sub
48
49 Private Sub btnDelCookie_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelCookie.Click
50 Dim respond As String = ""
51 If Response.Cookies("MyCookie") Is Nothing Then
52 respond = "My Cookie didn't exist"
53 Else
54 Response.Cookies.Clear()
55 respond = "Delete My Cookie"
56 End If
57 If respond <> "" Then
58 MsgBox(respond)
59 End If
60 End Sub
61
62 Public Sub coba()
63 Try
64
65 Dim original As String = "Here is some data to encrypt!"
66
67 ' Create a new instance of the Rijndael
68 ' class. This generates a new key and initialization
69 ' vector (IV).
70 Using myRijndael = Rijndael.Create()
71
72 ' Encrypt the string to an array of bytes.
73 Dim encrypted As Byte() = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV)
74
75 ' Decrypt the bytes to a string.
76 Dim roundtrip As String = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV)
77
78 'Display the original data and the decrypted data.
79 lbl.Text &= "Original: " & original & "<br />"
80 lbl.Text &= "Original: " & New System.Text.ASCIIEncoding().GetString(encrypted) & "<br />"
81 lbl.Text &= "Round Trip: " & roundtrip & "<br />"
82 End Using
83 Catch e As Exception
84 lbl.Text &= "Error: " & e.Message
85 End Try
86
87 End Sub 'Main
88 Function EncryptStringToBytes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
89 ' Check arguments.
90 If plainText Is Nothing OrElse plainText.Length <= 0 Then
91 Throw New ArgumentNullException("plainText")
92 End If
93 If Key Is Nothing OrElse Key.Length <= 0 Then
94 Throw New ArgumentNullException("Key")
95 End If
96 If IV Is Nothing OrElse IV.Length <= 0 Then
97 Throw New ArgumentNullException("Key")
98 End If
99 Dim encrypted() As Byte
100 ' Create an Rijndael object
101 ' with the specified key and IV.
102 Using rijAlg = Rijndael.Create()
103 rijAlg.Key = Key
104 rijAlg.IV = IV
105 ' Create a decrytor to perform the stream transform.
106 Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
107 ' Create the streams used for encryption.
108 Using msEncrypt As New MemoryStream()
109 Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
110 Using swEncrypt As New StreamWriter(csEncrypt)
111
112 'Write all data to the stream.
113 swEncrypt.Write(plainText)
114 End Using
115 encrypted = msEncrypt.ToArray()
116 End Using
117 End Using
118 End Using
119
120 ' Return the encrypted bytes from the memory stream.
121 Return encrypted
122
123 End Function 'EncryptStringToBytes
124
125 Function DecryptStringFromBytes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
126 ' Check arguments.
127 If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
128 Throw New ArgumentNullException("cipherText")
129 End If
130 If Key Is Nothing OrElse Key.Length <= 0 Then
131 Throw New ArgumentNullException("Key")
132 End If
133 If IV Is Nothing OrElse IV.Length <= 0 Then
134 Throw New ArgumentNullException("Key")
135 End If
136 ' Declare the string used to hold
137 ' the decrypted text.
138 Dim plaintext As String = Nothing
139
140 ' Create an Rijndael object
141 ' with the specified key and IV.
142 Using rijAlg = Rijndael.Create()
143 rijAlg.Key = Key
144 rijAlg.IV = IV
145
146 ' Create a decrytor to perform the stream transform.
147 Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV)
148
149 ' Create the streams used for decryption.
150 Using msDecrypt As New MemoryStream(cipherText)
151
152 Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
153
154 Using srDecrypt As New StreamReader(csDecrypt)
155
156
157 ' Read the decrypted bytes from the decrypting stream
158 ' and place them in a string.
159 plaintext = srDecrypt.ReadToEnd()
160 End Using
161 End Using
162 End Using
163 End Using
164
165 Return plaintext
166
167 End Function 'DecryptStringFromBytes
168
169 Protected Sub btnenkript_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnenkript.Click
170
171 Dim iv As Byte() = New System.Text.UTF8Encoding().GetBytes(txtIV.Text)
172 Dim key As Byte() = New System.Text.UTF8Encoding().GetBytes(txtKey.Text)
173
174 Using myRijndael = Rijndael.Create()
175 Dim RijndaelResult As Byte() = EncryptStringToBytes(txtTTE.Text, key, iv)
176 lblEnkriptLog.Text = "Rijndael Encryption result : " & Encoding.UTF8.GetString(RijndaelResult) & "<br />"
177 lblEnkriptLog.Text &= "To Base64 : " & Convert.ToBase64String(RijndaelResult) & "<br />"
178 End Using
179 End Sub
180
181 Protected Sub btnDec_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDec.Click
182 Dim decodedBytes As Byte()
183 decodedBytes = Convert.FromBase64String(txtTTD.Text)
184 Encoding.UTF8.GetString(decodedBytes)
185 lbldecryptLog.Text = "Decoded Data: " & Encoding.UTF8.GetString(decodedBytes) & "<br />"
186
187 Dim iv As Byte() = New System.Text.UTF8Encoding().GetBytes(txtIV.Text)
188 Dim key As Byte() = New System.Text.UTF8Encoding().GetBytes(txtKey.Text)
189 Using myRijndael = Rijndael.Create()
190 Dim RijndaelResult As String = DecryptStringFromBytes(decodedBytes, key, iv)
191 lbldecryptLog.Text &= "Rijndael Decryption result : " & RijndaelResult & "<br />"
192 End Using
193 End Sub
194End Class