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

From: rockwell (rockwell_001_at_yahoo.com)
Date: 07/26/04


Date: 25 Jul 2004 16:33:30 -0700

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.Toerring@physik.fu-berlin.de wrote in message news:<2mhpqqFm835sU1@uni-berlin.de>...
> rockwell <rockwell_001@yahoo.com> wrote:
> > 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.
>
> 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



Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • Can anyone help me out?
    ... int main{ ... string fname; ... Packet *packet = new Packet; ... packet->package(filename, filesize, buffer); ...
    (comp.unix.programmer)
  • Re: How to split a compressed file programmatically?
    ... value is there in converting that to a string and then back to a long? ... And why allocate a new buffer for each chunk you want to write, ... void splitFile(string path, string path_parts, int size_part) ... just read chunks of the original file until you can't ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to split a compressed file programmatically?
    ... Personally, I would forget about the calculation altogether and just write a loop that keeps writing bytes in chunks as large as you want or however many bytes you have remaining, whichever is less, until you have no more bytes to write. ... The "size_part" variable is already a long; what possible value is there in converting that to a string and then back to a long? ... And why allocate a new buffer for each chunk you want to write, and why does that buffer have to be the length of the original file, and given that you're allocating a new buffer each time, why read the data anywhere other than the beginning of the buffer? ... void splitFile(string path, string path_parts, int size_part) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Writing an int to a file, not quite sure how buffers work.
    ... I still don't know how the buffer works. ... This will write the bit pattern of the int to the file. ... write the string "1234" to represent the integer 1234. ... integer-to string conversion routine, for instance to write in binary rather ...
    (comp.lang.c)