Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. proper conversion of unsigned char [16] -> unsigned long [4] [modified]

proper conversion of unsigned char [16] -> unsigned long [4] [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saiyuk6 7
    wrote on last edited by
    #1

    I am stuck actually, im trying to write a md5 / blowfish function im not sure if i am correctly converting the MD5[16] buffer into a unsigned long[4] this doesnt really make too much sense too me

    typedef struct _tag2Long {
    unsigned long _1;
    unsigned long _2;
    unsigned long _3;
    unsigned long _4;
    } _2LONG;

    void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
    unsigned int iSize, unsigned long pSignature[4])
    {
    MD5_CTX md5;
    BLOWFISH_CTX bf;

    MD5Init(&md5);
    MD5Update(&md5, pbzBuffer, iSize);
    MD5Final(&md5);

    _2LONG *tl = (_2LONG*) md5.digest;

    printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

    Blowfish_Init(&bf, pbzKey, 16);
    Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
    Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);

    pSignature[0] = tl->_1;
    pSignature[1] = tl->_2;
    pSignature[2] = tl->_3;
    pSignature[3] = tl->_4;

    printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
    pSignature[2], pSignature[3]);
    }

    bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
    unsigned int iSize, unsigned long pBuffer[4])
    {
    BLOWFISH_CTX bf;
    MD5_CTX md5;
    bool bResult = false;

    printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
    pBuffer[2], pBuffer[3]);

    Blowfish_Init(&bf, pbzKey, 16);
    Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
    Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);

    printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
    pBuffer[2], pBuffer[3]);

    MD5Init(&md5);
    MD5Update(&md5, pSignature, iSize);
    MD5Final(&md5);

    _2LONG *tl = (_2LONG*) md5.digest;
    // printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

    if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
    tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
    bResult = true;

    return bResult;
    }

    void main(void)
    {
    unsigned char bszBuffer[2] = {'A', 'B'};
    unsigned long ulSig[4];
    SignBuffer((unsigned char*)"key", bszBuffer, 2, ulSig);
    printf("\n");
    if (VerifyBuffer((unsigned char*)"key", bszBuffer, 2, ulSig))
    printf("\nok");
    else printf("\nerr");
    }

    and this is the output of them

    BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
    BLE 2 f12b0ed9 782fae18

    C C 2 Replies Last reply
    0
    • S saiyuk6 7

      I am stuck actually, im trying to write a md5 / blowfish function im not sure if i am correctly converting the MD5[16] buffer into a unsigned long[4] this doesnt really make too much sense too me

      typedef struct _tag2Long {
      unsigned long _1;
      unsigned long _2;
      unsigned long _3;
      unsigned long _4;
      } _2LONG;

      void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
      unsigned int iSize, unsigned long pSignature[4])
      {
      MD5_CTX md5;
      BLOWFISH_CTX bf;

      MD5Init(&md5);
      MD5Update(&md5, pbzBuffer, iSize);
      MD5Final(&md5);

      _2LONG *tl = (_2LONG*) md5.digest;

      printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

      Blowfish_Init(&bf, pbzKey, 16);
      Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
      Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);

      pSignature[0] = tl->_1;
      pSignature[1] = tl->_2;
      pSignature[2] = tl->_3;
      pSignature[3] = tl->_4;

      printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
      pSignature[2], pSignature[3]);
      }

      bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
      unsigned int iSize, unsigned long pBuffer[4])
      {
      BLOWFISH_CTX bf;
      MD5_CTX md5;
      bool bResult = false;

      printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
      pBuffer[2], pBuffer[3]);

      Blowfish_Init(&bf, pbzKey, 16);
      Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
      Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);

      printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
      pBuffer[2], pBuffer[3]);

      MD5Init(&md5);
      MD5Update(&md5, pSignature, iSize);
      MD5Final(&md5);

      _2LONG *tl = (_2LONG*) md5.digest;
      // printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

      if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
      tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
      bResult = true;

      return bResult;
      }

      void main(void)
      {
      unsigned char bszBuffer[2] = {'A', 'B'};
      unsigned long ulSig[4];
      SignBuffer((unsigned char*)"key", bszBuffer, 2, ulSig);
      printf("\n");
      if (VerifyBuffer((unsigned char*)"key", bszBuffer, 2, ulSig))
      printf("\nok");
      else printf("\nerr");
      }

      and this is the output of them

      BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
      BLE 2 f12b0ed9 782fae18

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      the conversion looks fine. but doesn't Blowfish use 64-bit (8-byte) blocks ? so this Blowfish_Encrypt(&bf, &tl->_1, &tl->_2); will encrypt the 8 bytes starting at tl->_1 ? since _1 is only four bytes, it's going to run over into _2. so, you're doing BF(_1 + _2) -> _2 + _3. or maybe not. i don't know what Blowfish_Encrypt looks like.

      image processing toolkits | batch image processing

      S 1 Reply Last reply
      0
      • S saiyuk6 7

        I am stuck actually, im trying to write a md5 / blowfish function im not sure if i am correctly converting the MD5[16] buffer into a unsigned long[4] this doesnt really make too much sense too me

        typedef struct _tag2Long {
        unsigned long _1;
        unsigned long _2;
        unsigned long _3;
        unsigned long _4;
        } _2LONG;

        void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
        unsigned int iSize, unsigned long pSignature[4])
        {
        MD5_CTX md5;
        BLOWFISH_CTX bf;

        MD5Init(&md5);
        MD5Update(&md5, pbzBuffer, iSize);
        MD5Final(&md5);

        _2LONG *tl = (_2LONG*) md5.digest;

        printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

        Blowfish_Init(&bf, pbzKey, 16);
        Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
        Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);

        pSignature[0] = tl->_1;
        pSignature[1] = tl->_2;
        pSignature[2] = tl->_3;
        pSignature[3] = tl->_4;

        printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
        pSignature[2], pSignature[3]);
        }

        bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
        unsigned int iSize, unsigned long pBuffer[4])
        {
        BLOWFISH_CTX bf;
        MD5_CTX md5;
        bool bResult = false;

        printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
        pBuffer[2], pBuffer[3]);

        Blowfish_Init(&bf, pbzKey, 16);
        Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
        Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);

        printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
        pBuffer[2], pBuffer[3]);

        MD5Init(&md5);
        MD5Update(&md5, pSignature, iSize);
        MD5Final(&md5);

        _2LONG *tl = (_2LONG*) md5.digest;
        // printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

        if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
        tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
        bResult = true;

        return bResult;
        }

        void main(void)
        {
        unsigned char bszBuffer[2] = {'A', 'B'};
        unsigned long ulSig[4];
        SignBuffer((unsigned char*)"key", bszBuffer, 2, ulSig);
        printf("\n");
        if (VerifyBuffer((unsigned char*)"key", bszBuffer, 2, ulSig))
        printf("\nok");
        else printf("\nerr");
        }

        and this is the output of them

        BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334
        BLE 2 f12b0ed9 782fae18

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        Hello! It might only be me but it is not obvious (to me) where you are trying to do this conversion you are talking about, you should at least highlight the part of interest in your code so people who want to help don't have to study your whole program trying to grasp the way you think.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

        S 1 Reply Last reply
        0
        • C Chris Losinger

          the conversion looks fine. but doesn't Blowfish use 64-bit (8-byte) blocks ? so this Blowfish_Encrypt(&bf, &tl->_1, &tl->_2); will encrypt the 8 bytes starting at tl->_1 ? since _1 is only four bytes, it's going to run over into _2. so, you're doing BF(_1 + _2) -> _2 + _3. or maybe not. i don't know what Blowfish_Encrypt looks like.

          image processing toolkits | batch image processing

          S Offline
          S Offline
          saiyuk6 7
          wrote on last edited by
          #4

          well it would be total 8 blocks for both _1 and _2, because a unsigned long is 4 bytes i am using Paul Kocher Blowfish Algorithm http://www.schneier.com/code/bfsh-koc.zip[^]

          C 1 Reply Last reply
          0
          • C Code o mat

            Hello! It might only be me but it is not obvious (to me) where you are trying to do this conversion you are talking about, you should at least highlight the part of interest in your code so people who want to help don't have to study your whole program trying to grasp the way you think.

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

            S Offline
            S Offline
            saiyuk6 7
            wrote on last edited by
            #5

            BLE 2 f12b0ed9 782fae18 a87960ac f37b342e

            is passed into VerifyBuffer, but when it is decrypted it doesnt match BLE 1 b0c66fb8 733df651 4c2d26de a9a0e334 when i pass a MD5 comparision from unsigned char *pSignature hope that makes sense? it seems to lose data some how, not sure

            1 Reply Last reply
            0
            • S saiyuk6 7

              well it would be total 8 blocks for both _1 and _2, because a unsigned long is 4 bytes i am using Paul Kocher Blowfish Algorithm http://www.schneier.com/code/bfsh-koc.zip[^]

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              ah, ok. it's expecting 2 32-bit parts. i thought the params were input and output.

              image processing toolkits | batch image processing

              S 1 Reply Last reply
              0
              • C Chris Losinger

                ah, ok. it's expecting 2 32-bit parts. i thought the params were input and output.

                image processing toolkits | batch image processing

                S Offline
                S Offline
                saiyuk6 7
                wrote on last edited by
                #7

                do you happen to know how i can fix it? i am lost

                C 1 Reply Last reply
                0
                • S saiyuk6 7

                  do you happen to know how i can fix it? i am lost

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

                  you're doing this, right: Sign: Sig = BFE(MD5(data)) Verify: X = BFD(Sig) Sig2 = MD5(Sig) Compare: X == Sig2 ? but shouldn't that bold line be Sig2 = MD5(data) ? the MD5 of the signature doesn't get you anything.

                  image processing toolkits | batch image processing

                  S 1 Reply Last reply
                  0
                  • C Chris Losinger

                    you're doing this, right: Sign: Sig = BFE(MD5(data)) Verify: X = BFD(Sig) Sig2 = MD5(Sig) Compare: X == Sig2 ? but shouldn't that bold line be Sig2 = MD5(data) ? the MD5 of the signature doesn't get you anything.

                    image processing toolkits | batch image processing

                    S Offline
                    S Offline
                    saiyuk6 7
                    wrote on last edited by
                    #9

                    i fixed it, thank you very much for your help, i noticed

                    if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
                    tl->_3 == pBuffer[2] && tl->_4 == pBuffer[4])
                    bResult = true;

                    pBuffer[4] should of been pBuffer[3] i was going out of range of the array which didnt exist here is the updated code

                    int _ustrlen(unsigned char *pbz)
                    {
                    const unsigned char *ptr;
                    for (ptr = pbz; *ptr; ++ptr);
                    return (ptr - pbz);
                    }

                    void SignBuffer(unsigned char *pbzKey, unsigned char *pbzBuffer,
                    unsigned int iSize, unsigned long *pSignature)
                    {
                    MD5_CTX md5, md5key;
                    BLOWFISH_CTX bf;

                    MD5Init(&md5);
                    MD5Update(&md5, pbzBuffer, iSize);
                    MD5Final(&md5);

                    _2LONG *tl = (_2LONG*) md5.digest;

                    printf("BLE 1\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

                    MD5Init(&md5key);
                    MD5Update(&md5key, pbzKey, _ustrlen(pbzKey));
                    MD5Final(&md5key);

                    Blowfish_Init(&bf, md5key.digest, 16);
                    Blowfish_Encrypt(&bf, &tl->_1, &tl->_2);
                    Blowfish_Encrypt(&bf, &tl->_3, &tl->_4);

                    pSignature[0] = tl->_1;
                    pSignature[1] = tl->_2;
                    pSignature[2] = tl->_3;
                    pSignature[3] = tl->_4;

                    printf("BLE 2\t\t %x %x %x %x\n", pSignature[0], pSignature[1],
                    pSignature[2], pSignature[3]);
                    }

                    bool VerifyBuffer(unsigned char *pbzKey, unsigned char *pSignature,
                    unsigned int iSize, unsigned long *pBuffer)
                    {
                    BLOWFISH_CTX bf;
                    MD5_CTX md5, md5key;
                    bool bResult = false;

                    printf("BLD 1\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
                    pBuffer[2], pBuffer[3]);

                    MD5Init(&md5key);
                    MD5Update(&md5key, pbzKey, _ustrlen(pbzKey));
                    MD5Final(&md5key);

                    Blowfish_Init(&bf, md5key.digest, 16);
                    Blowfish_Decrypt(&bf, &pBuffer[0], &pBuffer[1]);
                    Blowfish_Decrypt(&bf, &pBuffer[2], &pBuffer[3]);

                    printf("BLD 2\t\t %x %x %x %x\n", pBuffer[0], pBuffer[1],
                    pBuffer[2], pBuffer[3]);

                    MD5Init(&md5);
                    MD5Update(&md5, pSignature, iSize);
                    MD5Final(&md5);

                    _2LONG *tl = (_2LONG*) md5.digest;
                    printf("BLD 3\t\t %x %x %x %x\n", tl->_1, tl->_2, tl->_3, tl->_4);

                    if (tl->_1 == pBuffer[0] && tl->_2 == pBuffer[1] &&
                    tl->_3 == pBuffer[2] && tl->_4 == pBuffer[3])
                    bResult = true;

                    return bResult;

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups