encrypt a string using SHA512 - vb.net

Here 's my code and I have no idea why it produces a weird code in the console
(output is "b5?2???p?????'5???.?H???Kun???a??\??d??+\??%??A)?_???j?" without the quotes)
Private Sub TestSHA512()
Dim key As String = "635357773463315343"
Dim pass As String = "somepasswd"
Dim enc As System.Text.Encoding = New System.Text.ASCIIEncoding
Dim keyBytes() As Byte = enc.GetBytes(key)
Dim passBytes() As Byte = enc.GetBytes(pass)
Dim SHA As New HMACSHA512(keyBytes)
Dim resultBytes() As Byte = SHA.ComputeHash(passBytes)
Console.WriteLine(enc.GetString(resultBytes))
Console.WriteLine(enc.GetString(SHA.Hash)) 'same...
End Sub

First, SHA512 is a hash algorithm, not an encryption scheme, so if you're trying to encrypt then SHA512 isn't the way to do it. You'd need to look at an encryption class, such as AesManaged.
ComputeHash gives you the computed hash as a byte array. You're using ASCIIEncoding.GetString to convert that into a string, but not every byte is a printable ASCII character. That's why you're seeing the ??? characters in your console output.
If you're asking how to display the hash output as a printable string, use Convert.ToBase64String, which will convert the byte array into a string using base64 encoding. If you were expecting it in hexadecimal, you can loop through the byte array and print the Hex() value of each byte.

Related

ASCII to Base32

I am working at making what 10 characters go into a text box in my vb project convert into Base32. Here is my code. I am getting an error
Value of type 'String' cannot be converted to 'Byte()'. WindowsApplication2
Private Sub Ok_Click(sender As Object, e As EventArgs) Handles Ok.Click
Dim DataToEncode As Byte() = txtbox.Text
Dim Base32 As String
Base32 = DataToEncode.ToBase32String()
Auth.Text = Base32
End Sub
The value in txtbox.Text is a string which can't be automatically converted to a byte array. So the line Dim DataToEncode As Byte() = txtbox.Text can't be compiled. To get the ASCII representation of a string use the System.Text.Encoding.ASCII.GetBytes() method.
Dim DataToEncode As Byte() = System.Text.Encoding.ASCII.GetBytes(txtbox.Text)
Also strings in VB.Net do not store ASCII values, they use UTF-16.
As the error indicates, you're trying to take a string (the context of txtbox.Text) and put it in a variable of type Byte(), an array of bytes. A string isn't a byte array, it's a logical sequence of characters that can have different representation in bytes - do you want to treat it as a UTF-8-encoded string? An ASCII string? A full-blown UTF-32 string? All these are different byte representations of what might be the same textual data.
Once you know the representation you care about, use the System.Text.Encoding classes to convert the text to a Byte() and pass that to your method.
Try converting the string into a byte array using the GetBytes method:
Dim DataToEncode As Byte() = Encoding.UTF8.GetBytes(txtbox.Text)

Read Data From The Byte Array Returned From Web Service

I have a web service,which return data in byte array.Now i want to read that data in my console project.How can i do that,i already add the desire references to access that web service.I am using vb.net VS2012.Thanks.My web service method is as follow.
Public Function GetFile() As Byte()
Dim response As Byte()
Dim filePath As String = "D:\file.txt"
response = File.ReadAllBytes(filePath)
Return response
End Function
Something like,
Dim result As String
Using (Dim data As New MemoryStream(response))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
if you knew the encoding, lets say it was UTF-8 you could do,
Dim result = System.Text.UTF8Encoding.GetString(response)
Following on from your comments, I think you are asserting this.
Dim response As Byte() 'Is the bytes of a Base64 encoded string.
So, we know all the bytes will be valid ASCII (because its Base64,) so the string encoding is interchangable.
Dim base64Encoded As String = System.Text.UTF8Encoding.GetString(response)
Now, base64Encoded is the string Base64 representation of some binary.
Dim decodedBinary As Byte() = Convert.FromBase64String(base64Encoded)
So, we've changed the encoded base64 into the binary it represents. Now, because I can see that in your example, you are reading a file called "D:/file.txt" I'm going to make the assumption that the contents of the file is a character encoded string, but I don't know the encoding of the string. The StreamReader class has some logic in the constructor that can make an educated guess at character encoding.
Dim result As String
Using (Dim data As New MemoryStream(decodedBinary))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
Hopefully, now result contains the context of the text file.

VB .NET - Length of the data to decrypt is invalid

I'm trying to do an exercise about encryption and decryption. I already have the following working code to encrypt (I know that ECB is bad, I won't use it in real life, I promise):
Dim hashmd5 As MD5CryptoServiceProvider
Dim des As TripleDESCryptoServiceProvider
Dim keyhash As Byte()
Dim buff As Byte()
Try
hashmd5 = New MD5CryptoServiceProvider
keyhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("exe67rci89"))
des = New TripleDESCryptoServiceProvider
des.Mode = CipherMode.ECB
des.Key = keyhash
buff = ASCIIEncoding.ASCII.GetBytes(Me.txtPwd.Text)
Me.txtPwd.Text = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length))
Catch ex As System.Exception
Throw ex
End Try
Now I'm trying to decrypt the output of this function. I tried to use the same code, just substituting the encryption line with:
Me.txtPwd.Text = Convert.ToBase64String(des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length))
But it doesn't work, because I receive the error "Length of the data to decrypt is invalid". I already tried many solutions found here on stack overflow, but no one works. Can someone help me to solve this problem? Thanks in advance!
You're not reversing enough of the transformation. What's now in txtPwd is a base-64 string, not a plain string to be converted using the ASCII encoding. And conversely, what you're going to produce in decryption are bytes that should be interpreted as ASCII characters.
So what you want is:
buff = Convert.FromBase64String(Me.txtPwd.Text)
Me.txtPwd.Text = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length))
What you're trying to decrypted isn't exactly the same as what came out of the actual encrypt:
In your encrypt you convert the encrypted bytes to a base 64 string using Convert.ToBase64String
You need to convert the string in the txtPwd field back into the byte array that was the result of the original encryption. See the matching Convert.FromBase64String method.
You might be able to view what I mean better if you split the original encrypt line up:
dim encBytes = des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length)
Me.txtPwd.Text = Convert.ToBase64String(encBytes)
This way you can see the actual encrypt results, and validate that what you pass in is the same.

Converting UTF-8 to windows-1255 encoding in VB.NET

I am trying to convert a string encoded in UTF-8 to windows-1255 in VB.NET with no luck. Admittedly, I don't know VB but have tried using an example at MSDN and modifying it to my needs:
Public Function Utf82Hebrew(ByVal Str As String) As String
Dim ascii As Encoding = Encoding.GetEncoding("windows-1255")
Dim unicode As Encoding = Encoding.Unicode
' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(Str)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)
' Convert the new byte array into a char array and then into a string.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
Utf82Hebrew = asciiString
End Function
This function doesn't actually do anything—the string remains in UTF-8. However, if I change this line:
Dim ascii As Encoding = Encoding.GetEncoding("windows-1255")
To this:
Dim ascii As Encoding = Encoding.ASCII
Then the function returns question marks in the place of the string.
Does anyone know how to properly convert a UTF-8 string to a specific encoding (in this case, windows-1255), and/or what I'm doing wrong in the above code?
Thanks in advance.
I modified your code.
It is very straightforward to convert text from one encoding into another.
This is how you should do it in VB.Net.
Microsof Windows file encoding is 1252, not 1255.
Public Function Utf82Hebrew(ByVal Str As String) As String
Dim ascii As System.Text.Encoding = System.Text.Encoding.GetEncoding("1252")
Dim unicode As System.Text.Encoding = System.Text.Encoding.Unicode
' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(Str)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = System.Text.Encoding.Convert(unicode, ascii, unicodeBytes)
' Convert the new byte array into a char array and then into a string.
Dim asciiString As String = ascii.GetString(asciiBytes)
Utf82Hebrew = asciiString
End Function

Converting String to List of Bytes

This has to be incredibly simple, but I must not be looking in the right place.
I'm receiving this string via a FTDI usb connection:
'UUU'
I would like to receive this as a byte array of
[85,85,85]
In Python, this I would convert a string to a byte array like this:
[ord(c) for c in 'UUU']
I've looked around, but haven't figured this out. How do I do this in Visual Basic?
Use the Encoding class with the correct encoding.
C#:
// Assuming string is UTF8
Encoding utf8 = Encoding.UTF8Encoding();
byte[] bytes = utf8.GetBytes("UUU");
VB.NET:
Dim utf8 As Encoding = Encoding.UTF8Encoding()
Dim bytes As Byte() = utf8.GetBytes("UUU")
depends on what kind of encoding you want to use but for UTF8 this works, you could chane it to UTF16 if needed.
Dim strText As String = "UUU"
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(strText)