473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7582
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(ByVa l s1 As String, ByVal s2 As String) As
String
' length of the resulting string
Dim length As Integer = Math.Max(s1.Len gth, s2.Length)

' ensure both strings are the same length
s1 = s1.PadRight(len gth, Char.MinValue)
s2 = s2.PadRight(len gth, 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.Ch ars(index)) Xor
AscW(s2.Chars(i ndex)))
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(resu lt, 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.Enc oding to convert the string to a
byte array, then xor each byte in both arrays. Optionally you could use
System.Text.Enc oding 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.Enc oding 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.Enc oding 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******** ********@TK2MSF TNGP10.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******** ********@TK2MSF TNGP10.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******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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) misunderstandin g, 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******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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********@ema il.msn.com> wrote in message
news:%2******** *******@tk2msft ngp13.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******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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******@DONTW ANTSPAMmail.pt> wrote in message
news:%2******** *******@TK2MSFT NGP10.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) misunderstandin g, 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******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.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
8911
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) outputBank(0) = True outputBank(1) = False outputBank(2) = False
2
4697
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
9854
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 appreciate any assistance. Thank you, Laszlo
0
1162
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() endstructure dim Array() as stBarray so I can:
3
1765
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
2196
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; iArray.CopyTo( lByteArr, 0 );
4
3834
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 = 0xAA55; for (int i = 0 ; i<16 ; i++){
7
2410
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; using namespace System::Collections;
2
2834
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 ! but Xoring 3 with 2 give 2 which is correct... (should be 1) here is the sample code...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.