For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2004 > Re: Right way of using setkey(3) and encrypt(3)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Right way of using setkey(3) and encrypt(3)
cnystrom@gmail.com

2004-12-16, 4:05 pm


YBH123 wrote:
> I need a dual-way encrypt function ( encrypt and decrypt). The unix
> function setkey(3) and encrypt(3) seems the right function to use,

but the
> man page of these two functions are really obscure about the

parameter
> passing. Could some one tell me the right way of using these two

function to
> encrypt and decrypt a character string?


I do not know.

The encrypt(3) man page gives this example:

--

#include <crypt.h>

main() {
char key[64]; /* bit pattern for key */
char txt[64]; /* bit pattern for messages */
setkey(key);
encrypt(txt, 0); /* encode */
encrypt(txt, 1); /* decode */
}

--

which is incomplete, so I tried putting in some real values:

--

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

int
main()
{

/* bit pattern for key */
char key[64] =
" 1234567890123456789012345678901234567890
12345678901234567890123";

/* bit pattern for messages */
char txt[64] =
" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN
OPQRSTUVWXYZ0123456789_";

setkey(key);

printf("|%s|\n", txt);

encrypt(txt, 0); /* encode */

printf("|%s|\n", txt);

encrypt(txt, 1); /* decode */

printf("|%s|\n", txt);
}

--

This does not work. The text after decode is not the same as the text
before encode. Obviously, I am not understanding something.

Can someone point out to me what I am doing wrong?

Please do not refer me to other libraries. I am trying to understand
how these functions work.

Thank you,
Chris

cnystrom@gmail.com

Wayne C. Morris

2004-12-16, 4:05 pm

In article <1103201296.447112.45930@f14g2000cwb.googlegroups.com>,
cnystrom@gmail.com wrote:

> YBH123 wrote:
>
> I do not know.
>

[snip]
> /* bit pattern for key */
> char key[64] =
> " 1234567890123456789012345678901234567890
12345678901234567890123";
>
> /* bit pattern for messages */
> char txt[64] =
> " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN
OPQRSTUVWXYZ0123456789_";
>
> setkey(key);
>
> printf("|%s|\n", txt);
>
> encrypt(txt, 0); /* encode */

[snip]
> This does not work. The text after decode is not the same as the text
> before encode. Obviously, I am not understanding something.
>
> Can someone point out to me what I am doing wrong?


You're trying to use 64-byte strings. Both key[] and txt[] are supposed to
be 64-byte arrays of 1's and 0's (numeric values, not ASCII digits).

Here's a corrected version of your test program:
[color=darkred]
#include <stdio.h>
#include <crypt.h>


void display( char *msg )
{
int i;
for (i=0; i<64; i++)
{
printf( "%1d", (msg[i] ? 1 : 0) );
}
printf("\n");
}


int main()
{
/* bit pattern for key */
char key[64] =
{
1,0,0,1,0,1,0,0, 1,0,1,0,1,0,1,1, 1,1,0,0,0,1,0,1, 1,1,1,1,0,0,0,1,
0,0,0,1,0,0,1,0, 1,1,1,0,0,0,1,0, 1,0,1,1,0,1,0,0, 1,0,1,0,1,0,1,1
};

/* bit pattern for messages */
char txt[64] =
{
0,0,1,1,0,1,0,0, 0,1,0,0,1,0,1,1, 0,1,0,1,0,1,1,0, 0,0,1,0,1,0,0,0,
1,0,1,0,1,1,0,0, 1,0,1,0,0,0,1,0, 1,0,1,0,1,0,0,0, 1,0,1,1,1,0,1,1
};


setkey(key);

display(txt);

encrypt(txt, 0); /* encode */

display(txt);

encrypt(txt, 1); /* decode */

display(txt);
}
<<<

For a more practical program, you'd want to add functions to convert an
8-byte string into a 64-byte array of 1's and 0's, and vice versa.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com