469,927 Members | 1,727 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Bit operations in ASP

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;
}

++++++++++++++++++++++++++++++++++++++
Aug 14 '06 #1
8 7263
Delphido wrote:
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?
--
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"
Aug 14 '06 #2
Stupid me, in vbscript...

"Bob Barrows [MVP]" wrote:
Delphido wrote:
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?
--
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"
Aug 14 '06 #3

"Delphido" <De******@discussions.microsoft.comwrote in message
news:D8**********************************@microsof t.com...
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

"Bob Barrows [MVP]" wrote:
>Delphido wrote:
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?
--
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"

Aug 14 '06 #4
Mark J. McGinty wrote:
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.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Aug 14 '06 #5

"Dave Anderson" <NY**********@spammotel.comwrote in message
news:ef**************@TK2MSFTNGP04.phx.gbl...
Mark J. McGinty wrote:
>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.
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

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message.
Use of this email address implies consent to these terms.

Aug 14 '06 #6

"Delphido" <De******@discussions.microsoft.comwrote in message
news:BE**********************************@microsof t.com...
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.

Aug 14 '06 #7
Mark J. McGinty wrote:
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.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Aug 15 '06 #8

"Dave Anderson" <NY**********@spammotel.comwrote in message
news:Ou**************@TK2MSFTNGP06.phx.gbl...
Mark J. McGinty wrote:
>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.
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
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message.
Use of this email address implies consent to these terms.

Aug 16 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leslaw Bieniasz | last post: by
3 posts views Thread by Scott Brady Drummonds | last post: by
3 posts views Thread by ThunderMusic | last post: by
17 posts views Thread by Chad Myers | last post: by
13 posts views Thread by Immanuel Goldstein | last post: by
3 posts views Thread by Hallvard B Furuseth | last post: by
6 posts views Thread by carsonbj | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.