String to BitArray for XORing

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi All,

I need to XOR two same-length Strings against each other. I'm assuming
that, in order to do so, I'll need to convert each String to a BitArray.
Thus, my question is this: is there an easy way to convert a String to a
BitArray (and back again)?

I explained the ultimate goal (XORing two Strings) so that, if anyone has a
better idea of how to go about this they may (hopefully) bring that up...?


Thanks for your help!!

Eric
 
Eric,
Do you want a string as the result?

I don't think I would bother with a BitArray, instead use a normal Char
array, iterate over both strings xor each char into this char array, then I
would create a new string based on the array:

Something like:

' VS.NET 2003 syntax
Option Strict On
Option Explicit On

Private Function XorStrings(ByVal s1 As String, ByVal s2 As String) As
String
' length of the resulting string
Dim length As Integer = Math.Max(s1.Length, s2.Length)

' ensure both strings are the same length
s1 = s1.PadRight(length, Char.MinValue)
s2 = s2.PadRight(length, Char.MinValue)

' xor one string into the other
Dim chars(length - 1) As Char
For index As Integer = 0 To length - 1
chars(index) = ChrW(AscW(s1.Chars(index)) Xor
AscW(s2.Chars(index)))
Next
' return a new string, eliminating trailing padding
Dim result As New String(chars)
Return result.TrimEnd(Char.MinValue)
End Function

Public Sub Main()
Dim s1 As String = "First String"
Dim s2 As String = "Second string"

Dim result As String = XorStrings(s1, s2)

result = XorStrings(result, s2)

End Sub

Instead of Char.MinValue you could a different char to pad the string length
to in XorStrings, I would use the same string to trim the end.

Note the above does 16 bit char values, if instead you want ASCII or ANSI
values (bytes), I would use System.Text.Encoding to convert the string to a
byte array, then xor each byte in both arrays. Optionally you could use
System.Text.Encoding to convert the byte arrays back to a String.
Thus, my question is this: is there an easy way to convert a String to a
BitArray (and back again)?
Use the System.Text.Encoding class to decode the string into an array of
Bytes, pass this to the BitArray constructor. To get back to a string, use
BitArray.CopyTo to copy the contents of the BitArray to an array of bytes,
then use the System.Text.Encoding class to convert this array of bytes back
to a string.

Hope this helps
Jay
 
Hi Eric,
you can use UnicodeEncoding.GetBytes method, to obtain an array of
bytes from the string, as it is stored in memory. Then, you can use
that array to construct an BitArray.
The problem is to get it back again, to a string. I didn't tried it,
but may be using BitArray.CopyTo, you can get the bytes array again.
Once there, use UnicodeEncoding.GetString.
 
Thank you both for your help; I feel I can accomplished this now! Jay, to
answer your question: yes, I'm going to want to wind up with a string when
I'm finished. I'm working on encryption software. I'll (unfortunately)
need to keep this ASCII rather than Unicode because the software which
originally encrypted this text treated it as ASCII (and I need to be able to
have this program share the same ecrypted data which the original program
uses).

I have some great direction now - I truly thank both of you for your time!
:)

Eric
 
Eric,
Just remember once you put it in a String variable it is Unicode.

Hope this helps
Jay
 
Hi Eric,

You can Xor two numbers, but not two chars (nor strings). You can also Xor
two arrays with numbers (bytes, for instance), and the result (xored output,
"ciphertext") is also an array with numbers.

It's a common (newbie) misunderstanding, about storing ciphertext as
strings. The point is you shouldn't. Converting a buffer into a string and
vice-versa is called Text encoding and decoding. There are several encoding
schemes (Ascii, Unicode, UTF8, etc). Each ciphertext byte can range from 0
to 255. Encoding schemes do not preserve all those unique values. And a
single wrong byte is enough to make decryption impossible.

You should always store and handle ciphertext as a buffer (array of bytes).
If you really need to store it as a string then you need to get a Base
encoding representation of the buffer (hexadecimal, or Base64/Mime). You can
create your own Base encoding functions or use framework
System.Convert.ToBase64(buffer).

Avoid Xor with key algorithms... that's no real encryption. It's just too
plain simple to run some frequency statistics on the ciphertext and get your
key.

Regards,
Mario
 
Yeah - that occured to me. I think I'm going to have to do *exactly* what
you suggested in your first post. Thank you so much for your help!


Eric
 
Hi Mario,

Your last statement piqued my interest - I'll be the first to profess my
ignorance regarding encryption techniques - however, I'd love to learn more.
Would you mind if I asked you a couple of quick questions via email (they're
sort of off-topic for this newsgroup, I think)? If you don't have time for
that, I understand completely. However, if you wouldn't mind, please just
send a reply to my email address (by removing all hyphens from the reply-to
address in this post). Also, thank your response to this post!


Eric
 
Back
Top