Unsigned Byte flip with *.bin file [duplicate] - vb.net

This question already has answers here:
16 bit unsigned short byte flip in VB.NET
(2 answers)
Closed 8 years ago.
I need example in code, how to byte flip whole binary file. VB.NET
Example
02 00 0D 78 10 20 40 80 F1 F2 F4 F8 1F 2F 4F 8F
Flip Operation
00 02 78 0D 20 10 80 40 F2 F1 F8 F4 2F 1F 8F 4F
Whole, binary file using OpenFileDialog (OFD)

You just need to loop all items of the array and swap each bytes.
Dim bytes() As Byte = {&H2, &H0, &HD, &H78, &H10, &H20, &H40, &H80, &HF1, &HF2, &HF4, &HF8, &H1F, &H2F, &H4F, &H8F}
For i As Integer = 0 To bytes.Length - 1 Step 2
bytes(i) = bytes(i) Xor bytes(i + 1)
bytes(i + 1) = bytes(i + 1) Xor bytes(i)
bytes(i) = bytes(i) Xor bytes(i + 1)
Next
If you want, use a temp variable
For i As Integer = 0 To bytes.Length - 1 Step 2
Dim tempByte As Byte = bytes(i)
bytes(i) = bytes(i+1)
bytes(i + 1) = tempByte
Next

erm, you could write as you go, something like,
Dim file = dialog.FileName
Using output = New BinaryWriter(New FileStream( _
file, _
FileMode.Append, _
FileAccess.Write, _
FileShare.Read))
Using input = New BinaryReader(New FileStream( _
file, _
FileMode.Open, _
FileAccess.Read, _
FileShare.ReadWrite))
Dim bufferLength = 2
While bufferLength = 2
Dim buffer = input.ReadBytes(2)
bufferLength = buffer.Length
If bufferLength = 2 Then
output.Write(buffer(1))
output.Write(buffer(0))
Else If bufferLength = 1 Then
output.Write(buffer(0))
End If
End While
End Using
End Using

Related

Convert big string to decimal in vb.net

Hello i have big string value which is md5 of something now i need to convert it into the decimal value
for example
Dim md5_s As String = "6F05AF42533432A5513610FE839ACC86"
now i need output same like the online converters to this
"54 70 48 53 65 70 52 50 53 51 51 52 51 50 65 53 53 49 51 54 49 48 70
69 56 51 57 65 67 67 56 54 "
is it possible i don't want spaces in the above converted decimal ?
vb.net help please
Okay here i tried and got it n is my approach works fine will this fine n work always right
Dim t As String
Dim a As String = "6F05AF42533432A5513610FE839ACC86"
For Each c As Char In a
t &= Convert.ToInt32(c)
Next
TextBox1.Text = t
will this one is right ?
result is same what i am looking for as
5470485365705250535151525150655353495154494870695651576567675654
so i assume this is right huh ?
I'm not quite sure this is what you are really looking for but this is what you asked for
For count = 0 To md5_s.Length - 1
Dim tempChar As String = md5_s.Substring(count, 1)
Console.Write(Asc(tempChar))
Next
What is more likely what you want is something like this
Private Function HexToByteArray(ByVal hex As [String]) As Byte()
Dim NumberChars As Integer = hex.Length
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
For i As Integer = 0 To NumberChars - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
either way ... hope this helps

Convert Hex to String: special characters not converted properly

I have to convert the following hex encoded string:
56 6f 6e 68 c3 b6 67 65 6e
The result should be: Vonhöger
However, the result I get is: Vonhöger
What is wrong with my code?
For x = 0 To hexString.Length - 1 Step 2
Dim k As String = hexString.Substring(x, 2)
If (k <> "X'") Then
com &= Chr(Val("&h" & k))
End If
Next
You are converting each byte to a unicode character, but the data is UTF-8 encoded, so some bytes in combination forms a character. The bytes c3 b6 is the code for the ö character.
Convert the data into bytes, then decode it as UTF-8:
Dim hexString = "566f6e68c3b667656e"
Dim bytes() As Byte
ReDim bytes(hexString.Length \ 2 - 1)
For i As Integer = 0 To bytes.Length - 1
bytes(i) = Convert.ToByte(hexString.Substring(i * 2, 2), 16)
Next
Dim com As String = Encoding.UTF8.GetString(bytes)
Similar to the other suggestions:
<Extension>
Public Function FromHexToUnicodeString(target As String) As String
Dim bytes((target.Length \ 2) - 1) As Byte
For b = 0 To bytes.Length - 1
bytes(b) = Convert.ToByte(target.Substring(b * 2, 2), 16)
Next
Return Text.Encoding.UTF8.GetString(bytes)
End Function
Usage:
MessageBox.Show("566f6e68c3b667656e".FromHexToUnicodeString)

Base64 Encoding for file upload VB.Net

I have a project in VBA that does the base64 encoding and uploads the file (.xlsm).
VBA Code:
Option Explicit
Private Const clOneMask = 16515072 '000000 111111 111111 111111
Private Const clTwoMask = 258048 '111111 000000 111111 111111
Private Const clThreeMask = 4032 '111111 111111 000000 111111
Private Const clFourMask = 63 '111111 111111 111111 000000
Private Const clHighMask = 16711680 '11111111 00000000 00000000
Private Const clMidMask = 65280 '00000000 11111111 00000000
Private Const clLowMask = 255 '00000000 00000000 11111111
Private Const cl2Exp18 = 262144 '2 to the 18th power
Private Const cl2Exp12 = 4096 '2 to the 12th
Private Const cl2Exp6 = 64 '2 to the 6th
Private Const cl2Exp8 = 256 '2 to the 8th
Private Const cl2Exp16 = 65536 '2 to the 16th
Private cbTransTo(63) As Byte
Private cbTransFrom(255) As Byte
Private clPowers8(255) As Long
Private clPowers16(255) As Long
Private clPowers6(63) As Long
Private clPowers12(63) As Long
Private clPowers18(63) As Long
Private Sub Class_Initialize()
Dim lTemp As Long
For lTemp = 0 To 63 'Fill the translation table.
Select Case lTemp
Case 0 To 25
cbTransTo(lTemp) = 65 + lTemp 'A - Z
Case 26 To 51
cbTransTo(lTemp) = 71 + lTemp 'a - z
Case 52 To 61
cbTransTo(lTemp) = lTemp - 4 '1 - 0
Case 62
cbTransTo(lTemp) = 43 'Chr(43) = "+"
Case 63
cbTransTo(lTemp) = 47 'Chr(47) = "/"
End Select
Next lTemp
For lTemp = 0 To 255 'Fill the lookup tables.
clPowers8(lTemp) = lTemp * cl2Exp8
clPowers16(lTemp) = lTemp * cl2Exp16
Next lTemp
For lTemp = 0 To 63
clPowers6(lTemp) = lTemp * cl2Exp6
clPowers12(lTemp) = lTemp * cl2Exp12
clPowers18(lTemp) = lTemp * cl2Exp18
Next lTemp
For lTemp = 0 To 255 'Fill the translation table.
Select Case lTemp
Case 65 To 90
cbTransFrom(lTemp) = lTemp - 65 'A - Z
Case 97 To 122
cbTransFrom(lTemp) = lTemp - 71 'a - z
Case 48 To 57
cbTransFrom(lTemp) = lTemp + 4 '1 - 0
Case 43
cbTransFrom(lTemp) = 62 'Chr(43) = "+"
Case 47
cbTransFrom(lTemp) = 63 'Chr(47) = "/"
End Select
Next lTemp
End Sub
Public Function Encode(sString As String) As String
Dim bTrans(63) As Byte, bOut() As Byte, bIn() As Byte, lOutSize As Long
Dim lChar As Long, lTrip As Long, iPad As Integer, lLen As Long, lTemp As Long, lPos As Long
iPad = Len(sString) Mod 3 'See if the length is divisible by 3
If iPad Then 'If not, figure out the end pad and resize the input.
iPad = 3 - iPad
sString = sString & String(iPad, Chr(0))
End If
bIn = StrConv(sString, vbFromUnicode) 'Load the input string.
lLen = ((UBound(bIn) + 1) \ 3) * 4 'Length of resulting string.
lTemp = lLen \ 72 'Added space for vbCrLfs.
lOutSize = ((lTemp * 2) + lLen) - 1 'Calculate the size of the output buffer.
ReDim bOut(lOutSize) 'Make the output buffer.
lLen = 0 'Reusing this one, so reset it.
For lChar = LBound(bIn) To UBound(bIn) Step 3
lTrip = clPowers16(bIn(lChar)) + clPowers8(bIn(lChar + 1)) + bIn(lChar + 2) 'Combine the 3 bytes
lTemp = lTrip And clOneMask 'Mask for the first 6 bits
bOut(lPos) = cbTransTo(lTemp \ cl2Exp18) 'Shift it down to the low 6 bits and get the value
lTemp = lTrip And clTwoMask 'Mask for the second set.
bOut(lPos + 1) = cbTransTo(lTemp \ cl2Exp12) 'Shift it down and translate.
lTemp = lTrip And clThreeMask 'Mask for the third set.
bOut(lPos + 2) = cbTransTo(lTemp \ cl2Exp6) 'Shift it down and translate.
bOut(lPos + 3) = cbTransTo(lTrip And clFourMask) 'Mask for the low set.
If lLen = 68 Then 'Ready for a newline
bOut(lPos + 4) = 13 'Chr(13) = vbCr
bOut(lPos + 5) = 10 'Chr(10) = vbLf
lLen = 0 'Reset the counter
lPos = lPos + 6
Else
lLen = lLen + 4
lPos = lPos + 4
End If
Next lChar
If bOut(lOutSize) = 10 Then lOutSize = lOutSize - 2 'Shift the padding chars down if it ends with CrLf.
If iPad = 1 Then 'Add the padding chars if any.
bOut(lOutSize) = 61 'Chr(61) = "="
ElseIf iPad = 2 Then
bOut(lOutSize) = 61
bOut(lOutSize - 1) = 61
End If
Encode = StrConv(bOut, vbUnicode) 'Convert back to a string and return it.
End Function
The generated output is exactly like this:
Required Output
I am trying to achieve the same output in VB.Net but I am unable to do so. I tried using the following:
Dim testStr As String = "test.xlsm"
Dim byt As Byte() = Encoding.Default.GetBytes(testStr)
testStr = Convert.ToBase64String(byt)
Input String: http://txt.do/obff
But I am not getting exactly the same string back. The first line is different in both cases (VBA and VB.Net) and I'm not sure why.
Required Output (VBA and the weblink):
UEsDBBQABgAIAAAAIQCsmTVRbwEAAD8EAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAA
Generated Output (VB.Net):
UEsDBBQABgAIAAAAIQA/PzVRbwEAAD8EAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCA/BAIoPwACAAAAAAAAAAAAAAAAAAAAAAAAA
Could you please let me know where I am going wrong and how can I achieve the same in VB.Net?
I managed to solve the problem by getting the file directly, reading all its bytes and converting it to base64 string.
Dim dat As Byte() = IO.File.ReadAllBytes(FileName)
testStr = Convert.ToBase64String(dat, Base64FormattingOptions.InsertLineBreaks)

How to generate a 26-character hex string that equals to 106 bits and ((53 Ones - 53 Zeros) in binary)

I am looking for a way to generate a hexadecimal string that equals out to 106 bits, more specifically fifty three 1's and fifty three 0's after each hex char is converted to binary and added together. I'd like to keep it as random as possible considering the parameters of the request. How would I go about keeping an eye on the construction of the string so that it equals out the way I want?
For example:
(a8c05779f8934b14ce96f8aa93) =
(1010 1000 1100 0000 0101 0111 0111 1001 1111 1000 1001 0011 0100
1011 0001 0100 1100 1110 1001 0110 1111 1000 1010 1010 1001 0011)
One option is to create a list with an equal number of 0s and 1s and then sort it with an array of random keys:
Sub Main()
' Start with a list of 53 0's and 1's
Dim bitsList = New List(Of Integer)
For i = 1 To 53
bitsList.Add(1)
bitsList.Add(0)
Next
Dim bits = bitsList.ToArray()
' Create list of random keys
Dim keys = New List(Of Integer)
Dim rand = New Random()
For i = 1 To bits.Count
keys.Add(rand.Next())
Next
' Sort bits by random keys
Array.Sort(keys.ToArray(), bits)
' Create hex string
Dim s = ""
For i = 1 To bits.Length - 4 Step 4
Dim digit = bits(i + 3) * 8 + bits(i + 2) * 4 + bits(i + 1) * 2 + bits(i)
s = s + Hex(digit)
Next
Console.WriteLine(s)
End Sub
You can place 52 ones randomly in a 104 bit number by keeping track of how many ones has been placed already and calculate the probability that the next digit should be one. The first digit always has 1/2 probability (52/104), then the second digit has 51/103 or 52/103 probability depending on what the first digit was, and so on.
Put the bits in a buffer, and when it is full (four bits), that makes a hexadecimal digit that you can add to the string:
Dim rnd As New Random()
Dim bin As New StringBuilder()
Dim buf As Integer = 0, bufLen As Integer = 0, left As Integer = 52
For i As Integer = 104 To 1 Step -1
buf <<= 1
If rnd.Next(i) < left Then
buf += 1
left -= 1
End If
bufLen += 1
If bufLen = 4 Then
bin.Append("0123456789abcdef"(buf))
bufLen = 0
buf = 0
End If
Next
Dim b As String = bin.ToString()
To make a 106 bit value, change these lines:
Dim buf As Integer = 0, bufLen As Integer = 0, left As Integer = 53
For i As Integer = 106 To 1 Step -1
The resulting string is still 26 characters, the two extra bits are in the buf variable. It has a value between 0 and 3 that you can use to create the 27th character, however that is done.
To add a 22 bit hash to the string, you can use code like this:
bin.Append("048c"(buf))
Dim b As String = bin.ToString()
Dim m As New System.Security.Cryptography.SHA1Managed
Dim hash As Byte() = m.ComputeHash(Encoding.UTF8.GetBytes(b))
'replace first two bits in hash with bits from buf
hash(0) = CByte(hash(0) And &H3F Or (buf * 64))
'append 24 bits from hash
b = b.Substring(0, 26) + BitConverter.ToString(hash, 0, 3).Replace("-", String.Empty)

Simple rot13 encoder in vb.net

I am looking for a simple way to encode an inputted text into Rot13. I am hitting a brick wall at the stage of being able to separate out words into individual characters and integers so that I can change each one and output the result. I can do it with single letters using a simple if statement listed bellow but if anyone can help with a way of doing it for whole words I would be very appreciative.
If kInput = "a" then kOutput = "n"
Thanks, Kai
Looks like people are giving good answers to this but here's my try at it.
Dim input As String = "This is a Test!! Guvf vf n Grfg!!"
Dim result As StringBuilder = New StringBuilder()
For Each ch As Char In input
If (Not Char.IsLetter(ch)) Then
result.Append(ch)
Continue For
End If
Dim checkIndex As Integer = Asc("a") - (Char.IsUpper(ch) * -32)
Dim index As Integer = ((Asc(ch) - checkIndex) + 13) Mod 26
result.Append(Chr(index + checkIndex))
Next
Console.WriteLine(result.ToString())
EDIT: improved to remove need for uppercase check. This will properly handle case and special characters with only 1 if statement inside the loop.
It seems like you're making this way harder than it has to be. No need to separate words, etc, and definitely no need for a large If/Else block:
Public Function Rot13(ByVal input As String) As String
Dim result As Char() = input.ToCharArray()
For i As Integer = 0 To result.Length - 1
Dim temp As Integer = Asc(result(i))
Select Case temp
Case 65 to 77, 97 To 109 'A - M
result(i) = Chr(temp + 13)
Case 78 to 90, 110 To 122 'N - Z
result(i) = Chr(temp - 13)
End Select
Next i
Return New String(result)
End Function
Note that this was entered directly into the browser window and is completely untested.
Just call it once to encode, call it again to decode.
Private Function ROT13_Encode(ByVal Input As String) As String
Dim chrs As Char() = Input.ToCharArray()
Dim ReturnString As String = ""
Dim CharInt As Integer
For Each Chr As Char In chrs
CharInt = Asc(Chr)
If CharInt >= 65 And CharInt <= 77 Then 'A-M
CharInt += 13
ElseIf CharInt >= 78 And CharInt <= 90 Then 'M-Z
CharInt -= 13
ElseIf CharInt >= 97 And CharInt <= 109 Then 'a-m
CharInt += 13
ElseIf CharInt >= 110 And CharInt <= 122 Then 'm-z
CharInt -= 13
End If
ReturnString &= ChrW(CharInt)
Next
Return ReturnString
End Function