Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

encrypt and decrypt using encrypt(char block[64], int edflag)

82 views
Skip to first unread message

rockwell

unread,
Jul 25, 2004, 4:40:35 AM7/25/04
to
Hi groups i am trying to encrypt and decrypt back a block of data .I
am using the standard encrypt(char block[64], int edflag) function. I
have written a small program which should do this according to the man
page.the program looks like

#include <stdio.h>
#include <unistd.h>
#include <crypt.h>

int main(void)
{
char key[64];
char txt[64]="myblockofdata";

strcat(key, "thisismystring");

printf("Before encrypting");
printf("txt is %s",txt);
printf("\n");
setkey(key);

printf("After encrypting");
encrypt(txt, 0);
printf("txt is %s",txt);
printf("\n");


printf("After decrypting");
encrypt(txt, 1);
printf("txt is %s",txt);
printf("\n");
return 0;
}


but this program does not display any thing except the string before
encrypting.Can u suggest me what to do and how to proceed. According
to the manpage on encrypt it just says if edflag is 0 it encrypts and
if edflag is 1 it decrypts and i even want to know what does this mean
void setkey(const char *key). The key parameter used here is an array
of bytes, having each byte the numerical value 1 or 0. Does this mean
that the key parameter should contain only 0's and 1's.I would be very
thankful if you can answer me. If this is not the proper group can you
please direct me to a proper group.

Thanks in advance.

Jens.T...@physik.fu-berlin.de

unread,
Jul 25, 2004, 9:10:50 AM7/25/04
to

> strcat(key, "thisismystring");

Yes. And also the text you want to encrypt or decrypt need to be
converted to something that consists of 1 and 0's only! So out of
each char you're going to encrypt you first have to make an array
of 8 chars. Here's a program that shows one wy how you can do that:

#define _XOPEN_SOURC
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <crypt.h>

int main(void)
{
char key[64];

char orig[ 9 ]= "myblocko";
char buf[64];
char txt[ 9 ];
int i, j;

for ( i = 0; i < 64; i++ )
key[ i ] = rand( ) & 1;

printf("Before encrypting: %s\n", orig);
for ( i = 0; i < 8; i++ )
for ( j = 0; j < 8; j++ )
buf[ i * 8 + j ] = orig[ i ] >> j & 1;
setkey(key);

encrypt(buf, 0);
for ( i = 0; i < 8; i++ )
for ( j = 0, txt[ i ] = '\0'; j < 8; j++ )
txt[ i ] |= buf[ i * 8 + j ] << j;
txt[ 9 ] = '\0';
printf("After encrypting: %s\n", txt);

encrypt(buf, 1);
for ( i = 0; i < 8; i++ )
for ( j = 0, txt[ i ] = '\0'; j < 8; j++ )
txt[ i ] |= buf[ i * 8 + j ] << j;
txt[ 9 ] = '\0';
printf("After decrypting: %s\n", txt);
return 0;
}

If you take a closer look at this

for ( i = 0; i < 8; i++ )
for ( j = 0; j < 8; j++ )
buf[ i * 8 + j ] = orig[ i ] >> j & 1;

It takes the characters from 'orig' and spreads them over 8 bytes for
each of the charatcters, putting this into the buffer to be converted.
After the encryption/decryption you need to do the reverse to get back
at text. (Since encrypt() only takes a buffer of 64 bytes I had to
cut back your original string to 8 characters, more wont fit into
the 64 byte wide buffer.) In contrast to your program I am using a
random key - if you want instead to use your string you also have to
spread the first 8 chars over the 64 byte key.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.T...@physik.fu-berlin.de
\__________________________ http://www.toerring.de

rockwell

unread,
Jul 25, 2004, 7:33:30 PM7/25/04
to
Hi jens,
I am really thankful to u man,this has solved my problem,it
works now.I appreciate that.Once again thanks man.

Bye.

Jens.T...@physik.fu-berlin.de wrote in message news:<2mhpqqF...@uni-berlin.de>...

David Schwartz

unread,
Jul 25, 2004, 11:58:20 PM7/25/04
to

"rockwell" <rockwe...@yahoo.com> wrote in message
news:6bbb0df8.04072...@posting.google.com...

> int main(void)
> {
> char key[64];
> char txt[64]="myblockofdata";
>
> strcat(key, "thisismystring");

Oops. The 'strcat' function requires a pointer to a nul terminated
string for its first parameter. You want 'strcpy', which just needs a
pointer to space in which a string can be held.

DS


0 new messages