Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > padding zeros in char *

Reply
Thread Tools

padding zeros in char *

 
 
s.seitz@netz-haut.de
Guest
Posts: n/a
 
      10-09-2005
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(const 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
<(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      10-09-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> 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.


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

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      10-09-2005
[snipped some white space]
(E-Mail Removed) 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.
>
> 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(const 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.


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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-09-2005
(E-Mail Removed) 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.
>
> 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.


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_decrypt(char *s)
{
int i;
printf("Received 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 = "abcedefghijklmnopqrstuvwxyz"
"ABCDEFGHHIJKLMNOPQRSTUVWXYZ";
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_decrypt(buf);
}
return 0;
}

Received array: abcedefghijklmno
Received array: pqrstuvwxyzABCDE
Received array: FGHHIJKLMNOPQRST
Received array: UVWXYZ\000\000\000\000\000\000\000\000\000\000
 
Reply With Quote
 
s.seitz@netz-haut.de
Guest
Posts: n/a
 
      10-09-2005
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
<(E-Mail Removed)>

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-09-2005
On 9 Oct 2005 12:38:14 -0700, in comp.lang.c , (E-Mail Removed)
wrote:

>hi gods and programmers!
>
>i've got a weird problem with strlen() in C.


(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 =----
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      10-09-2005
(E-Mail Removed) 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(destination,16,0)
if length>16,
piecelength := 16
else
piecelength := length
memcpy(destination,string,piecelength)
decrypt(destination)
string +:= piecelength
length -:= piecelength

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no respect for people with no shopping agenda.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-10-2005
(E-Mail Removed) 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.
>
> 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.
>


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

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-10-2005
Joe Wright wrote:
> (E-Mail Removed) 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.]

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-10-2005
Martin Ambuhl <(E-Mail Removed)> writes:
> Joe Wright wrote:
>> (E-Mail Removed) 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?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Number padding... (trailing zeros'.) ThePotPlants Perl Misc 4 05-23-2004 10:19 PM



Advertisments