standard keyboard character not included in Base64? - vb.net

I have a function that generates a random Base64 String
Public Shared Function GenerateSalt() As String
Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider
Dim buff(94) As Byte
rng.GetBytes(buff)
Return Convert.ToBase64String(buff)
End Function
This will always return a 128 Character String. I then take that string and divide it into 4 substrings. I then merge that all back into one big string called MasterSalt like so
MasterSalt = (Salt.Substring(1,32)) + "©" + (Salt.Substring(32,32)) + "©" + etc...
I am doing this because I then put all of this into an array and say Split(MasterSalt, "©")
My concern is I am not overly confident in the stability of using "©" as the delimiter to define where the string should be split. However I have to use something that is not going to be included in the randomly generated base64string. I would like it to be something that can be found on a standard keyboard if possible. So to be clear my question is: is there a glyph or character on a standard keyboard that would never be included in a randomly generated base64string??

Base64 uses 64 characters to encode 6 bits of the content at a time as values 0-63;
A-Z (0-25)
a-z (26-51)
0-9 (52-61)
+ (62)
/ (63)
...and it uses = as filler at the end if required.
Any other character will be available for you to use as a delimiter, for example space, period and minus.

Related

How to Trim right and left a String in VB .net

I want to take the value of
T.GS.+0.220kg
but I don't know how to remove the string.
I just want to take numbers from the weight.
like 0.220
Can someone help me ?
You can make use of the Regular Expressions to extract a decimal value from basically any string. First you'd need to import the library:
Imports System.Text.RegularExpressions
Then using this will return just the decimal value:
Regex.Match("T.GS.+0.220kg", "\d+.\d+").Value
This particular expression looks for a digit or digits, followed by a point (dot), followed by another number of digits, so the previous points (in between T and G for example) aren't included.
This returns exactly 0.220, you can then replace the string with any string variable and assign this expression as needed.
If you havn't worked with regular expressions before and want somthing that looks a little nicer. You could use the string.split method.
dim input as string = "T.GS.+0.220kg"
input = input.split("+")(1) ' which will grab the "0.220kg"
input = input.substring(0, input.length - 2) ' then filter off the last 2 chars
In english:
split the string into 2 seperate pieces grabing the part to the right of the first '+' symbol.
Then remove the last 2 chars from the end.

Labview converting hexformatted string to ascii

In labview I am trying to convert a hex string to ascii format. For example if I have a hexstring like: 09124E4F21CD0024FFFFFFFFFFFFFFFF the ascii version of this is : NO!Í or basically a bunch of illegible symbols. I tried using the labview functions of converting hexstring to number but they didn't work. How would I convert the ascii part to hexformatted ascii?
Hexadecimal String to Number works fine, but only for a hex string that represents a number that can be stored as a numeric data type:
If the input string represents a number outside the range of the
representation of number, number is set to the maximum value for that
data type.
Your example input is 128 bits long whereas the longest integer data type in current LabVIEW is 64 bits.
You can use this function, but you need to convert the input one byte at a time:
Create a While Loop and add a shift register. Initialise the shift register with your input string.
Inside the loop, wire the string from the shift register to the string input of a Search/Split String function
Wire a numeric constant of 2 to the offset input - i.e. split the string into the first two characters, and the rest
Wire the match + rest of string output to the right-hand shift register terminal
Wire the substring before match output to a Hexadecimal String to Number function
Wire the default input of this function to a numeric constant with value 0 and type U8
Wire the output of this function to the right-hand side of the While loop and make the terminal indexing (via right-click)
Use an Empty String/Path? function to exit the While loop when the string being passed back into the shift register is empty.
The output from the indexing terminal you created will now be a U8 (byte) array containing the data converted from the input string. If you want it in string form you can convert it using Byte Array to String.
This assumes that your input string is always a multiple of 2 characters in length. If you need it to handle an input such as "3F2" you'll need to check for this and do something to the input (I'll let you figure out what) before passing it into your loop.

how to correct incoming data that contains char not in Ascii to unicode before saving to the database

I have a webservice api in vb.net that accepts string. but i cannot control the data coming to this API. I sometimes receive chars in between words in this format (–, Á, •ï€,ââ€ï€, etc. ) Is there a way for me to handle these or convert these characters to their correct symbols before saving to the database?
i know that the best solution would be to go after the source where the characters get malformed.. but i'll make that as plan B
my code is already using utf-8 as encoding pattern, but what if the client that uses my API messed up and inadvertently sent the malformed char thru the API. can i clean that string and convert the malformed char to the correct symbol?
If you only want to accept ASCII characters, you could remove non-ASCII characters by encoding and decoding the string - the default ASCII encoding uses "?" as a substitute for unrecognized characters, so you probably want to override that:
' Using System.Text
Dim input As String = "âh€eÁlâl€o¢wïo€râlâd€ï€"
Dim ascii As Encoding = Encoding.GetEncoding(
"us-ascii",
New EncoderReplacementFallback(" "),
New DecoderReplacementFallback(" ")
)
Dim bytes() As Byte = ascii.GetBytes(input)
Dim output As String = ascii.GetString(bytes)
Output:
h e l l o w o r l d
The replacement given to the En/DecoderReplacementFallback can be empty if you just want to drop the non-ASCII characters.
You could use a different encoding than ASCII if you want to accept more characters - but I would imagine that most of the characters you listed are valid in most European character sets.
While you are kind of vague I could guide you in something I think you could potentially do:
Sub Main()
Dim allowedValues = "abcdefghijklmnopqrstuvwxyz".ToCharArray()
Dim someGoodSomeBad = "###$##okay##$##"
Dim onlyGood = New String(someGoodSomeBad.ToCharArray().Where(Function(x) allowedValues.Contains(x)).ToArray)
Console.WriteLine(onlyGood)
End Sub
The first line would be valid characters, in my example I chose to use alpha characters, you could add more characters and numbers too. Basically you are creating a whitelist of acceptable characters, that you the developer would make.
The next line would be an output from your API that has some good and some bad lines.
The next part is really more simple than it looks. I am extending the string to be an array of characters, then I am finding ONLY the characters that match my whitelist in a lambda statement. Then I extend this to an array again because if I do a new String in .NET from a char array.
I then get a good string, but I could make 'good' to be subjective based on a whitelist.
The bigger question though is WHY is your Web API sending garbled data over? It should be sending well formed JSON or XML that is then able to well parsed and strongly type to models. Doing what I have shown above is more of a bandaide than a real fix to the underlying problem and it will have MANY holes.

Regex extracting numbers & letters - Visual Basic

And again, I am having issues with regex.
Well basically I want to extract info from a website, and get it in a textbox.
what I want to be extracted
So as you can see, the part what says 11111 must be enabled to also extract letters instead of only numbers.
I use this code:
Dim mcol As MatchCollection = Regex.Matches(source, "/player/\d+"">(.+)</a>")
How can I make it to extract not only numbers, but both.
So it will be enable to extract a MD5 hash?
Kind regards
/\w+/i it will match any alphanumeric character with case insensitive modifier. As we know that MD5 is 32 digit hexadecimal number so the above regex may be useful in your case.

How can I write special character in VB code

I have a Sql statament using special character (ex: ('), (/), (&)) and I don't know how to write them in my VB.NET code. Please help me. Thanks.
Find out the Unicode code point for the character (from http://www.unicode.org) and then use ChrW to convert from the code point to the character. (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.)
For example, for the Euro sign (U+20AC) you'd write:
Dim euro as Char = ChrW(&H20AC)
The advantage of this over putting the character directly into source code is that your source code stays "just pure ASCII" - which means you won't have any strange issues with any other program trying to read it, diff it, etc. The disadvantage is that it's harder to see the symbol in the code, of course.
The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap
If you are passing strings to be processed as SQL statement try doubling the characters for example.
"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "
The '' double works with the other special characters as well.
The ' character can be doubled up to allow it into a string e.g
lSQLSTatement = "Select * from temp where name = 'fred''s'"
Will search for all records where name = fred's
Three points:
1) The example characters you've given are not special characters. They're directly available on your keyboard. Just press the corresponding key.
2) To type characters that don't have a corresponding key on the keyboard, use this:
Alt + (the ASCII code number of the special character)
For example, to type ¿, press Alt and key in 168, which is the ASCII code for that special character.
You can use this method to type a special character in practically any program not just a VB.Net text editor.
3) What you probably looking for is what is called 'escaping' characters in a string. In your SQL query string, just place a \ before each of those characters. That should do.
Chr() is probably the most popular.
ChrW() can be used if you want to generate unicode characters
The ControlChars class contains some special and 'invisible' characters, plus the quote - for example, ControlChars.Quote