Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Bit operations in ASP

Reply
Thread Tools

Bit operations in ASP

 
 
Delphido
Guest
Posts: n/a
 
      08-14-2006
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;
}

++++++++++++++++++++++++++++++++++++++
 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      08-14-2006
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"


 
Reply With Quote
 
 
 
 
Delphido
Guest
Posts: n/a
 
      08-14-2006
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"
>
>
>

 
Reply With Quote
 
Mark J. McGinty
Guest
Posts: n/a
 
      08-14-2006

"Delphido" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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"
>>
>>
>>



 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      08-14-2006
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.


 
Reply With Quote
 
Mark J. McGinty
Guest
Posts: n/a
 
      08-14-2006

"Dave Anderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      08-14-2006

"Delphido" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      08-15-2006
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.


 
Reply With Quote
 
Mark J. McGinty
Guest
Posts: n/a
 
      08-16-2006

"Dave Anderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
"LoadLibrary" of a 32 bit so with 64 bit java on a 64 bit machine markryde@gmail.com Java 3 01-19-2007 10:30 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments