Announcement

Collapse
No announcement yet.

C/C++ Quick Conversion help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • C/C++ Quick Conversion help

    Hi there.

    It's been awhile since I programmed, I'm working on something small and need help to narrow down my error in this conversion. Hoping someone with more experience can help. Have not posted code on this forum for a long time, not sure how to get the code to format correctly... sorry!

    Many Thanks!
    best regards
    Jules

    converted code...

    Code:
    '##############################################################################
    '################### START OF KEELOQ KEYGEN ###################################
    '##############################################################################
    
    %KeeLoq_NLF = &H3A5C742E      'constant
    
    GLOBAL Key_Low    AS LONG     'return values after keygen called
    GLOBAL Key_High   AS LONG     'return values
    
    '------------------------------------------------------------------------------
    '
    '------------------------------------------------------------------------------
    SUB keygen( BYVAL sn AS LONG )
    
    LOCAL SerialNum AS LONG '// 28 bits effective
    'LOCAL keyLow AS LONG : keyLow = &H49543942
    'LOCAL keyHigh AS LONG : keyHigh = &H554d495f
    
    SerialNum = sn AND &H0fffffff
    SerialNum = SerialNum OR &H20000000
    
    Key_Low = decrypt(SerialNum)
    
    SerialNum = sn AND &H0fffffff
    SerialNum = SerialNum OR( &H60000000)
    
    Key_High = decrypt(SerialNum)
    
    END SUB
    
    '------------------------------------------------------------------------------
    'Description: Reads a bit of a number.
    ' Parameters: x: the number from which to read
    ' n: which bit to read,starting at 0 for the LSB (rightmost) bit
    
    '------------------------------------------------------------------------------
    FUNCTION bitRead(BYVAL b AS DWORD,BYVAL bitPos AS LONG) AS LONG
    LOCAL x AS LONG
    '(1 << bitPos)
    'x = b AND bitPos
    FUNCTION = BIT(b, bitPos) 'Return the value of the bit (0 or 1)
    END FUNCTION
    
    
    '------------------------------------------------------------------------------
    '
    '------------------------------------------------------------------------------
    FUNCTION decrypt(BYVAL xdata AS LONG) AS LONG
    
    LOCAL keyLow AS LONG : keyLow = &H49543942
    LOCAL keyHigh AS LONG : keyHigh = &H554d495f
    LOCAL x AS LONG : x = xdata
    LOCAL r AS LONG
    LOCAL keyBitNo AS INTEGER
    LOCAL index AS INTEGER
    LOCAL keyBitVal AS LONG
    LOCAL bitVal AS LONG
    
    
    FOR r = 0 TO 528 'for (r=0; r < 528; r++)
    keyBitNo = (15-r) AND 63
    IF(keyBitNo < 32) THEN
    keyBitVal = bitRead(keyLow,keyBitNo)
    ELSE
    keyBitVal = bitRead(keyHigh, keyBitNo - 32)
    index = 1*bitRead(x,0)+2*bitRead(x,8)+4*bitRead(x,19)+8*bitRead(x,25)+16*bitRead(x,30)
    bitVal = bitRead(x,31)XOR bitRead(x, 15) XOR bitRead(%KeeLoq_NLF,index) XOR keyBitVal
    SHIFT LEFT x,1 XOR bitVal
    END IF
    NEXT r
    
    FUNCTION = x
    
    END FUNCTION
    '######################## END OF KEELOQ KEYGEN ################################


    The original C/C++ code...

    Code:
    define KeeLoq_NLF 0x3A5C742EUL
    unsigned long Key_Low;
    unsigned long Key_High;
    unsigned long SerialNum; // 28 bits effective
    
    unsigned long Keeloq::decrypt(unsigned long data)
    {
    unsigned long _keyLow;
    unsigned long _keyHigh;
    unsigned long x = data;
    unsigned long r;
    int keyBitNo, index;
    unsigned long keyBitVal,bitVal;
    
    _keyLow = 0x49543942;
    _keyHigh = 0x554d495f;
    
    for (r = 0; r < 528; r++)
    {
    keyBitNo = (15-r) & 63;
    if(keyBitNo < 32)
    keyBitVal = bitRead(_keyLow,keyBitNo);
    else
    keyBitVal = bitRead(_keyHigh, keyBitNo - 32);
    index = 1 * bitRead(x,0) + 2 * bitRead(x,8) + 4 * bitRead(x,19) + 8 * bitRead(x,25) + 16 * bitRead(x,30);
    bitVal = bitRead(x,31) ^ bitRead(x, 15) ^ bitRead(KeeLoq_NLF,index) ^ keyBitVal;
    x = (x<<1) ^ bitVal;
    }
    return x;
    }
    
    
    
    void keygen(unsigned long sn)
    {
    _keyLow = 0x49543942;
    _keyHigh = 0x554d495f;
    
    SerialNum = sn & 0x0fffffff;
    SerialNum |= 0x20000000;
    
    Key_Low = decrypt(SerialNum);
    
    SerialNum = sn & 0x0fffffff;
    SerialNum |= 0x60000000;
    
    Key_High = decrypt(SerialNum);
    }
    Best regards
    Jules
    www.rpmarchildon.com

  • #2
    Hey Jules,
    So good to see you around. :-)

    You did a great job, this might add a little...

    Code:
    #COMPILE EXE '#CC 5.07#
    #DIM ALL
    '_____________________________________________________________________________
    
    FUNCTION DeCrypt(BYVAL dwData AS DWORD) AS DWORD
     LOCAL KeyLow     AS DWORD
     LOCAL KeyHigh    AS DWORD
     LOCAL x          AS DWORD
     LOCAL Looper     AS DWORD
     LOCAL KeyBitVal  AS DWORD
     LOCAL BitVal     AS DWORD
     LOCAL KeeLoq_NLF AS DWORD
     LOCAL KeyBitNo   AS LONG
     LOCAL index      AS LONG
    
     x          = dwData
     KeeLoq_NLF = &H3A5C742E???
     KeyLow     = &H49543942???
     KeyHigh    = &H554D495F???
    
     FOR Looper = 0 TO 527
       KeyBitNo = (15 - Looper) AND 63
       IF KeyBitNo < 32 THEN
         KeyBitVal = BIT(KeyLow, KeyBitNo)
       ELSE
         KeyBitVal = BIT(KeyHigh, KeyBitNo - 32)
       END IF
       index = 1 * BIT(x, 0) + 2 * BIT(x, 8) + 4 * BIT(x, 19) + 8 * BIT(x, 25) + 16 * BIT(x, 30)
       BitVal = BIT(x, 31) XOR BIT(x, 15) XOR BIT(KeeLoq_NLF, index) XOR KeyBitVal
       SHIFT LEFT x, 1
       x = x XOR BitVal
     NEXT
     FUNCTION = x
    
    END FUNCTION
    '_____________________________________________________________________________
    
    SUB Keygen(BYVAL sn AS DWORD)
     LOCAL SerialNum AS DWORD '28 bits effective
     LOCAL KeyLow    AS DWORD
     LOCAL KeyHigh   AS DWORD
    
     SerialNum = sn AND &H0FFFFFFF???
     SerialNum = SerialNum OR &H20000000???
     KeyLow    = DeCrypt(SerialNum)
     PRINT "KeyLow  0x" & HEX$(KeyLow) & " ~ (Expected 0xF1E85123)"
    
     SerialNum = sn AND &H0FFFFFFF???
     SerialNum = SerialNum OR &H60000000???
     KeyHigh   = DeCrypt(SerialNum)
     PRINT "KeyHigh 0x" & HEX$(KeyHigh) & " ~ (Expected 0x111C7C91)"
    
    END SUB
    '_____________________________________________________________________________
    
    FUNCTION PBMAIN()
     LOCAL sn AS DWORD
    
     sn = 65537 '&H00010001
     Keygen(sn)
    
     PRINT : PRINT "Press any Key or click to continue..." : MOUSE ON : MOUSE 3, UP : WAITKEY$
    
    END FUNCTION
    '_____________________________________________________________________________
    '
    'C++ code: #include <windows.h>
    'C++ code: #include <iostream>
    'C++ code: using namespace std;
    'C++ code:
    'C++ code: #define KeeLoq_NLF 0x3A5C742EUL
    'C++ code: unsigned long Key_Low;
    'C++ code: unsigned long Key_High;
    'C++ code: unsigned long SerialNum; // 28 bits effective
    'C++ code:
    'C++ code: bool BitRead(unsigned long dwVal, int position) // position in range 0-7
    'C++ code: {
    'C++ code:     return (dwVal >> position) & 0x1;
    'C++ code: }
    'C++ code:
    'C++ code: unsigned long DeCrypt(unsigned long data) // unsigned long Keeloq::DeCrypt(unsigned long data)
    'C++ code: {
    'C++ code:   unsigned long _KeyLow;
    'C++ code:   unsigned long _KeyHigh;
    'C++ code:   unsigned long x = data;
    'C++ code:   unsigned long r;
    'C++ code:   int KeyBitNo, index;
    'C++ code:   unsigned long KeyBitVal,BitVal;
    'C++ code:
    'C++ code:   _KeyLow = 0x49543942;
    'C++ code:   _KeyHigh = 0x554d495f;
    'C++ code:
    'C++ code:   for (r = 0; r < 528; r++)
    'C++ code:   {
    'C++ code:     KeyBitNo = (15-r) & 63;
    'C++ code:     if(KeyBitNo < 32)
    'C++ code:       KeyBitVal = BitRead(_KeyLow,KeyBitNo);
    'C++ code:     else
    'C++ code:       KeyBitVal = BitRead(_KeyHigh, KeyBitNo - 32);
    'C++ code:
    'C++ code:     index = 1 * BitRead(x,0) + 2 * BitRead(x,8) + 4 * BitRead(x,19) + 8 * BitRead(x,25) + 16 * BitRead(x,30);
    'C++ code:     BitVal = BitRead(x,31) ^ BitRead(x, 15) ^ BitRead(KeeLoq_NLF,index) ^ KeyBitVal;
    'C++ code:     x = (x<<1) ^ BitVal;
    'C++ code:   }
    'C++ code:   return x;
    'C++ code: }
    'C++ code:
    'C++ code: void Keygen(unsigned long sn)
    'C++ code: {
    'C++ code:   //KeyLow = 0x49543942;
    'C++ code:   //KeyHigh = 0x554d495f;
    'C++ code:
    'C++ code:   SerialNum = sn & 0x0fffffff;
    'C++ code:   SerialNum |= 0x20000000;
    'C++ code:
    'C++ code:   Key_Low = DeCrypt(SerialNum);
    'C++ code:
    'C++ code:   SerialNum = sn & 0x0fffffff;
    'C++ code:   SerialNum |= 0x60000000;
    'C++ code:
    'C++ code:   Key_High = DeCrypt(SerialNum);
    'C++ code: }
    'C++ code:
    'C++ code:
    'C++ code: int main ()
    'C++ code: {
    'C++ code:   unsigned long sn = 123456;
    'C++ code:   Keygen(sn);
    'C++ code:   cout << "The sn value is " << sn << "\n";
    'C++ code:   cout << "The SerialNum value is " << SerialNum << "\n";
    'C++ code:
    'C++ code:   // The sn value is 123456
    'C++ code:   // The SerialNum value is 1610736192
    'C++ code:
    'C++ code:     while (GetKeyState(VK_SPACE) + GetKeyState(VK_XBUTTON1) >= 0)
    'C++ code:   {
    'C++ code:     Sleep(20);
    'C++ code:   }
    'C++ code:
    'C++ code:     return 0;
    'C++ code: }
    '_____________________________________________________________________________
    '
    Last edited by Pierre Bellisle; 7 Jun 2018, 10:23 PM.

    Comment


    • #3
      Hello Pierre, yeah, it's been long long long time for me. I've been lurking here and there. :-)

      Thank you for the look over! The two variables I need are the Key_High and Key_Low results from decrypt()

      I keep scanning the code for the error, it's just not clicking in my head, I keep getting Zero returned for both.

      for instance if I provide the facility code (1) and card key(4)
      my serial no. is 65537 ( '&H00010001 )

      the results should be:
      Key_High = &H111C7C91
      Key_Low = &HF1E85123

      Best regards
      Jules
      Best regards
      Jules
      www.rpmarchildon.com

      Comment


      • #4
        I got to go for now,
        with this supplemental infos,
        if nobody else does it before, I will see if I can do something...
        :-)

        Comment


        • #5
          Not as important, but also from post #1 -
          ...not sure how to get the code to format correctly...
          place the code tags

          [_CODE] | [_/CODE] ((no underscores in tags, no spaces between))(((they're just to make the tags show here.))) Can be typed or use large italic # icon first.

          With code to be pasted in clipboard do <CTL C> to paste. This will preserve the indent spacing.

          Putting the code in the Post Reply box first, then adding the code tags loses the indents.

          If copying from IDE, you can also preserve color. Highlight the code you want, then menu item Edit, Copy as BBCode (or Alt C) then paste. ((this includes code tags, so don't type them or use # first.)) (((note ALT C, not CTL C)))

          Cheers,
          Dale

          Comment


          • #6
            Hey Jules,

            There was some little issues and a big one, the big one is somewatt easy to find for a C coder, more difficult for a BASIC one.
            Since there are no curly brackets "{}" after the "else" in DeCrypt(),
            only the first line after this should be considered part of the if/else,
            like if an "END IF" was inserted after "KeyBitVal = BIT(KeyHigh, KeyBitNo - 32)".

            This done, I got the expected results...
            Code above is updated.

            Comment


            • #7
              Pierre, you are Genius!

              That works perfect. Thank you for spending the time to help, it is very much appreciated!!! This is for a in-house project that we re-wrote our own code for a water sensor that uses the MSP430 to adapt to our RF protocol on the receiver side for security suite panel. Now we can automate the programming rather than hard coding the keeloq into the image (TI-Text format).

              Thank you so much for your help!!
              Best regards
              Jules

              Best regards
              Jules
              www.rpmarchildon.com

              Comment


              • #8
                Great! I'm glad to help.
                Looking at your impressive RF TI MSP430, I see that a float switch is a little less sophisticated. :-)
                Wish you a lot of fun with the project...

                Comment


                • #9
                  We purchased these from Taiwan, not our design. WLS-15 433Mhz.
                  http://www.climax.com.tw/downloads/W...2017.07.28.pdf

                  Pierre, It's always what the customer wants whether they need it or not. Each Condo suite will use 3 of them, sends an alert to the Suite Security Panel, which is polled by the controller and reported to the Guard/Concierge server. Yes, it's one big float system! LOL

                  Dale, thank you for the tip! I forgot so much.

                  Best regards
                  Jules
                  Best regards
                  Jules
                  www.rpmarchildon.com

                  Comment

                  Working...
                  X