12-17-04 12:45 AM
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 supp
osed 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:
[vbcol=seagreen]
#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.
[ Post a follow-up to this message ]
|