Re: simple string encryption



"Matt Williamson" <ih8spam@xxxxxxxxxxx> wrote in message news:%23tzqHNS3IHA.4476@xxxxxxxxxxxxxxxxxxxxxxx

I'm working on a little project that requires lightweight string
encryption and I found this: http://www.devx.com/tips/Tip/5676
which seems to be a fast and small xor encryption algorithm. It
works fine based upon the example, but if I dump the encrypted
string into a textbox and then read the string from the textbox
and feed it back to the routine, it doesn't decrypt the string. Does
adding it to a textbox introduce any additional characters that throw off the routine?

Yes, it does. The reason is because strings in VB use two bytes per character and your code, as it stands, is messing about with both bytes of each character. This causes the VB TextBox to fail to understand the characters when you pass it the string, because a standard textBox is not unicode aware and the character byte that it expects to be zero is something other than zero. This causes the TextBox to fail to recognise the character and so it changes it to a standard question mark and inserts those question mark characters into the TextBox. When you later transfer the contents of the textBox to a String for running through the encryption routine again (to decrypt it) you are actually passing the routine a string consisting of just a bunch of question marks, which of course causes it to fail to perform its task. There are various ways around this problem but perhaps the simplest is to prevent the encryption routine from messing about with the "zero bytes" of each character (which it is only setting to the value 38 anyway and so it lends virtually nothing to the encryption itself). One way would be to change the code so that it converts into a Byte Array at one byte per character instead of two (using StrConv) but perhaps a simpler way would be to leave it as it is and simply Step 2 instead of 1 when running through the Byte Array. Try changing this part of the routine:

For lNum = LBound(bytData) To UBound(bytData)
If lNum Mod 2 Then
bytData(lNum) = bytData(lNum) Xor (bytKey(lNum) _
+ KEY_OFFSET)
Else
bytData(lNum) = bytData(lNum) Xor (bytKey(lNum) _
- KEY_OFFSET)
End If
Next lNum

to :

For lNum = LBound(bytData) To UBound(bytData) Step 2
bytData(lNum) = bytData(lNum) Xor (bytKey(lNum) _
- KEY_OFFSET)
Next lNum

Of course, if you want the encryption routine to encrpyt and decrypt Unicode characters then you actually do want it to "mess about with" both bytes of the characters (which it is already doing) but then of course you cannot pass the encrypted string to and from a standard TextBox, which is not Unicode aware.

Mike



.



Relevant Pages

  • Re: Microsoft.VisualBasic.Strings.Asc ?
    ... I'm trying to put together a simple utility to do rc4 encryption based on the rc4 algorithm here: ... You don't need to use Substring to access a single character in a string, and you don't need ToCharArray to convert a single character string to a character. ... Actually, the encryption methods in the .NET framework doesn't take strings at all, they encrypt streams of bytes, so the method should probably do the same and leave the encoding to the one using the method. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Encryption algorithm
    ... > character alphanumeric string in a COBOL program. ... consider these "obscuring" rather than "encryption", ... that you run the string through the same algorithm to encode and decode. ... This has the advantage of working with pretty much any character you'd ...
    (comp.lang.cobol)
  • End of String Character?
    ... string using some extended ASCII characters (ASCII code> 128) ... I am wondering if there is a reserved character in VB that signifies ... the end of a string of characters. ... Is my encryption algorithm possible generating a reserved character ...
    (microsoft.public.scripting.vbscript)
  • Re: simple string encryption
    ... seems to be a fast and small xor encryption algorithm. ... then read the string from the textbox and feed it back to the routine, ... additional characters that throw off the routine? ...
    (microsoft.public.vb.general.discussion)
  • Re: Problem with appending text to VBA Text box
    ... a trailing "new line" character is unusable unless there is text ... Private Sub AppendImportMessage(str As String) ... ImportResult.Value= & str ... a Null value, so if the textbox is initially empty, the new line character ...
    (microsoft.public.access.modulesdaovba)