Error 6, Overflow in VBA code

P

pcc DaveF

I am attempting a series of XOR's on LONG variables, but often times get a
runtime error 6 Overflow.

This is in an attempt to covert an encryption algorithm written in
javascript to VBA so I can encrypt / decrypt select data in select tables.

I have been looking for some time on what might be causing the problem but
the documentation on exceptions / errors seems to be scarce.

Any help is greatly appreciated -

Dave
 
S

Steve Rindsberg

Pcc DaveF said:
I am attempting a series of XOR's on LONG variables, but often times get a
runtime error 6 Overflow.

This is in an attempt to covert an encryption algorithm written in
javascript to VBA so I can encrypt / decrypt select data in select tables.

I have been looking for some time on what might be causing the problem but
the documentation on exceptions / errors seems to be scarce.

Any help is greatly appreciated -

Without seeing the code or the values that cause the overflow, all anyone can
suggest is "Fix the code or don't feed it values that make it go urp". ;-)

Post some code, add error trapping so you know what specific values cause it to
err and include that information too.
 
P

pcc DaveF

Thanks.

Sheepishly provided (as I should have done in my first post) is the code
that makes Access hiccup:

This is the primary code of the function that is called from a form. A
string to encrypt (sInput) is passed:

' if the string length is not a multiple of 8
' then pad with spaces
While (Len(sInput) And &H7)
sInput = sInput & Chr(32)
Wend

While i <= Len(sInput)
' VB long data type is a 32 bit integer (4 byte)
' put numeric representation of first 4 characters of the input
string in p1D
i = i + 1
p1D = Asc(Mid(sInput, i, 1))
i = i + 1
p1D = p1D Or lShift(Asc(Mid(sInput, i, 1)), 8)
i = i + 1
p1D = p1D Or lShift(Asc(Mid(sInput, i, 1)), 16)
i = i + 1
p1D = p1D Or lShift(Asc(Mid(sInput, i, 1)), 24)

' put numeric representation of next 4 characters of the input
string in p2D
i = i + 1
p2D = Asc(Mid(sInput, i, 1))
i = i + 1
p2D = p2D Or lShift(Asc(Mid(sInput, i, 1)), 8)
i = i + 1
p2D = p2D Or lShift(Asc(Mid(sInput, i, 1)), 16)
i = i + 1
p2D = p2D Or lShift(Asc(Mid(sInput, i, 1)), 24)

aReturn = encipher(p1D, p2D, aKey) ' aKey is an arrray of long
previously
'
populated
'
aReturn is an array of long

sOutput = sOutput & "" & decToHex(aReturn(1)) & decToHex(aReturn(2))
Wend


The encipher function looks like this:

sum = 0
n = 32 ' number of iterations
temp(0) = -1 ' temp is an array of Long
temp(1) = p1
temp(2) = p2

While n > 0
i1 = lShift(temp(2), 4) ' rShift and lShift are utility
functions
i2 = rShift(temp(2), 5, False) ' that perform a right and left
bitwise shift
i3 = i1 Xor i2 ' <-- this is where we bomb on
virtually all inputs
i4 = temp(2) Xor sum
i5 = k(sum And 3)
temp(1) = temp(1) + i3 + i4 + i5
sum = sum + delta ' delta is a constant with value &H9E3779B9
temp(2) = temp(2) + ((lShift(temp(1), 4) Xor rShift(temp(1), 5,
False)) + (temp(1) Xor sum) + k(rShift(sum, 11, False) And 3))
n = n - 1
Wend

encipher = temp

On test data, such as a single character 't', I get the following values
passed INTO the encrypt function:

p1 = 538976372
p2 = 538976288

The intermediate values i1 and i2 calculated are:

i1 = 8623620608
i2 = 21559051

the value of i1 XOR i2 results in an overflow

This is a convoluted, but I hope someone can decipher it and come up with a
reason.

Thanks much -
Dave
 
S

Steve Rindsberg

One thing right off:
i1 = 8623620608

What's i1 dimensioned as?

A long can't be > 2,147,483,647

XORing two Longs should yield a Long as well, so if the result is too large, an
overflow would result.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top