Hey guys
Anyone still around here?
I have an issue that's really been messing with my head!
I have a ready-made function for RC4 encryption.
Then it converted to Hex for storing in an SQL db (see below).
The very weird thing, is that when in the order process, the
encryption/decrypt process is perfect.
But when I access the same string, using the same functions, with the same
key, via an admin interface I built the credit card info that spits out - is
wrong. There is some kind of a corruption. I have managed to find a pattern
where things are corrupt in the hex-based encrypted string, and can fix it,
But I'm really at a loss why the same string, same functions and same key
can give different results.
For example, when I display the open full CC number in the admin site, i
get: 11&11111&&111&16
but the correct number converted using the same functions and stored
directly in the database, is: 1111111111111111 (and this is viewable during
the actual order process too, when we confirm payment details after
encryption).
Does anyone have any ideas why this might happen?
Thanks!
Moshe
'==========================================================
CODE:
ENCRYPTION CODE: EncryptedCCnumber =
Ascii2Hex(EnDeCrypt(FullCCnumber,mykey))
DECRYPTION CODE: FullCCnumber =
EnDeCrypt(Hex2Ascii(EncryptedCCnumber),mykey)
'FUNCTIONS:
Function EnDeCrypt(plaintxt, psw)
'If plaintext is empty, return Empty String
if isEmpty(plaintxt) or isNull(plaintxt) or plaintxt = "" then
EnDeCrypt = ""
exit function
end if
'Declare Variables
dim sbox(255), key(255)
dim temp, tempSwap, intLength
dim a, b, i, j, k
dim cipherby, cipher
'Initialize some variables
b = 0
i = 0
j = 0
'Initialize sbox and key array
intLength = len(psw)
For a = 0 To 255
key(a) = asc(mid(psw, (a mod intLength)+1, 1))
sbox(a) = a
next
For a = 0 To 255
b = (b + sbox(a) + key(a)) Mod 256
tempSwap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempSwap
Next
'Encrypt/Decrypt text
For a = 1 To Len(plaintxt)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)
cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
cipher = cipher & Chr(cipherby)
Next
EnDeCrypt = cipher
End Function
'******************************************************************
'Convert a String to Hex values
'******************************************************************
function Ascii2Hex(strTemp)
if strTemp = "" or isNull(strTemp) then
Ascii2Hex = ""
else
dim I
for I = 1 to len(strTemp)
Ascii2Hex = Ascii2Hex & right("00" &
hex(asc(mid(strTemp,I,1))),2)
next
end if
end function
'******************************************************************
'Convert a Hex values to String
'******************************************************************
function Hex2Ascii(strTemp)
if strTemp = "" or isNull(strTemp) then
Hex2Ascii = ""
else
dim I
for I = 1 to len(strTemp) step 2
Hex2Ascii = Hex2Ascii & Chr(Eval("&H" &
Mid(strTemp,I,2)))
next
end if
end function