Problems with encryption and decryption TripleDES in vb.net - vb.net

I'm practicing encryption and decryption with a text file by using TipleDES. I enter a word in a text box, which is in my application, and then click my button that will store the password in the text file. I check the text file to see if it encrypts it which it does and then I try to decrypt the text. It encrypts it again and doesn't decrypt it. I'm not sure what I'm doing wrong.
Public Function EncryptTripleDES(strInput As String, btKey As String) As String
Dim desTDES As New TripleDESCryptoServiceProvider()
Dim hashMD5TDES As New MD5CryptoServiceProvider()
Dim btHash As Byte()
Dim btBuff As Byte()
btHash = hashMD5TDES.ComputeHash(Encoding.UTF8.GetBytes(btKey))
desTDES.Key = btHash
desTDES.Mode = CipherMode.ECB
btBuff = Encoding.UTF8.GetBytes(strInput)
Dim strResult As String = Convert.ToBase64String _
(desTDES.CreateEncryptor().TransformFinalBlock(btBuff, 0,
btBuff.Length))
Return strResult
End Function
Public Function DecryptTripleDES(strInput As String, btKey As String) As String
Dim desTDES As New TripleDESCryptoServiceProvider()
Dim hashMD5TDES As New MD5CryptoServiceProvider()
Dim btHash As Byte()
Dim btBuff As Byte()
btHash = hashMD5TDES.ComputeHash(Encoding.UTF8.GetBytes(btKey))
desTDES.Key = btHash
desTDES.Mode = CipherMode.ECB
btBuff = Convert.FromBase64String(strInput)
Dim strResult As String = Encoding.UTF8.GetString _
(desTDES.CreateDecryptor().TransformFinalBlock(btBuff, 0,
btBuff.Length))
Return strResult
End Function
These are my functions to encrypt and decrypt.
strEncode = EncryptTripleDES(EmailPasswordTextBox.Text, "HTG")
strDecode = DecryptTripleDES(strEncode, "HTG")
Console.WriteLine("Encrypted: {0}", strEncode)
Console.WriteLine("Decrypted: {0}", strDecode)
If I change EmailPasswordTextBox.Text to "This is a Test". It works perfectly. What am I doing wrong?

Related

Padding Is invalid - rijndael encryption method

I've been trying to encrypt the connection string so I don't write it anywhere.
Using ChatGPT I came across the Rijndael method.
Module:
Imports System.Security.Cryptography
Imports System.Text
Module DB
' Encrypt the connection string
Public Function EncryptConnectionString(connectionString As String, key As Byte(), iv As Byte()) As String
' Create a new Rijndael instance
Dim rijndael As New RijndaelManaged()
rijndael.Padding = PaddingMode.PKCS7
rijndael.Key = key
rijndael.IV = iv
' Convert the connection string to a byte array
Dim data As Byte() = Encoding.UTF8.GetBytes(connectionString)
' Encrypt the data using the Rijndael instance
Dim encryptedData As Byte() = rijndael.CreateEncryptor().TransformFinalBlock(data, 0, data.Length)
' Concatenate the key, the initialization vector, and the encrypted data
Dim encryptedConnectionString As Byte() = New Byte(rijndael.Key.Length + rijndael.IV.Length + encryptedData.Length - 1) {}
Array.Copy(rijndael.Key, encryptedConnectionString, rijndael.Key.Length)
Array.Copy(rijndael.IV, 0, encryptedConnectionString, rijndael.Key.Length, rijndael.IV.Length)
Array.Copy(encryptedData, 0, encryptedConnectionString, rijndael.Key.Length + rijndael.IV.Length, encryptedData.Length)
' Convert the encrypted connection string to a base64 string
Return Convert.ToBase64String(encryptedConnectionString)
End Function
' Decrypt the connection string
Public Function DecryptConnectionString(encryptedConnectionString As String, key As Byte(), iv As Byte()) As String
' Convert the encrypted connection string from a base64 string
Dim encryptedConnectionBytes As Byte() = Convert.FromBase64String(encryptedConnectionString)
' Extract the key and initialization vector from the encrypted connection string
Dim decryptedKey As Byte() = New Byte(key.Length - 1) {}
Dim decryptedIV As Byte() = New Byte(iv.Length - 1) {}
Array.Copy(encryptedConnectionBytes, decryptedKey, key.Length)
Array.Copy(encryptedConnectionBytes, key.Length, decryptedIV, 0, iv.Length)
'Create a New Rijndael instance
Dim rijndael As New RijndaelManaged()
rijndael.Padding = PaddingMode.PKCS7
rijndael.Key = decryptedKey
rijndael.IV = decryptedIV
' Extract the encrypted data from the encrypted connection string
Dim encryptedData As Byte() = New Byte(encryptedConnectionBytes.Length - key.Length - iv.Length - 1) {}
Array.Copy(encryptedConnectionBytes, key.Length + iv.Length, encryptedData, 0, encryptedData.Length)
' Decrypt the data using the Rijndael instance
Dim decryptedData As Byte() = rijndael.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length)
' Convert the decrypted data to a string and return it
Return Encoding.UTF8.GetString(decryptedData)
End Function
End Module`
Form to test:
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Private Sub BtnEncrypt_Click(sender As Object, e As EventArgs) Handles btnEncrypt.Click
' Generate a new key and initialization vector
Dim key As Byte() = New Byte(31) {}
Dim iv As Byte() = New Byte(15) {}
Using rng As New RNGCryptoServiceProvider()
rng.GetBytes(key)
rng.GetBytes(iv)
End Using
' Encrypt the connection string
Dim connectionString As String = "Server=" & txtIP.Text & ";Database=" & txtDatabase.Text & ";User ID=" & txtUsername.Text & ";Password=" & txtPassword.Text & ";port=" & txtPort.Text & ";"
Dim rijndael As New RijndaelManaged()
rijndael.Padding = PaddingMode.PKCS7
rijndael.Key = key
rijndael.IV = iv
Dim data As Byte() = Encoding.UTF8.GetBytes(connectionString)
Dim encryptedData As Byte() = rijndael.CreateEncryptor().TransformFinalBlock(data, 0, data.Length)
Dim encryptedConnectionString As String = Convert.ToBase64String(encryptedData)
'Save the encrypted connection string And the key And IV to the settings
My.Settings.EncryptedConnectionString = encryptedConnectionString
My.Settings.Key = Convert.ToBase64String(key)
My.Settings.IV = Convert.ToBase64String(iv)
My.Settings.Save()
txtconnectionstring.Text = encryptedConnectionString
txtKey.Text = Convert.ToBase64String(key)
txtIV.Text = Convert.ToBase64String(iv)
End Sub
Private Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click
' Retrieve the encrypted connection string, key, and IV from your settings or configuration
Dim encryptedConnectionString As String = My.Settings.EncryptedConnectionString
Dim key As Byte() = Convert.FromBase64String(txtKey.Text)
Dim iv As Byte() = Convert.FromBase64String(txtIV.Text)
' Decrypt the connection string using the key and IV
Dim connectionString As String = DecryptConnectionString(encryptedConnectionString, key, iv)
txtconnectionstring.Clear()
txtconnectionstring.Text = connectionString
End Sub
End Class
So Encryption is working good but decrypt is giving me hell. Error is: Padding is invalid and cannot be removed.
System.Security.Cryptography.CryptographicException
HResult=0x80131430
Message=Padding is invalid and cannot be removed.
Source=mscorlib
StackTrace:
at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
For a little bit more context because i've read that the IV or KEY neeed to be the same. I saved those keys manual on the settings or i've saved it on the text box. I've always got the same error while decrypting.
If anyone could help,
Fábio
Tried to encrypt my connection string and save it to the settings of the app. Now I was trying to decrypt it so it could be used to connection to the mysql server.

VB NET AES encrypt

Can't reproduce an AES online encoder example using VB.Net
Trying in https://www.devglan.com/online-tools/aes-encryption-decryption with following parameters:
Text to be Encrypted: test
Cipher Mode: ECB
Key Size: 128
Secret Key: 1234567890123456
I get this output: 3fvaLg5IDlveswuXzhVQcw==
If I try in VB.Net using this function (found in https://gist.github.com/ShaneGowland/5973974):
Public Shared Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
Return ex.ToString
End Try
End Function
I get this output: 6mhZOr1dQ7PWqbRGzmMgjg== which is not matching with got at devglan.com
I tried with different paddings with no luck. What am I doing wrong?
PS: I am aware that the ECB method should not be used
Working with this function:
Public Shared Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
AES.Mode = CipherMode.ECB
AES.Key = Encoding.UTF8.GetBytes(pass)
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
Return ex.ToString
End Try
End Function

VB.Net Encryption Function Not Decrypting Without Using Base64Encode

I am having a issue with decrypting, my goal is to be able to encrypt/decrypt with/without base64 encoding on the encrypted string. As of now I can encrypt/decrypt with base64 and encrypt without it but not decrypt without it. I get errors regarding the padding being incorrect.
Thanks in advance!
Here is my encryption/decryption function:
Public Function DoCryptWork(Type As String, Data As String) As String
Dim Pass As String = Hasher.TextBoxPassword.Text
Dim Salt As String = Hasher.TextBoxSalt.Text
Dim Vect As String = Hasher.TextBoxIntVector.Text
Select Case Type
Case "e"
Try
Dim PassPhrase As String = Pass
Dim SaltValue As String = Salt
Dim HashAlgorithm As String = My.Settings.HashAlgorithm
Dim PasswordIterations As Integer = 2
Dim InitVector As String = Vect
Dim KeySize As Integer = 256
Dim InitVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue)
Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(Data)
Dim Password As New PasswordDeriveBytes(PassPhrase, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = Password.GetBytes(KeySize \ 8)
Dim SymmetricKey As New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitVectorBytes)
Dim MemoryStream As New MemoryStream()
Dim CryptoStream As New CryptoStream(MemoryStream, Encryptor, CryptoStreamMode.Write)
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
CryptoStream.FlushFinalBlock()
Dim CipherTextBytes As Byte() = MemoryStream.ToArray()
MemoryStream.Close()
CryptoStream.Close()
Dim CipherText As String = Nothing
If My.Settings.Base64EncodeMD5Hash = True Then
CipherText = Convert.ToBase64String(CipherTextBytes)
Return CipherText
Else
Dim TextCipher As New StringBuilder()
For n As Integer = 0 To CipherTextBytes.Length - 1
TextCipher.Append(CipherTextBytes(n).ToString("X2"))
Next n
CipherText = TextCipher.ToString()
Return CipherText
End If
Catch ex As Exception
MsgBox("Encryption was unsuccessfull!", MsgBoxStyle.Critical, "Error")
Return "Encryption was unsuccessfull!"
End Try
Case "d"
Try
Dim PassPhrase As String = Pass
Dim SaltValue As String = Salt
Dim HashAlgorithm As String = My.Settings.HashAlgorithm
Dim PasswordIterations As Integer = 2
Dim InitVector As String = Vect
Dim KeySize As Integer = 256
Dim InitVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue)
Dim CipherTextBytes As Byte() = Nothing
If My.Settings.Base64EncodeMD5Hash = True Then
CipherTextBytes = Convert.FromBase64String(Data)
Else
Dim bytedata As Byte() = Encoding.UTF8.GetBytes(Data)
CipherTextBytes = bytedata
End If
Dim Password As New PasswordDeriveBytes(PassPhrase, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = Password.GetBytes(KeySize \ 8)
Dim SymmetricKey As New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitVectorBytes)
Dim MemoryStream As New MemoryStream(CipherTextBytes)
Dim CryptoStream As New CryptoStream(MemoryStream, Decryptor, CryptoStreamMode.Read)
Dim PlainTextBytes As Byte() = New Byte(CipherTextBytes.Length - 1) {}
Dim DecryptedByteCount As Integer = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length)
MemoryStream.Close()
CryptoStream.Close()
Dim PlainText As String = Encoding.UTF8.GetString(PlainTextBytes, 0, DecryptedByteCount)
Return PlainText
Catch Ex As Exception
MsgBox("Decryption was unsuccessfull!" & vbNewLine & vbNewLine & Ex.ToString(), MsgBoxStyle.Critical, "Error")
Return "Decryption was unsuccessfull!"
End Try
Case Else
Return "Error! Invalid Case Selected We should never see this but just to be safe we'll show this message if the wrong case is selected!"
End Select
Return True
End Function
I call the function as so:
TextBoxOutput.Text = Encryption.DoCryptWork("e", TextBoxInput.Text) ' encrypt data.
TextBoxOutput.Text = Encryption.DoCryptWork("d", TextBoxInput.Text) ' decrypt data.
When you convert the bytes to hex, you output two hex digits per byte. When you convert that hex back to bytes, you're converting every hex digit to a byte instead of every pair of hex digits.
Actually, I just took another look and noticed that you're not even keeping the earlier bytes. This loop:
For n As Integer = 0 To Data.Length - 1
CipherTextBytes = Convert.ToByte(Data(n))
Next n
sets CipherTextBytes on each iteration so you're going to replace the previous byte each time, so you only end up keeping the byte from the last digit.

vb.net rijndael limit to 64 character

i have problem while encrypt and decrypt using rijndael in vb.net.
it cannot work on string more than 64 character.
here is my code:
Private Function prepareRijn() As Rijndael
Dim KEY As String = Left(_KEY, 32)
Dim IV As String = Right(_KEY, 32)
Dim enc As New System.Text.UTF8Encoding
Dim byteKEY() As Byte = enc.GetBytes(KEY)
Dim byteIV() As Byte = enc.GetBytes(IV)
Dim alg As Rijndael = Rijndael.Create
alg.BlockSize = 256
alg.KeySize = 256
alg.Padding = PaddingMode.Zeros
alg.Mode = CipherMode.CBC
alg.Key = byteKEY
alg.IV = byteIV
Return alg
End Function
Function decrypt(ByVal encrypted As String) As String
encrypted = encrypted.Replace("Q2FrZQ==.", "")
Dim enc As New System.Text.UTF8Encoding
Dim alg As Rijndael = prepareRijn()
Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, alg.CreateDecryptor, CryptoStreamMode.Write)
Dim data() As Byte = Convert.FromBase64String(encrypted)
cs.Write(data, 0, data.Length)
'ms.SetLength(data.Length)
Dim decrypted() As Byte
decrypted = ms.ToArray
cs.Close()
Return enc.GetString(decrypted)
End Function
Function encrypt(ByVal decrypt As String) As String
decrypt = decrypt + " "
Dim alg As Rijndael = prepareRijn()
Dim ms As New MemoryStream()
Dim cs As CryptoStream = New CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write)
Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(decrypt)
cs.Write(data, 0, data.Length)
'ms.SetLength(data.Length)
Dim encrypted() As Byte = ms.ToArray()
cs.Close()
Return Convert.ToBase64String(encrypted)
End Function
is there anything i miss during my rijndael configuration ?
I wrote complete Encryption Class for you. It works perfectly. It can be used for Both Strings and ByteArrays. This Class returns Encrypted Data in Base64, if you dont want Base64, just remove Conversion.
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Imports System
Public Class Encryption
' Fields
Private Shared sIV As String = "Your IV Key Placed Here 32-Bytes"
Private Shared sKey As String = "Your SecKey Placed Here 32-Bytes"
' Methods
Public Shared Function DecryptRJ256(ByVal prm_text_to_decrypt As String) As String
Dim s As String = prm_text_to_decrypt
Dim managed As New RijndaelManaged With { _
.Padding = PaddingMode.Zeros, _
.Mode = CipherMode.ECB, _
.KeySize = &H100, _
.BlockSize = &H100 _
}
Dim rgbKey As Byte() = Nothing
Dim rgbIV As Byte() = Nothing
s = s.Replace("-", "+").Replace("_", "/").Replace("|", "=")
rgbKey = Encoding.ASCII.GetBytes(Encryption.sKey)
rgbIV = Encoding.ASCII.GetBytes(Encryption.sIV)
Dim transform As ICryptoTransform = managed.CreateDecryptor(rgbKey, rgbIV)
Dim buffer As Byte() = Convert.FromBase64String(s)
Dim buffer4 As Byte() = New Byte((buffer.Length + 1) - 1) {}
Dim stream As New MemoryStream(buffer)
Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Read)
stream2.Read(buffer4, 0, buffer4.Length)
Return Encoding.ASCII.GetString(buffer4)
End Function
Public Shared Function EncryptRJ256(ByVal prm_text_to_encrypt As String) As String
Dim s As String = prm_text_to_encrypt
Dim managed As New RijndaelManaged With { _
.Padding = PaddingMode.Zeros, _
.Mode = CipherMode.ECB, _
.KeySize = &H100, _
.BlockSize = &H100 _
}
Dim buffer As Byte() = Nothing
Dim rgbKey As Byte() = Nothing
Dim rgbIV As Byte() = Nothing
rgbKey = Encoding.ASCII.GetBytes(Encryption.sKey)
rgbIV = Encoding.ASCII.GetBytes(Encryption.sIV)
Dim transform As ICryptoTransform = managed.CreateEncryptor(rgbKey, rgbIV)
Dim stream As New MemoryStream
Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Write)
buffer = Encoding.ASCII.GetBytes(s)
stream2.Write(buffer, 0, buffer.Length)
stream2.FlushFinalBlock()
Return Convert.ToBase64String(stream.ToArray).Replace("+", "-").Replace("/", "_").Replace("=", "|")
End Function
Public Shared Function EncryptRJ256(ByVal ArrayByte As Byte()) As String
Dim managed As New RijndaelManaged With { _
.Padding = PaddingMode.Zeros, _
.Mode = CipherMode.ECB, _
.KeySize = &H100, _
.BlockSize = &H100 _
}
Dim rgbKey As Byte() = Nothing
Dim rgbIV As Byte() = Nothing
rgbKey = Encoding.ASCII.GetBytes(Encryption.sKey)
rgbIV = Encoding.ASCII.GetBytes(Encryption.sIV)
Dim transform As ICryptoTransform = managed.CreateEncryptor(rgbKey, rgbIV)
Dim stream As New MemoryStream
Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Write)
stream2.Write(ArrayByte, 0, ArrayByte.Length)
stream2.FlushFinalBlock()
Return Convert.ToBase64String(stream.ToArray).Replace("+", "-").Replace("/", "_").Replace("=", "|")
End Function
Public Shared Function getMD5Hash(ByVal input As String) As String
Dim md As MD5 = MD5.Create
Dim bytes As Byte() = Encoding.ASCII.GetBytes(input)
Dim buffer2 As Byte() = md.ComputeHash(bytes)
Dim builder As New StringBuilder
Dim i As Integer
For i = 0 To buffer2.Length - 1
builder.Append(buffer2(i).ToString("X2"))
Next i
Return builder.ToString
End Function
Public Shared Function FromBase64String(ByVal prm_text_to_decrypt As String) As String
Dim s As String = prm_text_to_decrypt
s = s.Replace("-", "+").Replace("_", "/").Replace("|", "=")
Dim buffer As Byte() = Convert.FromBase64String(s)
Return Encoding.ASCII.GetString(buffer)
End Function
End Class

Shorten a string containing data

I am creating an application to create a key that is unique to each computer. This info is derived from the OS serial number and processorID.
Is there a way to 'shorten' a string? Maybe by converting it to HEX or something else...
The reason is this: I used to use a VB6 section of code (http://www.planet-source-code.com/vb...48926&lngWId=1) that gets the details and the output is only 13 digits long. Mine is a lot longer, but gets the same info...
BTW, the link I posted above won multiple awards, but I am having huge trouble in converting it to .NET. Has anyone by any chance converted it, or know of someone who has? Or a tool that actually works?
Thanks
EDIT
Here is the full working link: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=48926&lngWId=1
It sounds like you want a 'hashing algorithm' or 'hash function'. They are a common concept: http://en.wikipedia.org/wiki/Hash_function
Generally speaking you can simply write your own function to take a string and return a hashed number but there is some suitable code here that uses the .NET framework: http://support.microsoft.com/kb/301053
Here is a working example which retrieves the Processor ID, for the first Processor found, and the OS Serial Number; it concatenates these to strings together and then performs various encodings on them.
This is a simple VB.Net Console project. Be sure to reference the System.Management assembly in your project. Just copy and paste this code example into the main module, set a breakpoint at the end of Sub Main(), and look at the various results.
Module Module1
Sub Main()
Dim uniqueID As String = GetUniqueID()
' convert it to a base64 string
Dim encoding As New Text.ASCIIEncoding()
Dim result1 = Convert.ToBase64String(encoding.GetBytes(uniqueID))
' compress it
Dim result2 As String = CompressString(uniqueID)
Dim result3 As String = DecompressString(result2)
' encrypt it
Dim result4 As String = AES_Encrypt(uniqueID, "password")
Dim result5 As String = AES_Decrypt(result4, "password")
' hash it
Dim result6 As String = HashString(uniqueID)
End Sub
Private Function GetUniqueID() As String
Dim result As String = GetOSSerialNumber()
Dim processorIDs() As String = GetProcessorIDs()
If ((processorIDs IsNot Nothing) AndAlso (processorIDs.Count > 0)) Then
result &= processorIDs(0)
End If
Return result
End Function
Private Function GetProcessorIDs() As String()
Dim result As New List(Of String)
Dim searcher = New System.Management.ManagementObjectSearcher("Select ProcessorId from Win32_Processor")
For Each managementObj In searcher.Get()
result.Add(CStr(managementObj.Properties("ProcessorId").Value))
Next
Return result.ToArray()
End Function
Private Function GetOSSerialNumber() As String
Dim result As String = ""
Dim searcher = New System.Management.ManagementObjectSearcher("Select SerialNumber from Win32_OperatingSystem")
For Each managementObj In searcher.Get()
result = CStr(managementObj.Properties("SerialNumber").Value)
Next
Return result
End Function
Public Function CompressString(ByVal source As String) As String
Dim result As String = ""
Dim encoding As New Text.ASCIIEncoding()
Dim bytes() As Byte = encoding.GetBytes(source)
Using ms As New IO.MemoryStream
Using gzsw As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Compress)
gzsw.Write(bytes, 0, bytes.Length)
gzsw.Close()
result = Convert.ToBase64String(ms.ToArray)
End Using
End Using
Return result
End Function
Public Function DecompressString(ByVal source As String) As String
Dim result As String = ""
Dim bytes() As Byte = Convert.FromBase64String(source)
Using ms As New IO.MemoryStream(bytes)
Using gzsw As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress)
Dim data(CInt(ms.Length)) As Byte
gzsw.Read(data, 0, CInt(ms.Length))
Dim encoding As New Text.ASCIIEncoding()
result = encoding.GetString(data)
End Using
End Using
Return result
End Function
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
End Try
Return encrypted
End Function
Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim decrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(input)
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
End Try
Return decrypted
End Function
Private Function HashString(ByVal source As String) As String
Dim encoding As New Text.ASCIIEncoding()
Dim bytes() As Byte = encoding.GetBytes(source)
Dim workingHash() As Byte = New System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(bytes)
Dim result As String = ""
For Each b In workingHash
result = result & b.ToString("X2")
Next
Return result
End Function
End Module