473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String to BitArray for XORing

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
Nov 20 '05 #1
7 7569
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

"Eric" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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

Nov 20 '05 #2
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.

On Sat, 18 Oct 2003 09:27:08 -0700, "Eric"
<e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote:
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


Nov 20 '05 #3
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" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #4
Eric,
Just remember once you put it in a String variable it is Unicode.

Hope this helps
Jay

"Eric" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #5
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
"Eric" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #6
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

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Eric,
Just remember once you put it in a String variable it is Unicode.

Hope this helps
Jay

"Eric" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #7
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

"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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
"Eric" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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" <e-r-i-c-@-e-j-p-r-o-d-u-c-t-i-o-n-s-.-c-o-m> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) ...
2
by: Søren Nøhr Christensen | last post by:
Hi all! I was wondering how much overhead was involved in using a bitarray. In other words, how much space does an array of say 32bit use? snc
3
by: Laszlo Szijarto | last post by:
In using the BitArray class to parse a single byte, I noticed that the bits show up in Little Endian order. Aren't bits always arranged in Big Endian order? I'm a bit confused and would...
0
by: James | last post by:
Hi, I need to use an array of bitarray and being able to dinamically redim the bitarray and the array of bitarray. I need something like: structure stBarray public BA as bitarray()...
3
by: James | last post by:
Hi, Does anyone knows if it is possible to create an Array of Bitarrays. In such a way that I can dinamically change the lenght of each bitarray and redim the array of bitarray???
3
by: HKannen | last post by:
Hello Everybody, I wrote a little method that gets me an Int32 from a BitArray. This method looks like this: private Int32 GetIntVal( BitArray iArray ) { Byte lByteArr = new Byte; ...
4
by: Rainer Queck | last post by:
Hi NG I have some questions concerning BitArrays. Assumption : BitArray with 16 Bits Is it possible to "load" a BitArray with a UInt16 Value with out iterating it like: UInt16 Bits =...
7
by: Rick Williams | last post by:
I was so happy to find the BitArray class. Until I 'cut and pasted' the following sample code from Visual Studio's help: #using <mscorlib.dll> #using <system.dll> using namespace System;...
2
by: semedao | last post by:
Hi , I try the BitArray class to make Xor on 2 byte arrays. The result was that when I Xor 1with 2 I get 3 which is correct then Xoring 3 with 1 give me 0 (zero) when it should give me 2 ! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.