padding zeros in char *

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s.seitz@netz-haut.de

    padding zeros in char *

    hi gods and programmers!

    i've got a weird problem with strlen() in C.

    in a current project, i need to AES_decrypt() some string. By now, i'm
    using the libssl (openssl) API.

    To get this AES_decrypt() work, i needed to split the encoded input
    into pieces of 16 byte. This works as expected.

    Only problem is every LAST piece in a line, which is propably shorter
    than 16 byte (as you already assumed, this differs between 1 and 15).
    According to the manual, i'll need to pad the piece with zeros until it
    reaches the length of 16 byte.

    Well, this is my problem: I can't pad with \0 since C cuts this
    'string' at the first occurency of \0.

    I tried almost everything: strcat, membcpy, writing directly to the
    address ... nothing... the last piece of encoded data stays as short as
    before my trials.



    Again, my problem is NOT the AES_decrypt(). My problem is padding the
    input with trailing \0 bytes.




    void AES_decrypt(con st unsigned char *in, unsigned char *out,
    ^^^^ const AES_KEY
    *key);
    this needs to be strlen()=16 ---------'





    Please, if someone knows an answer, let me know.

    Thanks in advance.

    Stephan Seitz
    <s.seitz@netz-haut.de>

  • Emmanuel Delahaye

    #2
    Re: padding zeros in char *

    s.seitz@netz-haut.de a écrit :[color=blue]
    > I tried almost everything: strcat, membcpy, writing directly to the
    > address ... nothing... the last piece of encoded data stays as short as
    > before my trials.[/color]

    It might a job for strncpy(). Read the manual carefully.

    Comment

    • Michael Mair

      #3
      Re: padding zeros in char *

      [snipped some white space]
      s.seitz@netz-haut.de wrote:[color=blue]
      > hi gods and programmers!
      >
      > i've got a weird problem with strlen() in C.
      >
      > in a current project, i need to AES_decrypt() some string. By now, i'm
      > using the libssl (openssl) API.
      >
      > To get this AES_decrypt() work, i needed to split the encoded input
      > into pieces of 16 byte. This works as expected.
      >
      > Only problem is every LAST piece in a line, which is propably shorter
      > than 16 byte (as you already assumed, this differs between 1 and 15).
      > According to the manual, i'll need to pad the piece with zeros until it
      > reaches the length of 16 byte.
      >
      > Well, this is my problem: I can't pad with \0 since C cuts this
      > 'string' at the first occurency of \0.
      >
      > I tried almost everything: strcat, membcpy, writing directly to the
      > address ... nothing... the last piece of encoded data stays as short as
      > before my trials.
      >
      > Again, my problem is NOT the AES_decrypt(). My problem is padding the
      > input with trailing \0 bytes.
      >
      > void AES_decrypt(con st unsigned char *in, unsigned char *out,
      > ^^^^ const AES_KEY
      > *key);
      > this needs to be strlen()=16 ---------'
      >
      >
      > Please, if someone knows an answer, let me know.[/color]

      strlen() is not the right tool to measure an array length
      as it will stop at the first zero/'\0' byte. Regardless how
      many more follow.
      Your spec probably only demanded an array of unsigned char.
      So there is nothing to worry about in this case. Pad your
      last "string" with '\0' as needed and you will be fine.
      The padding will not be visible to strlen() and you can
      only inspect the zero values by printing them out as integer
      values.


      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • Martin Ambuhl

        #4
        Re: padding zeros in char *

        s.seitz@netz-haut.de wrote:[color=blue]
        > hi gods and programmers!
        >
        > i've got a weird problem with strlen() in C.
        >
        > in a current project, i need to AES_decrypt() some string. By now, i'm
        > using the libssl (openssl) API.
        >
        > To get this AES_decrypt() work, i needed to split the encoded input
        > into pieces of 16 byte. This works as expected.
        >
        > Only problem is every LAST piece in a line, which is propably shorter
        > than 16 byte (as you already assumed, this differs between 1 and 15).
        > According to the manual, i'll need to pad the piece with zeros until it
        > reaches the length of 16 byte.
        >
        > Well, this is my problem: I can't pad with \0 since C cuts this
        > 'string' at the first occurency of \0.
        >
        > I tried almost everything: strcat, membcpy, writing directly to the
        > address ... nothing... the last piece of encoded data stays as short as
        > before my trials.[/color]

        If AES_decrypt expects an array of 16 chars, possibly zero-padded on the
        right, there is nothing to stand in your way. Strings have nothing to
        do with it. There are many ways to do this. Here is a pedestrian one:
        #include <stdio.h>
        #include <ctype.h>

        #define AESsize 16

        void pseudo_AES_decr ypt(char *s)
        {
        int i;
        printf("Receive d array: ");
        for (i = 0; i < AESsize; i++) {
        if (isprint(s[i]))
        putchar(s[i]);
        else
        printf("\\%03o" , (unsigned) s[i]);
        }
        putchar('\n');
        }


        int main(void)
        {
        char *tst = "abcedefghijklm nopqrstuvwxyz"
        "ABCDEFGHHIJKLM NOPQRSTUVWXYZ";
        char buf[AESsize], *t, *b;
        int i;
        for (t = tst; *t;) {
        for (i = 0; b = buf, i < AESsize; i++)
        b[i] = (*t) ? *t++ : *t;
        pseudo_AES_decr ypt(buf);
        }
        return 0;
        }

        Received array: abcedefghijklmn o
        Received array: pqrstuvwxyzABCD E
        Received array: FGHHIJKLMNOPQRS T
        Received array: UVWXYZ\000\000\ 000\000\000\000 \000\000\000\00 0

        Comment

        • s.seitz@netz-haut.de

          #5
          Re: padding zeros in char *

          Many thank's to all of you, guys!

          Your answers were really helpful.

          Finally, the strncpy() manual solved my problem.

          char *strncpy(char *dest, const char *src, size_t n);

          --> "In the case where the length of src is less than that of n, the
          remainder of dest will be padded with nulls."

          This isn't that speedy than you solution, martin, so i'll swap over to
          your code.

          Many thanks again!

          Greetings

          Stephan Seitz
          <s.seitz@netz-haut.de>

          Comment

          • Mark McIntyre

            #6
            Re: padding zeros in char *

            On 9 Oct 2005 12:38:14 -0700, in comp.lang.c , s.seitz@netz-haut.de
            wrote:
            [color=blue]
            >hi gods and programmers!
            >
            >i've got a weird problem with strlen() in C.[/color]

            (of trying to use the str... functions on things which aren't strings)

            Your problem is that you're thinking of your 16-byte chunks as
            strings. They're not - they can't be, since they contain embedded
            nulls. Treat them as blocks of memory,. and use the mem... functions
            maybe?
            --
            Mark McIntyre
            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

            ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
            http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
            ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

            Comment

            • SM Ryan

              #7
              Re: padding zeros in char *

              s.seitz@netz-haut.de wrote:
              # hi gods and programmers!
              #
              # i've got a weird problem with strlen() in C.
              #
              # in a current project, i need to AES_decrypt() some string. By now, i'm
              # using the libssl (openssl) API.
              #
              # To get this AES_decrypt() work, i needed to split the encoded input
              # into pieces of 16 byte. This works as expected.
              #
              # Only problem is every LAST piece in a line, which is propably shorter
              # than 16 byte (as you already assumed, this differs between 1 and 15).
              # According to the manual, i'll need to pad the piece with zeros until it
              # reaches the length of 16 byte.
              #
              # Well, this is my problem: I can't pad with \0 since C cuts this
              # 'string' at the first occurency of \0.

              length := strlen(string)
              while length>0,
              memset(destinat ion,16,0)
              if length>16,
              piecelength := 16
              else
              piecelength := length
              memcpy(destinat ion,string,piec elength)
              decrypt(destina tion)
              string +:= piecelength
              length -:= piecelength

              --
              SM Ryan http://www.rawbw.com/~wyrmwif/
              I have no respect for people with no shopping agenda.

              Comment

              • Joe Wright

                #8
                Re: padding zeros in char *

                s.seitz@netz-haut.de wrote:[color=blue]
                > hi gods and programmers!
                >
                > i've got a weird problem with strlen() in C.
                >
                > in a current project, i need to AES_decrypt() some string. By now, i'm
                > using the libssl (openssl) API.
                >
                > To get this AES_decrypt() work, i needed to split the encoded input
                > into pieces of 16 byte. This works as expected.
                >
                > Only problem is every LAST piece in a line, which is propably shorter
                > than 16 byte (as you already assumed, this differs between 1 and 15).
                > According to the manual, i'll need to pad the piece with zeros until it
                > reaches the length of 16 byte.
                >
                > Well, this is my problem: I can't pad with \0 since C cuts this
                > 'string' at the first occurency of \0.
                >
                > I tried almost everything: strcat, membcpy, writing directly to the
                > address ... nothing... the last piece of encoded data stays as short as
                > before my trials.
                >
                >
                >
                > Again, my problem is NOT the AES_decrypt(). My problem is padding the
                > input with trailing \0 bytes.
                >[/color]

                How about '0' bytes instead of '\0' bytes?

                --
                Joe Wright
                "Everything should be made as simple as possible, but not simpler."
                --- Albert Einstein ---

                Comment

                • Martin Ambuhl

                  #9
                  Re: padding zeros in char *

                  Joe Wright wrote:[color=blue]
                  > s.seitz@netz-haut.de wrote:[/color]
                  [color=blue][color=green]
                  >> Again, my problem is NOT the AES_decrypt(). My problem is padding the
                  >> input with trailing \0 bytes.
                  >>[/color]
                  >
                  > How about '0' bytes instead of '\0' bytes?[/color]

                  What earthly difference could that make?
                  [Ans: none.]

                  Comment

                  • Keith Thompson

                    #10
                    Re: padding zeros in char *

                    Martin Ambuhl <mambuhl@earthl ink.net> writes:[color=blue]
                    > Joe Wright wrote:[color=green]
                    >> s.seitz@netz-haut.de wrote:[/color]
                    >[color=green][color=darkred]
                    >>> Again, my problem is NOT the AES_decrypt(). My problem is padding the
                    >>> input with trailing \0 bytes.
                    >>>[/color]
                    >> How about '0' bytes instead of '\0' bytes?[/color]
                    >
                    > What earthly difference could that make?
                    > [Ans: none.][/color]

                    Presumably the difference is that padding with '0' bytes won't work.
                    According to the OP, the AES_decrypt requires the last block to be
                    padded with '\0' bytes.

                    Joe, what did you have in mind?

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: padding zeros in char *

                      Keith Thompson wrote:[color=blue]
                      > Martin Ambuhl <mambuhl@earthl ink.net> writes:
                      >[color=green]
                      >>Joe Wright wrote:[/color][/color]
                      [color=blue][color=green][color=darkred]
                      >>>How about '0' bytes instead of '\0' bytes?[/color]
                      >>
                      >>What earthly difference could that make?
                      >>[Ans: none.][/color]
                      >
                      >
                      > Presumably the difference is that padding with '0' bytes won't work.
                      > According to the OP, the AES_decrypt requires the last block to be
                      > padded with '\0' bytes.[/color]

                      Silly me. I read Joe's suggestion as "0 bytes instead of '\0' bytes,"
                      which of course would make no difference. This sort of reading failure
                      is why debugging is sometimes difficult. My response was obviously
                      wrong: the difference that Joe's suggestion makes is that it guarantees
                      failure.

                      Comment

                      • Joe Wright

                        #12
                        Re: padding zeros in char *

                        Keith Thompson wrote:[color=blue]
                        > Martin Ambuhl <mambuhl@earthl ink.net> writes:
                        >[color=green]
                        >>Joe Wright wrote:
                        >>[color=darkred]
                        >>>s.seitz@ne tz-haut.de wrote:[/color]
                        >>[color=darkred]
                        >>>>Again, my problem is NOT the AES_decrypt(). My problem is padding the
                        >>>>input with trailing \0 bytes.
                        >>>>
                        >>>
                        >>>How about '0' bytes instead of '\0' bytes?[/color]
                        >>
                        >>What earthly difference could that make?
                        >>[Ans: none.][/color]
                        >
                        >
                        > Presumably the difference is that padding with '0' bytes won't work.
                        > According to the OP, the AES_decrypt requires the last block to be
                        > padded with '\0' bytes.
                        >
                        > Joe, what did you have in mind?
                        >[/color]
                        The problem seems to be that OP needs 16-byte strings. Clearly an
                        arbitrarily placed '\0' may shorten the string. Padding with '0'
                        characters will solve length problem.

                        The OP may be mis-reading the requirement. Demanding 16-byte strings and
                        padding with '\0' doesn't make sense.

                        --
                        Joe Wright
                        "Everything should be made as simple as possible, but not simpler."
                        --- Albert Einstein ---

                        Comment

                        • Keith Thompson

                          #13
                          Re: padding zeros in char *

                          Joe Wright <jwright@comcas t.net> writes:[color=blue]
                          > Keith Thompson wrote:[color=green]
                          >> Martin Ambuhl <mambuhl@earthl ink.net> writes:
                          >>[color=darkred]
                          >>>Joe Wright wrote:
                          >>>
                          >>>>s.seitz@net z-haut.de wrote:
                          >>>
                          >>>>>Again, my problem is NOT the AES_decrypt(). My problem is padding the
                          >>>>>input with trailing \0 bytes.
                          >>>>>
                          >>>>
                          >>>>How about '0' bytes instead of '\0' bytes?
                          >>>
                          >>>What earthly difference could that make?
                          >>>[Ans: none.][/color]
                          >> Presumably the difference is that padding with '0' bytes won't work.
                          >> According to the OP, the AES_decrypt requires the last block to be
                          >> padded with '\0' bytes.
                          >> Joe, what did you have in mind?
                          >>[/color]
                          > The problem seems to be that OP needs 16-byte strings. Clearly an
                          > arbitrarily placed '\0' may shorten the string. Padding with '0'
                          > characters will solve length problem.
                          >
                          > The OP may be mis-reading the requirement. Demanding 16-byte strings
                          > and padding with '\0' doesn't make sense.[/color]

                          I strongly suspect that AES_decrypt() requires 16-byte character
                          arrays, not strings (at least not the way C defines the term
                          "string"). Padding a character array with '\0' does make sense; in
                          fact, this may be one of the few good uses for strncpy().

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Old Wolf

                            #14
                            Re: padding zeros in char *

                            Keith Thompson wrote:[color=blue]
                            >
                            > I strongly suspect that AES_decrypt() requires 16-byte character
                            > arrays, not strings (at least not the way C defines the term
                            > "string"). Padding a character array with '\0' does make sense; in
                            > fact, this may be one of the few good uses for strncpy().[/color]

                            Unlikely, as strncpy() will stop reading the source when it
                            encounters the first '\0' character. I gather from the thread
                            that the source may contain multiple '\0' characters at
                            various points.

                            Comment

                            • Joe Wright

                              #15
                              Re: padding zeros in char *

                              Keith Thompson wrote:[color=blue]
                              > Joe Wright <jwright@comcas t.net> writes:
                              >[color=green]
                              >>Keith Thompson wrote:
                              >>[color=darkred]
                              >>>Martin Ambuhl <mambuhl@earthl ink.net> writes:
                              >>>
                              >>>
                              >>>>Joe Wright wrote:
                              >>>>
                              >>>>
                              >>>>>s.seitz@ne tz-haut.de wrote:
                              >>>>
                              >>>>>>Again, my problem is NOT the AES_decrypt(). My problem is padding the
                              >>>>>>input with trailing \0 bytes.
                              >>>>>>
                              >>>>>
                              >>>>>How about '0' bytes instead of '\0' bytes?
                              >>>>
                              >>>>What earthly difference could that make?
                              >>>>[Ans: none.]
                              >>>
                              >>>Presumably the difference is that padding with '0' bytes won't work.
                              >>>According to the OP, the AES_decrypt requires the last block to be
                              >>>padded with '\0' bytes.
                              >>>Joe, what did you have in mind?
                              >>>[/color]
                              >>
                              >>The problem seems to be that OP needs 16-byte strings. Clearly an
                              >>arbitrarily placed '\0' may shorten the string. Padding with '0'
                              >>characters will solve length problem.
                              >>
                              >>The OP may be mis-reading the requirement. Demanding 16-byte strings
                              >>and padding with '\0' doesn't make sense.[/color]
                              >
                              >
                              > I strongly suspect that AES_decrypt() requires 16-byte character
                              > arrays, not strings (at least not the way C defines the term
                              > "string"). Padding a character array with '\0' does make sense; in
                              > fact, this may be one of the few good uses for strncpy().
                              >[/color]

                              Go for it. Your suspicion is every bit as good as mine. I have no idea
                              of AES_decrypt() and what it requires. I hope I haven't reduced the S/N
                              too much.

                              --
                              Joe Wright
                              "Everything should be made as simple as possible, but not simpler."
                              --- Albert Einstein ---

                              Comment

                              Working...