The above is a SHA1 hash function for VB.NET.
Function getSHA1Hash(ByVal strToHash As String) As String
Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
bytesToHash = sha1Obj.ComputeHash(bytesToHash)
Dim strResult As String = ""
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next
Return strResult
End Function
Please could someone explain the code above (Visual Basic .NET), specifically the lines below -
bytesToHash = sha1Obj.ComputeHash(bytesToHash)
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
SHA1 creates a hash (array of bytes) representing the value strToHash. The foreach is just converting this array of bytes into a string.
Create a byte array containing the hash
bytesToHash = sha1Obj.ComputeHash(bytesToHash)
Loop through each byte just created
For Each b As Byte In bytesToHash
Append to the string the hex value of each byte
strResult += b.ToString("x2")
See this for into on hex format of ToString: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#XFormatString
Related
I used vb .NET function ReadAllBytes to read a file and send it over a socket. When received, I used WriteAllBytes. The problem is they are not same size! The original is 16kb, but the received data is 24kb. My code is below. What am I doing wrong?
Dim bteRead() As Byte
Try
bteRead = IO.File.ReadAllBytes(filepath)
Catch ex As System.IO.IOException
End Try
Return bteRead
then i convert bytes to string and send it , and when received i convert it back from string to bytes and do the WriteAllBytes
Dim str As String = a(1)
Dim encod As New System.Text.UTF8Encoding
Dim byteData() As Byte = encod.GetBytes(str)
IO.File.WriteAllBytes("c:\lol.db", byteData)
Solution for me was to change:
Dim encod As New System.Text.UTF8Encoding
Dim byteData() As Byte = encod.GetBytes(str)
To
Dim byteData() As Byte = System.Text.Encoding.Default.GetBytes(str)
i try to make function for lost password in my website using vb.net the code below is fore encrypt
Function getMD5Hash(ByVal strToHash As String) As String
Dim md5Obj As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
bytesToHash = md5Obj.ComputeHash(bytesToHash)
Dim strResult As String = ""
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next
Return strResult
End Function
MD5 is a hashing algorithm, not a bidirectional encryption system.
For lost password, you should send a password reset token instead of sending the end user the password.
I'm converting text to binary, and sending that information to the console. However, I want the console to write a new line for every 8 characters.
My convert to binary code looks like this (where Result.Text contains random text, that will be converted)
Dim Resultconvert As String = ""
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Resultconvert &= s
Next
Console.WriteLine(Resultconvert)
So it should look like this:
01110010
01111110
01101111
01011100
01100100
01010001
01001101
00111010
01010100
instead of:
011100100111111001101111010111000110010001010001010011010011101001010100
Any help is appreciated. Thank you in advance.
Dim Resultconvert As String = String.Empty
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Debug.Print(s)
Resultconvert &= s
Next
The simplest solution is to just call WriteLine separately for each byte, like this:
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Console.WriteLine(s)
Next
However, if you want to write it all at once, you need to append a new line to the string after each byte, like this:
Dim Resultconvert As String = ""
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
Resultconvert &= s & Environment.NewLine
Next
Console.WriteLine(Resultconvert)
But, at that point, it would be easier and more efficient to use a StringBuilder:
Dim builder As New StringBuilder()
For Each C As Char In Result.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
builder.AppendLine(s)
Next
Console.WriteLine(builder.ToString())
I figured this would be pretty straight forward but I have an issue with getting my AES Encryption function to return a Hex String. I can get it to work when I convert it to Base64 but I cannot get the String with Hex values. Here is my code. Any help would be appreciated.
Dim AES_ENCRYPTION As New System.Security.Cryptography.RijndaelManaged
Dim CODE_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = CODE_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES_ENCRYPTION.Key = hash
AES_ENCRYPTION.Mode = CipherMode.ECB
Dim AES_ENCRYPTOR As System.Security.Cryptography.ICryptoTransform = AES_ENCRYPTION.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = (Conversion.Hex(AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length)))
Catch ex As Exception
End Try
Return encrypted
I've tried your example, and I've got nothing either.
So what I tried instead of having encrypted = (Conversion.Hex(AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length))), I have used a loop to convert each byte to its hex equivalent, and concatenate it to encrypted.
Dim encrypted_byte() As Byte = AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length)
For i As Integer = 0 To encrypted_byte.Length - 1
encrypted = encrypted & Hex(encrypted_byte(i)).ToUpper
Next
I'm not sure how you formatted your hex string in Java, but this should be at least a start.
I have the following function for generating sha512 hashs. The hash is generated successfully, but is causing this error when the resulting string is passed to other functions:
Input string was not in a correct format
When debugged the variable holding the returned hash (set as string) is empty. I have tried changing the type to int, int64 and byte (array and standard variable) in the function and in the calling code, which causes various other errors. How can I change the datatype correctly to solve this?
Function create_hash(ByVal password, ByVal salt)
Dim salty As String = password & salt
'convert salty password to binary to feed into hash function
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(salty)
'Dim data(btText) As Byte
'create password hash
Dim result() As Byte
Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(btText)
Dim return_result As String
For Each Item As Integer In result
return_result = return_result & Item
Next
Return return_result
End Function
Calling code:
Dim i_h_pass As String
Dim i_pass As String = pass.Text
'handle password generation (matching passwords checked at validation)
Dim newHash = New hashing
Dim salt As String = Convert.ToString(newHash.create_salt)
i_h_pass = Convert.ToString(newHash.create_hash(i_pass, salt))
edit:
the create_salt function has also been checked - it works perfectly and returns a random integer, returned as string for conveince
Fixed with:
Function create_hash(ByVal password, ByVal salt)
Dim salty As String = password & salt
'convert salty password to binary to feed into hash function
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(salty)
'Dim data(btText) As Byte
'create password hash
Dim result() As Byte
Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(btText)
Dim return_result As String = BitConverter.ToString(result)
Return return_result
End Function
Dim return_result As String = BitConverter.ToString(result)
Being the change