Bit operations in ASP

Discussion in 'ASP General' started by Delphido, Aug 14, 2006.

  1. Delphido

    Delphido Guest

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

    ++++++++++++++++++++++++++++++++++++++
     
    Delphido, Aug 14, 2006
    #1
    1. Advertising

  2. 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"
     
    Bob Barrows [MVP], Aug 14, 2006
    #2
    1. Advertising

  3. Delphido

    Delphido Guest

    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"
    >
    >
    >
     
    Delphido, Aug 14, 2006
    #3
  4. "Delphido" <> wrote in message
    news:D...
    > 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"
    >>
    >>
    >>
     
    Mark J. McGinty, Aug 14, 2006
    #4
  5. 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.
     
    Dave Anderson, Aug 14, 2006
    #5
  6. "Dave Anderson" <> wrote in message
    news:...
    > 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.
    >
     
    Mark J. McGinty, Aug 14, 2006
    #6
  7. "Delphido" <> wrote in message
    news:...
    > 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.
     
    Anthony Jones, Aug 14, 2006
    #7
  8. 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.
     
    Dave Anderson, Aug 15, 2006
    #8
  9. "Dave Anderson" <> wrote in message
    news:...
    > 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.
    >
     
    Mark J. McGinty, Aug 16, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sanket Suryawanshi

    bit operations in Java

    Sanket Suryawanshi, Jan 23, 2004, in forum: Java
    Replies:
    3
    Views:
    7,082
    Jon A. Cruz
    Jan 23, 2004
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,271
    robert
    Feb 11, 2006
  3. Replies:
    3
    Views:
    1,776
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    989
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    183
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page