Files
2015-03-20 07:07:22 +00:00

308 lines
11 KiB
VB.net

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports System.Xml
Public Class KeyFileGenerator
Dim ProcId As String
Private Sub KeyFileGenerator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Decrypt deel is niet voor de gebruiker
Me.Width = 1120
Label5.Text = String.Empty
CreateKeyButton.Enabled = False
DecryptButton.Enabled = False
Me.Text = "Key File Generator - " & My.Application.Info.Version.Major.ToString & "." & My.Application.Info.Version.Minor.ToString & " Build: " & My.Application.Info.Version.Revision.ToString
End Sub
Private Sub KeyFileGenerator_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
ProcId_1.Focus()
End Sub
Private Sub ProcId_1_TextChanged(sender As Object, e As EventArgs) Handles ProcId_1.TextChanged
If ProcId_1.TextLength = 4 Then
ProcId_2.Focus()
RefreshKeyFileData()
Else
DisableKey()
End If
End Sub
Private Sub ProcId_2_TextChanged(sender As Object, e As EventArgs) Handles ProcId_2.TextChanged
If ProcId_2.TextLength = 4 Then
ProcId_3.Focus()
RefreshKeyFileData()
ElseIf ProcId_2.TextLength = 0 Then
ProcId_1.Focus()
Else
DisableKey()
End If
End Sub
Private Sub ProcId_3_TextChanged(sender As Object, e As EventArgs) Handles ProcId_3.TextChanged
If ProcId_3.TextLength = 4 Then
ProcId_4.Focus()
RefreshKeyFileData()
ElseIf ProcId_3.TextLength = 0 Then
ProcId_2.Focus()
Else
DisableKey()
End If
End Sub
Private Sub ProcId_4_TextChanged(sender As Object, e As EventArgs) Handles ProcId_4.TextChanged
If ProcId_4.TextLength = 4 Then
RefreshKeyFileData()
ElseIf ProcId_4.TextLength = 0 Then
ProcId_3.Focus()
Else
DisableKey()
End If
End Sub
Private Sub RefreshKeyFileData()
If ProcId_1.TextLength = 4 AndAlso ProcId_2.TextLength = 4 AndAlso ProcId_3.TextLength = 4 AndAlso ProcId_4.TextLength = 4 Then
ProcId = ProcId_1.Text & ProcId_2.Text & ProcId_3.Text & ProcId_4.Text
If ProcId.Length = 16 Then
If CheckSum(ProcId.Substring(0, 14)) = ProcId.Substring(14, 2) Then
Label5.Text = "Checksum Oke"
BuildKeyFileData()
CreateKeyButton.Enabled = True
ClientName.Focus()
Else
Label5.Text = "Checksum Fout"
End If
Else
Label5.Text = "Key Fout"
End If
End If
End Sub
Private Sub DisableKey()
Label5.Text = "Geen volledige key"
CreateKeyButton.Enabled = False
ProcId = String.Empty
BuildKeyFileData()
End Sub
Private Function CheckSum(ByVal KeyString As String) As String
Dim xorvalue As Byte
For Each c As Char In KeyString
xorvalue = xorvalue Xor Convert.ToByte(c)
Next
Return xorvalue.ToString("X2")
End Function
Sub BuildKeyFileData()
Dim Keydata = _
<UCS_Registration>
<Client>
<Name><%= ClientName.Text %></Name>
<Regkey><%= ProcId %></Regkey>
</Client>
<Layout_Options>
<Buttoncount><%= ButtonCount.Text %></Buttoncount>
<Groupboxcount><%= GroupBoxCount.Text %></Groupboxcount>
</Layout_Options>
<UCS_Options>
<UDP><%= Option_UDP.Checked %></UDP>
<Telnet><%= Option_Telnet.Checked %></Telnet>
<Video><%= Option_Video.Checked %></Video>
<C2000><%= Option_C2000.Checked %></C2000>
<Voip><%= Option_voip.Checked %></Voip>
</UCS_Options>
<%= AddUDPOptions() %>
<%= AddTelnetOptions() %>
<%= AddVideoOptions() %>
<%= AddC2000Options() %>
<%= AddVoipOptions() %>
</UCS_Registration>
KeyFileData.Text = Keydata.ToString
'Dim fs As New IO.StreamWriter(part.GetStream, New System.Text.UTF8Encoding)
'Dim xmlWriter As New Xml.XmlTextWriter(part.GetStream, New UTF8Encoding)
'xmlWriter.Formatting = Xml.Formatting.Indented
'Dim enc As New UTF8Encoding
'xmlWriter.WriteStartDocument()
'x.WriteTo(xmlWriter)
'xmlWriter.WriteEndDocument()
'xmlWriter.Flush()
'xmlWriter.Close()
End Sub
Private Function AddUDPOptions() As XElement
If Option_UDP.Checked = True Then
Dim UDPData = _
<UDP_Options>
<IDCount><%= UdpIdCount.Text %></IDCount>
</UDP_Options>
Return UDPData
End If
Return Nothing
End Function
Private Function AddTelnetOptions() As XElement
If Option_Telnet.Checked = True Then
Dim TelnetData = _
<Telnet_Options>
<IPCount><%= TelnetIpCount.Text %></IPCount>
</Telnet_Options>
Return TelnetData
End If
Return Nothing
End Function
Private Function AddVideoOptions() As XElement
If Option_Video.Checked = True Then
Dim VideoData = _
<Video_Options>
<VideoCount><%= VideoBoxCount.Text %></VideoCount>
</Video_Options>
Return VideoData
End If
Return Nothing
End Function
Private Function AddC2000Options() As XElement
If Option_C2000.Checked = True Then
Dim C2000Data = _
<C2000_Options>
<ISSICount><%= C2000IssiCount.Text %></ISSICount>
</C2000_Options>
Return C2000Data
End If
Return Nothing
End Function
Private Function AddVoipOptions() As XElement
If Option_voip.Checked = True Then
Dim VoipData = _
<Voip_Options>
<CallNumberCount><%= VoipCallNumberCount.Text %></CallNumberCount>
</Voip_Options>
Return VoipData
End If
Return Nothing
End Function
Dim EncryptionKey As String = "UX8Zmc2mjhzySAFvkDsHjwAz441Mn2jSkxljDYpp9Xq9uCySPcezYtqzKebbIUu"
'Dim saltArray As Byte() = Encoding.ASCII.GetBytes("this is my salt")
Private Function Encrypt(clearText As String) As String
Try
Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
Using encryptor As Aes = Aes.Create()
encryptor.Padding = PaddingMode.PKCS7
Dim saltArray As Byte() = Encoding.ASCII.GetBytes(ProcId)
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, saltArray)
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using ms As New MemoryStream()
Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
cs.Write(clearBytes, 0, clearBytes.Length)
cs.Close()
End Using
clearText = Convert.ToBase64String(ms.ToArray())
End Using
End Using
Return clearText
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return Nothing
End Function
Private Function Decrypt(cipherText As String) As String
Try
Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
Using encryptor As Aes = Aes.Create()
encryptor.Padding = PaddingMode.PKCS7
Dim saltArray As Byte() = Encoding.ASCII.GetBytes(ProcId)
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, saltArray)
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using ms As New MemoryStream()
Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)
cs.Write(cipherBytes, 0, cipherBytes.Length)
cs.Close()
End Using
cipherText = Encoding.Unicode.GetString(ms.ToArray())
End Using
End Using
Return cipherText
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return Nothing
End Function
Private Sub CreateKeyButton_Click(sender As Object, e As EventArgs) Handles CreateKeyButton.Click
If KeyFileData.Text.Length > 10 Then
RichTextBox1.Text = Encrypt(KeyFileData.Text)
End If
End Sub
Private Sub DecryptButton_Click(sender As Object, e As EventArgs) Handles DecryptButton.Click
If DataToDecryptTextbox.Text.Length > 10 Then
RichTextBox2Decrypt.Text = Decrypt(DataToDecryptTextbox.Text)
End If
End Sub
Private Sub ClientName_TextChanged(sender As Object, e As EventArgs) Handles ClientName.TextChanged
BuildKeyFileData()
End Sub
Private Sub Option_CheckedChanged(sender As Object, e As EventArgs) Handles Option_UDP.CheckedChanged, Option_Telnet.CheckedChanged, Option_Video.CheckedChanged, Option_C2000.CheckedChanged, Option_voip.CheckedChanged
BuildKeyFileData()
End Sub
Private Sub DataToDecryptTextbox_TextChanged(sender As Object, e As EventArgs) Handles DataToDecryptTextbox.TextChanged
If DataToDecryptTextbox.Text.Length > 10 Then
DecryptButton.Enabled = True
End If
End Sub
Private Sub LayoutCount_KeyPress(sender As Object, e As KeyPressEventArgs) Handles GroupBoxCount.KeyPress, ButtonCount.KeyPress, TelnetIpCount.KeyPress, VideoBoxCount.KeyPress, C2000IssiCount.KeyPress, VoipCallNumberCount.KeyPress, UdpIdCount.KeyPress
If e.KeyChar = ChrW(Keys.Back) Then
'Niks doen bij een backspace
Else
Dim TextBoxKeyPress As TextBox
TextBoxKeyPress = DirectCast(sender, TextBox)
'Alleen numnerieke waarde in de textbox
If IsNumeric(TextBoxKeyPress.Text + e.KeyChar) = False Then e.Handled = True
End If
End Sub
Private Sub LayoutCount_TextChanged(sender As Object, e As EventArgs) Handles ButtonCount.TextChanged, GroupBoxCount.TextChanged, TelnetIpCount.TextChanged, VideoBoxCount.TextChanged, C2000IssiCount.TextChanged, VoipCallNumberCount.TextChanged, UdpIdCount.TextChanged
BuildKeyFileData()
End Sub
End Class