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#
  4. LRC calculation... What did I miss? [modified] Solved

LRC calculation... What did I miss? [modified] Solved

Scheduled Pinned Locked Moved C#
sysadminsecurityhelpquestion
10 Posts 5 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.
  • K Offline
    K Offline
    Kiotaya
    wrote on last edited by
    #1

    I need to get the LRC (longitudinal redundancy check) value of a string. This data is being sent to a credit card processing network over SSL. After I get the LRC I am coverting it to string, then to a byte, and adding it to the end of a byte[] to send through SSL. When I send the transaction I am getting a NAK response to my LRC. What am I doing wrong? I have tried this RequestMessage contains Message+ETX only

    public static char ReturnLRC(string RequestMessage)
    {
    int lrcAnswer = 0;
    for (int i = 0; i < RequestMessage.Length; i++)
    {
    lrcAnswer = lrcAnswer ^ (Byte)(Encoding.UTF7.GetBytes(RequestMessage.Substring(i, 1))[0]);
    }
    return (Char)lrcAnswer;
    }

    char charLrc = ReturnLRC(Trans);
    string lrcString = charLrc.ToString();
    byte[] LRC = unicode.GetBytes(lrcString );
    fullMessage[addLRCHere] = LRC[0];

    I have also tried this seperatly. RequestMessage contains Message+ETX only

    public static char theReturnLRC(string RequestMessage)
    {
    int lrcAnswer = 0;
    byte[] byArray = Encoding.UTF7.GetBytes(RequestMessage);

     foreach (byte curByte in byArray)
     {
                 lrcAnswer ^= curByte;
     }
             return (char)lrcAnswer;
    

    }

    char charLrc = ReturnLRC(Trans);
    string lrcString = charLrc.ToString();
    byte[] LRC = unicode.GetBytes(lrcString );
    fullMessage[addLRCHere] = LRC[0];

    Neither process works, are the methods incorrect or is my conversions afterwards messing everything up? Thanks in advance for any help or advice. Removing all the conversion confusion I found that I was generating the correct LRC. Thanks Everyone!!! Kiotaya

    modified on Monday, November 16, 2009 10:57 AM

    OriginalGriffO L 2 Replies Last reply
    0
    • K Kiotaya

      I need to get the LRC (longitudinal redundancy check) value of a string. This data is being sent to a credit card processing network over SSL. After I get the LRC I am coverting it to string, then to a byte, and adding it to the end of a byte[] to send through SSL. When I send the transaction I am getting a NAK response to my LRC. What am I doing wrong? I have tried this RequestMessage contains Message+ETX only

      public static char ReturnLRC(string RequestMessage)
      {
      int lrcAnswer = 0;
      for (int i = 0; i < RequestMessage.Length; i++)
      {
      lrcAnswer = lrcAnswer ^ (Byte)(Encoding.UTF7.GetBytes(RequestMessage.Substring(i, 1))[0]);
      }
      return (Char)lrcAnswer;
      }

      char charLrc = ReturnLRC(Trans);
      string lrcString = charLrc.ToString();
      byte[] LRC = unicode.GetBytes(lrcString );
      fullMessage[addLRCHere] = LRC[0];

      I have also tried this seperatly. RequestMessage contains Message+ETX only

      public static char theReturnLRC(string RequestMessage)
      {
      int lrcAnswer = 0;
      byte[] byArray = Encoding.UTF7.GetBytes(RequestMessage);

       foreach (byte curByte in byArray)
       {
                   lrcAnswer ^= curByte;
       }
               return (char)lrcAnswer;
      

      }

      char charLrc = ReturnLRC(Trans);
      string lrcString = charLrc.ToString();
      byte[] LRC = unicode.GetBytes(lrcString );
      fullMessage[addLRCHere] = LRC[0];

      Neither process works, are the methods incorrect or is my conversions afterwards messing everything up? Thanks in advance for any help or advice. Removing all the conversion confusion I found that I was generating the correct LRC. Thanks Everyone!!! Kiotaya

      modified on Monday, November 16, 2009 10:57 AM

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I am amazed that a credit card company is using only LRC - I would have thought at least CRC-16. However, assuming LRC is what they want, then I would be reasonably sure that it is the various conversions that are causing the problem. You are starting with a UTF16, convering it to a byte via UTF7, then back to a char (UTF16) and up to a string (still UTF16) and then back to a byte. I would start with a similar data set from them, and examine that. What do they send as the data, what do they send as the LRC? You should then be able to work it out from that.

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • K Kiotaya

        I need to get the LRC (longitudinal redundancy check) value of a string. This data is being sent to a credit card processing network over SSL. After I get the LRC I am coverting it to string, then to a byte, and adding it to the end of a byte[] to send through SSL. When I send the transaction I am getting a NAK response to my LRC. What am I doing wrong? I have tried this RequestMessage contains Message+ETX only

        public static char ReturnLRC(string RequestMessage)
        {
        int lrcAnswer = 0;
        for (int i = 0; i < RequestMessage.Length; i++)
        {
        lrcAnswer = lrcAnswer ^ (Byte)(Encoding.UTF7.GetBytes(RequestMessage.Substring(i, 1))[0]);
        }
        return (Char)lrcAnswer;
        }

        char charLrc = ReturnLRC(Trans);
        string lrcString = charLrc.ToString();
        byte[] LRC = unicode.GetBytes(lrcString );
        fullMessage[addLRCHere] = LRC[0];

        I have also tried this seperatly. RequestMessage contains Message+ETX only

        public static char theReturnLRC(string RequestMessage)
        {
        int lrcAnswer = 0;
        byte[] byArray = Encoding.UTF7.GetBytes(RequestMessage);

         foreach (byte curByte in byArray)
         {
                     lrcAnswer ^= curByte;
         }
                 return (char)lrcAnswer;
        

        }

        char charLrc = ReturnLRC(Trans);
        string lrcString = charLrc.ToString();
        byte[] LRC = unicode.GetBytes(lrcString );
        fullMessage[addLRCHere] = LRC[0];

        Neither process works, are the methods incorrect or is my conversions afterwards messing everything up? Thanks in advance for any help or advice. Removing all the conversion confusion I found that I was generating the correct LRC. Thanks Everyone!!! Kiotaya

        modified on Monday, November 16, 2009 10:57 AM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Kiotaya wrote:

        (Byte)(Encoding.UTF7.GetBytes(RequestMessage.Substring(i, 1))[0]

        This does not seem right: you choose UTF7 encoding, however for each string character you only use the first byte GetBytes returns. You should have two distinct steps: - represent your string as a byte array in the encoding of your choice; - calculate LRC on that byte array. :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        K 1 Reply Last reply
        0
        • L Luc Pattyn

          Kiotaya wrote:

          (Byte)(Encoding.UTF7.GetBytes(RequestMessage.Substring(i, 1))[0]

          This does not seem right: you choose UTF7 encoding, however for each string character you only use the first byte GetBytes returns. You should have two distinct steps: - represent your string as a byte array in the encoding of your choice; - calculate LRC on that byte array. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          K Offline
          K Offline
          Kiotaya
          wrote on last edited by
          #4

          Thank you for your response Luc. However it looks like what you are telling me is what I did in the second example I show. Please correct me if I am wrong in this assumtion. Here is the second example. RequestMessage contains Message+ETX only

          public static char theReturnLRC(string RequestMessage)
          {
          int lrcAnswer = 0;
          byte[] byArray = Encoding.UTF7.GetBytes(RequestMessage);

           foreach (byte curByte in byArray)     
           {                 
             lrcAnswer ^= curByte;     
           }             
             return (char)lrcAnswer;
          

          }

          char charLrc = ReturnLRC(Trans);
          string lrcString = charLrc.ToString();
          byte[] LRC = unicode.GetBytes(lrcString );
          fullMessage[addLRCHere] = LRC[0];

          L 2 Replies Last reply
          0
          • K Kiotaya

            Thank you for your response Luc. However it looks like what you are telling me is what I did in the second example I show. Please correct me if I am wrong in this assumtion. Here is the second example. RequestMessage contains Message+ETX only

            public static char theReturnLRC(string RequestMessage)
            {
            int lrcAnswer = 0;
            byte[] byArray = Encoding.UTF7.GetBytes(RequestMessage);

             foreach (byte curByte in byArray)     
             {                 
               lrcAnswer ^= curByte;     
             }             
               return (char)lrcAnswer;
            

            }

            char charLrc = ReturnLRC(Trans);
            string lrcString = charLrc.ToString();
            byte[] LRC = unicode.GetBytes(lrcString );
            fullMessage[addLRCHere] = LRC[0];

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            OK, two comments: 1.

            Kiotaya wrote:

            string lrcString = charLrc.ToString(); byte[] LRC = unicode.GetBytes(lrcString ); fullMessage[addLRCHere] = LRC[0];

            what is all this stuff? you perform calculations on bytes, turn the result in a char, then a string, then a byte array, and then take the first byte. Why can't theReturnLRC() just return the byte result for immediate use? 2. Your LRC is starting from zero, and XORing the bytes. There are hundreds of "standard" checksums and the like (using XOR or ADD; using left shifts, with recycling some carry, etc). Are you sure this is the one you need? Can you confirm with a few simple examples that are known to be good? This[^] says "...may be computed in software by the following algorithm...". While this[^] uses a different definition. :)

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            1 Reply Last reply
            0
            • K Kiotaya

              Thank you for your response Luc. However it looks like what you are telling me is what I did in the second example I show. Please correct me if I am wrong in this assumtion. Here is the second example. RequestMessage contains Message+ETX only

              public static char theReturnLRC(string RequestMessage)
              {
              int lrcAnswer = 0;
              byte[] byArray = Encoding.UTF7.GetBytes(RequestMessage);

               foreach (byte curByte in byArray)     
               {                 
                 lrcAnswer ^= curByte;     
               }             
                 return (char)lrcAnswer;
              

              }

              char charLrc = ReturnLRC(Trans);
              string lrcString = charLrc.ToString();
              byte[] LRC = unicode.GetBytes(lrcString );
              fullMessage[addLRCHere] = LRC[0];

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I forgot one remark: if (I don't know, you haven't told us enough to be sure) your communication is basically binary possibly with an occasional letter or word embedded in it, then you should not be using any strings at all, instead you then should be using byte[] exclusively. Do you have the full specification? :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              K 1 Reply Last reply
              0
              • L Luc Pattyn

                I forgot one remark: if (I don't know, you haven't told us enough to be sure) your communication is basically binary possibly with an occasional letter or word embedded in it, then you should not be using any strings at all, instead you then should be using byte[] exclusively. Do you have the full specification? :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                K Offline
                K Offline
                Kiotaya
                wrote on last edited by
                #7

                I am going to be running some more tests today. I will give you more details this afternoon. Thank you very much for your help and time.

                K 1 Reply Last reply
                0
                • K Kiotaya

                  I am going to be running some more tests today. I will give you more details this afternoon. Thank you very much for your help and time.

                  K Offline
                  K Offline
                  Kiotaya
                  wrote on last edited by
                  #8

                  Sorry I didn't reply to this much sooner but the help you game me help me clear up this problem. Thanks Luc Pattyn and everyone else.

                  U 1 Reply Last reply
                  0
                  • K Kiotaya

                    Sorry I didn't reply to this much sooner but the help you game me help me clear up this problem. Thanks Luc Pattyn and everyone else.

                    U Offline
                    U Offline
                    User 7684353
                    wrote on last edited by
                    #9

                    Hello would you be able to post the solution for this? I have a scenario where its been mentioned that my message will be sent in the following format STX Data ETX BCC And that BCC = LRC - XOR over all characters excluding STX but including ETX. I am kind of trying to figure out how this works? Here is the LRCCalculation method I have implemented.

                    private char CalculateLongitudinalRedundancyCheck(string source)
                    {
                    int result = 0;
                    for (int i = 0; i < source.Length; i++)
                    {
                    result = result ^ (Byte)(Encoding.ASCII.GetBytes(source.Substring(i, 1))[0]);
                    }
                    return (Char)result;
                    }

                    I get the LRC from this and then what...!! For ex: say my data is something like "345565645023495767714.12.1988021114:33 15.8001".

                    M 1 Reply Last reply
                    0
                    • U User 7684353

                      Hello would you be able to post the solution for this? I have a scenario where its been mentioned that my message will be sent in the following format STX Data ETX BCC And that BCC = LRC - XOR over all characters excluding STX but including ETX. I am kind of trying to figure out how this works? Here is the LRCCalculation method I have implemented.

                      private char CalculateLongitudinalRedundancyCheck(string source)
                      {
                      int result = 0;
                      for (int i = 0; i < source.Length; i++)
                      {
                      result = result ^ (Byte)(Encoding.ASCII.GetBytes(source.Substring(i, 1))[0]);
                      }
                      return (Char)result;
                      }

                      I get the LRC from this and then what...!! For ex: say my data is something like "345565645023495767714.12.1988021114:33 15.8001".

                      M Offline
                      M Offline
                      Michael Sogos 2021
                      wrote on last edited by
                      #10

                      I land to the same issue. Guys how did you resolved it?

                      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