Recipe 5.4. Obfuscating a StringProblem
You need to store a string in such a way that a
SolutionSample code folder: Chapter 05\ObfuscateString Process each printable character of the string by shifting its ASCII value to that of another character within the same set. The following two functions can be used to obfuscate strings in this way and then return them to their original states: Public Function Obfuscate(ByVal origText As String) As String ' ----- Make a string unreadable, but retrievable. Dim textBytes As Byte( ) = _ System.Text.Encoding.UTF8.GetBytes(origText) For counter As Integer = 0 To textBytes.Length - 1 If (textBytes(counter) > 31) And _ (textBytes(counter) < 127) Then textBytes(counter) += CByte(counter Mod 31 + 1) If (textBytes(counter) > 126) Then _ textBytes(counter) -= CByte(95) End If Next counter Return System.Text.Encoding.UTF8.GetChars(textBytes) End Function Public Function DeObfuscate(ByVal origText As String) _ As String ' ----- Restore a previously obfuscated string. Dim textBytes As Byte( ) = _ System.Text.Encoding.UTF8.GetBytes(origText) For counter As Integer = 0 To textBytes.Length - 1 If (textBytes(counter) > 31) And _ (textBytes(counter) < 127) Then textBytes(counter) -= CByte(counter Mod 31 + 1) If (textBytes(counter) < 32) Then _ textBytes(counter) += CByte(95) End If Next counter Return System.Text.Encoding.UTF8.GetChars(textBytes) End Function Figure 5-3 shows a string before and after calling Obfuscate() , and after returning it to its original state by calling DeObfuscate() . Figure 5-3. Results of obfuscating a string to make it unreadable, then deobfuscating it
Discussion
The
Obfuscate()
function lets you modify strings to an unreadable state without
When modifying individual bytes of a string, it's often best to first convert the string to an array of bytes, as shown in these functions. You can
If you work with international character sets, consider using the Unicode versions of the encoding conversion functions instead of the UTF8 versions. The byte arrays will be twice as large, but you should be able to handle other sets of characters. You'll also need to pay close attention to the numerical shift of the byte values, modifying the above code to keep the results within the desired range of characters. See Also
Recipe 5.23 discusses additional modifications to strings that can be
|
Recipe 5.5. Converting Binary Data to a Hexadecimal StringProblemYou need to convert a byte array to a hexadecimal string. This is handy for the display or documentation of binary data. SolutionUse a bit converter to get the hexadecimal representation of each byte within a block of data. The following code generates the hexadecimal string from source data: Dim result As String = Replace(BitConverter.ToString( _ origBytes), "-", "") Discussion
There are several approaches to solving this problem. A quick review of some of these approaches will
The code samples in this recipe assume a byte array named origBytes built using the following code, which creates a byte array of length 256 containing one each of the byte values 0 through 255: Dim origBytes(255) As Byte For counter As Byte = 0 To 255 origBytes(counter) = counter Next counter
The first approach is somewhat "brute force" in nature. Each byte of the array is converted to a two-character string using one of the many formatting options of the byte's
ToString()
method. These short strings are
Dim result As String = ""
For counter As Byte = 0 To 255
result &= origBytes(counter).ToString("X2")
Next counter
This is fine for small arrays of bytes, but the string concatenation quickly becomes
Dim workText As New System.Text.StringBuilder(600)
For counter = 0 To 255
workText.Append(origBytes(counter).ToString("X2"))
Next counter
Dim result As String = workText.ToString()
This solution runs faster, but it seems to lack the
Dim result As String result = BitConverter.ToString(origBytes) '00-3F-F7 etc. result = Replace(result, "-", "") '003FF7 etc. The solution presented first in this recipe is the result of combining these two function calls into a single line of code. Figure 5-4 shows the resulting hexadecimal string displaying all possible byte values. Figure 5-4. The hexadecimal string equivalent of a byte array comprised of the values 0 to 255
See AlsoRecipes 5.16 and 5.26 show other useful ways of modifying portions of strings. |