Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: Right way of using setkey(3) and encrypt(3)
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


Report this thread to moderator Post Follow-up to this message
Old Post
cnystrom@gmail.com
12-16-04 09:05 PM


Re: Right way of using setkey(3) and encrypt(3)
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:
 
#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.

Report this thread to moderator Post Follow-up to this message
Old Post
Wayne C. Morris
12-16-04 09:05 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:45 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.