Bit operations in ASP

D

Delphido

Hello,

i've been programming ASP for a few years, but this time i'm lost..
I need to translate a piece of C++ code which encrypts a string into the ASP
equivalent. Can anybody give me a had with this. I have nog clue about
bit-operations etc, and don't no to much about C++ as well..

+++++++++++++++ C++ code +++++++++++++++

rstring Encrypt_String( const rstring& OrgString, const rstring& KeyString)
{
rstring Result;
unsigned char* pKey;
unsigned char Val;
unsigned char ValRes;
int LenOrg = OrgString.GetLength();
int LenKey = KeyString.GetLength();

const char* pConvert = OrgString;

for( int i=0; i< LenOrg; ++i, ++pConvert)
{
Val = *pConvert ^ KeyString[ i % LenKey];
ValRes = 'a' + ((Val >> 4) & 0x0F);
Result.add( (const char*)&ValRes, 1);
ValRes = 'a' + (Val & 0x0F);
Result.add( (const char*)&ValRes, 1);
}
return Result;
}

++++++++++++++++++++++++++++++++++++++
 
B

Bob Barrows [MVP]

Delphido said:
Hello,

i've been programming ASP for a few years, but this time i'm lost..
I need to translate a piece of C++ code which encrypts a string into
the ASP equivalent.


I don't read C++, but you need to clarify what scripting language you wish
to do this in: vbscript? jscript? something else?
 
D

Delphido

Stupid me, in vbscript...

Bob Barrows said:
I don't read C++, but you need to clarify what scripting language you wish
to do this in: vbscript? jscript? something else?
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
 
M

Mark J. McGinty

Delphido said:
Stupid me, in vbscript...

In VBScript:

the modulo operator (% in C++) is MOD
the bitwise AND operator (& in C++) is AND
the XOR operator (^ in C++) is XOR

VBScript has no bitwise shift operators, however, dividing by 16 is the same
as right-shifting 4 bits (>> 4)

I didn't closely analyze the C++ code, but porting it may be non-trivial,
due to lack of unsigned types in VBS, and other VBS features/quirks that
protect the machine from the programmer. You should test the ported code
with a wide range of values to insure the same output.

It might be easier/faster/more reliable to write a COM object in C++ to do
the work. I'm not sure what an rstring is, you'd likely need to change that
to a BSTR, and convert from Unicode to ANSI, but the bitwise logic would
remain intact.

If you have the option, server-side JScript would be an easier port, since
its operator set mirrors C++, but the sign bit will still be an issue.

Good luck!


-Mark
 
D

Dave Anderson

Mark said:
VBScript has no bitwise shift operators, however, dividing by
16 is the same as right-shifting 4 bits (>> 4)

Integer division: Val\16 instead of Val/16.
 
M

Mark J. McGinty

Dave Anderson said:
Integer division: Val\16 instead of Val/16.

Indeed, thanks for clarifying.

Looking a little more closely at the OP's C++ code, it shouldn't be too
challenging, as long as no character in either the OrigString or the
KeyString has its high-order bit set, which would cause sign bit extension
in the result of the XOR op (unless of course, by chance, the high-order bit
was set in both operands) that could skew bitwise math with 8 bit value
expectations.

That *appears* to be the only danger spot -- though impossible to tell
without seeing the decrypt function as well.

-Mark
 
A

Anthony Jones

Delphido said:
Hello,

i've been programming ASP for a few years, but this time i'm lost..
I need to translate a piece of C++ code which encrypts a string into the ASP
equivalent. Can anybody give me a had with this. I have nog clue about
bit-operations etc, and don't no to much about C++ as well..

+++++++++++++++ C++ code +++++++++++++++

rstring Encrypt_String( const rstring& OrgString, const rstring& KeyString)
{
rstring Result;
unsigned char* pKey;
unsigned char Val;
unsigned char ValRes;
int LenOrg = OrgString.GetLength();
int LenKey = KeyString.GetLength();

const char* pConvert = OrgString;

for( int i=0; i< LenOrg; ++i, ++pConvert)
{
Val = *pConvert ^ KeyString[ i % LenKey];
ValRes = 'a' + ((Val >> 4) & 0x0F);
Result.add( (const char*)&ValRes, 1);
ValRes = 'a' + (Val & 0x0F);
Result.add( (const char*)&ValRes, 1);
}
return Result;
}

++++++++++++++++++++++++++++++++++++++

This is a best guess:-

Function EncryptString(PlainText, KeyText)

Dim lKeyLen: lKeyLen = Len(KeyText)
Dim i
Dim bytA : bytA = AscB("a")
Dim bytVal
Dim asRes: ReDim asRes(Len(PlainText)*2)

For i = 1 To Len(PlainText)
bytVal = AscB(Mid(PlainText, i, 1)) Xor AscB(Mid(KeyText, (i Mod
lKeyLen)+1,1))
asRes((i-1)*2) = Chr(bytA + (bytVal \ 16))
asRes((i-1)*2+1) = Chr(bytA + (bytVal And &h0F))
Next

EncryptString = Join(asRes,"")

End Function

Function DecryptString(CryptText, KeyText)

Dim lKeyLen: lKeyLen = Len(KeyText)
Dim i
Dim bytA : bytA = AscB("a")
Dim bytVal
Dim asRes: ReDim asRes(Len(CryptText) \ 2)

For i = 2 To Len(CryptText) Step 2
bytVal = (AscB(Mid(CryptText, i - 1, 1)) - bytA) * 16
bytVal = bytVal Or ((AscB(Mid(CryptText, i, 1)) - bytA) And &H0F)
bytVal = bytVal Xor AscB(Mid(KeyText, ((i \ 2) Mod lKeyLen)+1,1))
asRes(i \ 2) = Chr(bytVal)
Next

DecryptString = Join(asRes,"")

End Function

Works fine if these matching functions are used. However if you have the
C++ code on the decrypt end then it should still work for purely ASCII codes
but extended ANSI characters may have trouble passing through and you can
forget about the full extended unicode character set.
 
D

Dave Anderson

Mark said:
That *appears* to be the only danger spot -- though impossible
to tell without seeing the decrypt function as well.

It isn't clear to me that you can perform integer arithmetic on strings in
VBScript, which was what I thought it was going to be used for.
 
M

Mark J. McGinty

Dave Anderson said:
It isn't clear to me that you can perform integer arithmetic on strings in
VBScript, which was what I thought it was going to be used for.

One character at a time, using Asc() and Chr()... <shrug>? Should be
possible as long as all calc results are kept inside of 7 bits -- or both
encrypt and decrypt functions are written in VBS.

In C++ elements in a character array aren't assumed to be part of a text
string until output/display time; defining a character in single quotes is
merely a notation option, 'a', 97 and 0x61 are binary equivilents, and
equally legal rvalues when assigning a char.

But in VBS, character data isn't quite so unfettered, string assumptions are
imposed by notation, thus the need to "convert" a character back to what it
actually already is, to act on it arithmetically. :)


-Mark
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top