473,659 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.*****@netz-haut.de>

Nov 15 '05 #1
16 6175
s.*****@netz-haut.de 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.

Nov 15 '05 #2
[snipped some white space]
s.*****@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.

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.


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.
Nov 15 '05 #3
s.*****@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.

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_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
Nov 15 '05 #4
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.*****@netz-haut.de>

Nov 15 '05 #5
On 9 Oct 2005 12:38:14 -0700, in comp.lang.c , s.*****@netz-haut.de
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 =----
Nov 15 '05 #6
s.*****@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.
Nov 15 '05 #7
Joe Wright wrote:
s.*****@netz-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.]

Nov 15 '05 #8
Martin Ambuhl <ma*****@earthl ink.net> writes:
Joe Wright wrote:
s.*****@netz-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?

--
Keith Thompson (The_Other_Keit h) ks***@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.
Nov 15 '05 #9
Keith Thompson wrote:
Martin Ambuhl <ma*****@earthl ink.net> writes:
Joe Wright wrote:
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.


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.

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
17875
by: Rowland | last post by:
Hi, I've got a field that stores numeric values, representing a tracking number. I've also got a stored procedure that will extract this field and return it to a client. However, I would like to return it slightly differently to the way in which it is stored. Basically, I want to return it as TRK000nnn - Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn is the number itself - results would look something like...
3
6067
by: Thanos | last post by:
hello, update trl_info set rec_count = repeat('0', 11- Length(cast((select count(*) from tableA) as char(5)))) concat cast((select count(*) from tableA) as char(5)) The problem is the that value of count(*) may be big or smaller than 5 characters long so how to dynamically change the casting of the interger to a char that is the same length of the count(*) size so
13
3874
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char keys; char padding;
13
22710
by: pb | last post by:
Im wanted to pad out blank spaces with a specific character instead of spaces or zeros, does C support that? printf("$%*d", '*', 5); // Not sure what the format string is supposed to look like to do this example output i would want is this: $********5
11
3623
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a certain gradient, a struct "rounds up" to the nearest multiple of the gradient. This teacher is somewhat erratic and has said on subsequent days that first the padding is rounded up to nearest multiple of 4, and then the nearest power of 2.
14
2004
by: Francine.Neary | last post by:
Consider the following situation. I have a large static array s of structs, say of size 500. I also need a smaller array of chars, say of size 100, which has nothing to do with the struct. To conserve memory, I'd like if possible to use (after checking with offsetof and sizeof that there's enough padding to fit a char - usually it will be) space between fields in the structs to store these chars. If the padding is between fields s.a and...
10
3272
by: Rohit kumar Chandel | last post by:
Hi All, Please let me know how to find in a structure whether compiler has used padding or not. Regards Rohit
12
4786
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
0
4091
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL = "SELECT * FROM " sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(msFile) & _
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.