Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
a problem with copymem API
Old
  (#1)
Antony Clements
Guest
 
Posts: n/a
Default a problem with copymem API - 06-04-2007, 10:35 AM

i am using the following code

private var1 as Long
private var2 as Long
private var3 as Long

ReDim bytearray(0 To 3)
ReDim bytearray2(0 To 3)

For a = 0 To 3
bytearray(a) = Asc(Mid(DataSource, b, 1))
bytearray2(a) = Asc(Mid(DataSource2, c, 1))
b = b + 1
c = c + 1
Next a

var1 = IVbytearray(0) & IVbytearray(1) & IVbytearray(2) & IVbytearray(3)
var2 = IVbytearray2(0) & IVbytearray2(1) & IVbytearray2(2) & IVbytearray2(3)

CopyMemory var1, var1 Xor var2, 4
CopyMemory var1, var1 Xor var3, 4

an overflow error is sometimes thrown up in the copymemory api, how do i fix
it?


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: a problem with copymem API
Old
  (#2)
J French
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

On Thu, 15 Feb 2007 10:27:25 +1100, "Antony Clements"
<EMAIL REMOVED> wrote:

>i am using the following code
>
>private var1 as Long
>private var2 as Long
>private var3 as Long
>
>ReDim bytearray(0 To 3)
>ReDim bytearray2(0 To 3)
>
>For a = 0 To 3
> bytearray(a) = Asc(Mid(DataSource, b, 1))
> bytearray2(a) = Asc(Mid(DataSource2, c, 1))
> b = b + 1
> c = c + 1
>Next a
>
>var1 = IVbytearray(0) & IVbytearray(1) & IVbytearray(2) & IVbytearray(3)
>var2 = IVbytearray2(0) & IVbytearray2(1) & IVbytearray2(2) & IVbytearray2(3)
>
>CopyMemory var1, var1 Xor var2, 4
>CopyMemory var1, var1 Xor var3, 4
>
>an overflow error is sometimes thrown up in the copymemory api, how do i fix
>it?


I can't make any sense of this code
- you don't use & for building a Long out of Bytes

If you described what you are trying to do, then maybe we can come up
with a suggestion.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#3)
Antony Clements
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

i eralilse what i said i'm trying to do is quite vague. the purpose of the
code is the core of a stream cipher where they key is defined as

key = var1 xor var2 xor var3 where var3 is (2147483647 * rnd). what happens
next is cipherbyte = chr((messagebyte xor key) mod 256)

>> Call CopyMemory(var1, var1 Xor var2, 8)
>> Call CopyMemory(var1, var1 Xor var3, 8)


does actually work as written.

the problem is that var1 and var2 are always the same constant no matter
what the source is.

"Dean Earley" <EMAIL REMOVED> wrote in message
news:45d438c9$0$22115$EMAIL REMOVED...
> Antony Clements wrote:
>> i modified the code after i posted this, it no longer throws up an error
>> but it still doesn't work correctly.
>>
>> dim var1 var2 and var3 as long
>>
>> ReDim IVbytearray(0 To 7)
>> ReDim IVbytearray2(0 To 7)

>
> These are redundant as they are replaced in the next block.
>
>> bytearray() = Mid(stream1, b, 8)
>> bytearray2() = Mid(stream2, c, 8) 'a messagebox here proves that at this
>> point, the data held in bytearray() is different than the data held in
>> bytearray2()

>
> This copies a UNICODE string into the byte array.
> This will be 16 bytes, every other one being a null character.
>
> If you want each character as an item in the array, then you want:
> bytearray = StrConv(string, vbFromUnicode)
>
>> var1 = bytearray()
>> var2 = bytearray2() 'this is where the problem is as var2 returns the
>> same value as var1 even though the value in bytearray() is different to
>> the value in bytearray2().

>
> From the code below, you can't join them up like this.
> You will need to do the arithmetic manually (shifting and adding) or
> copymemory from the byte array to a long.
> Bear in mind that you can only fit 4 bytes in a long...
>
>> Call CopyMemory(var1, var1 Xor var2, 8)
>> Call CopyMemory(var1, var1 Xor var3, 8)

>
> Be VERY careful as longs are still only 4 bytes.
> you WILL be corrupting memory with this code.
>
>> now here is the problem. even though the data source for bytearray() and
>> bytearray2() are different, for some reason or other they both have the
>> same value. var2 needs to be a value from the second data source. i hope
>> this is a clear indication of what i am trying to do.

>
> It still doesn't explain what you have and what results you want at the
> end...
>
> --
> Dean Earley (EMAIL REMOVED)
> i-Catcher Development Team
>
> iCode Systems



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#4)
J French
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

On Fri, 16 Feb 2007 09:46:04 +1100, "Antony Clements"
<EMAIL REMOVED> wrote:

>if stream1 has a value of 1a2b3c4d i want bytearray() to return the asc
>value of the string. the only way i know how to do this is take each
>character convert to ascii and then place in a string and then place the
>string in bytearray(). i'm looking for a simpler way. var1 and var2 are
>always returning the value of 1306840 regardless. bytearray() will return
>1a2b3c4d. i am copying the bytearray variables to long variables. the
>problem will be solved once var1 and var2 are correct values.


I still don't follow what you are trying to do.

It looks as if you want to compute some sort of 32 bit number from an
8 character String.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#5)
Dean Earley
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

Antony Clements wrote:
> if stream1 has a value of 1a2b3c4d i want bytearray() to return the asc
> value of the string. the only way i know how to do this is take each
> character convert to ascii and then place in a string and then place the
> string in bytearray().


As I said...

> If you want each character as an item in the array, then you want:
> bytearray = StrConv(string, vbFromUnicode)


From a string of "1a2b3c4d", this will give you an 8 item array
containing: 49, 97, 50, 98, 51, 99, 52, 100,

IF however, you want the 32bit equivalent of that hex encoded string (if
it is hex) then you will need to manually split it on every two
characters and p*** it through val("&H" & hexvalue) and store each part
in the array.

--
Dean Earley (EMAIL REMOVED)
i-Catcher Development Team

iCode Systems
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#6)
Antony Clements
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

> It looks as if you want to compute some sort of 32 bit number from an
> 8 character String.


the numbers i am trying to generate from the string has a minimum value of
00000000 and a maximum of 255255255255255255255255 wich are the ascii codes
of 8 characters.

the block of code works except for those 2 little lines that don't return
correct values. no data type that i know of will support such a large number
but it is critical that the code does, and i wouldnt know where to begin to
write a type def that will support such a large number either.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#7)
Antony Clements
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

stream1 is a pseudorandom string that can containing every character
available within the ansi standard with an undefined length. stream2 has a
defined length of 512 bits but can still contain any combination of
characters within the full ansi standard. that is characters with an ascii
code of 0 to 255. this means that var1 and var2 must hold a maximum number
of 255255255255. no data type that i know of will ever hold such a large
number except maybe a type def variable. but i wouldnt know where to begin
writing one to hold such a large number. i don't think converting from
unicode to ansi will have any positive effect on the rest of the code.

"Dean Earley" <EMAIL REMOVED> wrote in message
news:45d5750b$0$32030$EMAIL REMOVED...
> Antony Clements wrote:
>> if stream1 has a value of 1a2b3c4d i want bytearray() to return the asc
>> value of the string. the only way i know how to do this is take each
>> character convert to ascii and then place in a string and then place the
>> string in bytearray().

>
> As I said...
>
> > If you want each character as an item in the array, then you want:
> > bytearray = StrConv(string, vbFromUnicode)

>
> From a string of "1a2b3c4d", this will give you an 8 item array
> containing: 49, 97, 50, 98, 51, 99, 52, 100,
>
> IF however, you want the 32bit equivalent of that hex encoded string (if
> it is hex) then you will need to manually split it on every two characters
> and p*** it through val("&H" & hexvalue) and store each part in the array.
>
> --
> Dean Earley (EMAIL REMOVED)
> i-Catcher Development Team
>
> iCode Systems



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#8)
Antony Clements
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

correction of mistake. i am only grabbing 4 bytes of both strings instead of
the original 8 and am trying to generate a number between 0 and 255255255255
from each 4 byte block of string
"J French" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED...
> On Fri, 16 Feb 2007 09:46:04 +1100, "Antony Clements"
> <EMAIL REMOVED> wrote:
>
>>if stream1 has a value of 1a2b3c4d i want bytearray() to return the asc
>>value of the string. the only way i know how to do this is take each
>>character convert to ascii and then place in a string and then place the
>>string in bytearray(). i'm looking for a simpler way. var1 and var2 are
>>always returning the value of 1306840 regardless. bytearray() will return
>>1a2b3c4d. i am copying the bytearray variables to long variables. the
>>problem will be solved once var1 and var2 are correct values.

>
> I still don't follow what you are trying to do.
>
> It looks as if you want to compute some sort of 32 bit number from an
> 8 character String.
>
>



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#9)
Dean Earley
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

Antony Clements wrote:
> stream1 is a pseudorandom string that can containing every character
> available within the ansi standard with an undefined length. stream2
> has a defined length of 512 bits but can still contain any
> combination of characters within the full ansi standard. that is
> characters with an ascii code of 0 to 255. this means that var1 and
> var2 must hold a maximum number of 255255255255.


So you want ascii value string concatenation rather than just the
decimal value of the 4 byte string?

If this really is the case (I don't believe it is) then use the strconv
code I gave you and join the byte array items together in a string and
convert the result to a number rather than copymemory into a long.

> no data type that i know of will ever hold such a large number except
> maybe a type def variable. but i wouldnt know where to begin writing
> one to hold such a large number.


The currency data type will

> i don't think converting from unicode to ansi will have any positive
> effect on the rest of the code.


Yes it will, it will give you the real ascii values in a byte array
rather than utf16 unicode in a byte array.

You seem to be missing the point by a very long way here.

What EXACTLY do you have as input, what do you expect the intermediary
and final values to be?

--
Dean Earley (EMAIL REMOVED)
i-Catcher Development Team

iCode Systems
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: a problem with copymem API
Old
  (#10)
Antony Clements
Guest
 
Posts: n/a
Default Re: a problem with copymem API - 06-04-2007, 10:35 AM

> So you want ascii value string concatenation rather than just the
> decimal value of the 4 byte string?


i would prefer the decimal value, but the decimal value remains constant
regardless of what 4 bytes i pull from the streams, it is always 1306840

> The currency data type will


i tried currency, it threw up an error

> Yes it will, it will give you the real ascii values in a byte array
> rather than utf16 unicode in a byte array.
>
> You seem to be missing the point by a very long way here.
>
> What EXACTLY do you have as input, what do you expect the intermediary
> and final values to be?


i gave you an example for an input from stream2 "1a2b" which is input into a
bytearray, it is meant to return a dynamic number that changes as input
changes not a constant. even if the input from the stream was "help" it
still returns the same decimal value of 1306840. even if the input is 8
bytes "1a2b3c4d" it still returns the exact same value of 1306840. the point
i am making is that it doesn't matter what the input is, or how long the
input is, i get the same value from both streams regardless. the number that
gets returned from stream1 and stream2 must always be different depending on
the input.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top