LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices

Closed Thread
 
Search this Thread
Old 07-23-2004, 01:13 PM   #1
rockwell_001
LQ Newbie
 
Registered: Jun 2004
Posts: 23

Rep: Reputation: 15
encrypt and decrypt using encrypt(char block[64], int edflag)


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)
{
const char *key = "keyusedtoencrypt";
char txt[64]="myblockofdata";

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.
 
Old 07-23-2004, 11:04 PM   #2
GodSendDeath
Member
 
Registered: Mar 2004
Posts: 71

Rep: Reputation: 15
Hey, I was working on an encryption program earlier this week. Try the following, they helped me....


instead of char key*; use char key[64];

clear the buffer:

bzero(&key, sizeof(key));

then put in your key"

#include <string.h>

strcat(key, "thisismystring");

I got a feeling just changing *key to key will work, but the rest are safeguards I used for stupid memory and null character problems I always seem to run into. Your key can be anything, not just 1's or 0's. I couldnt compile your program properly...... so let me know if this works for you.

-GSD
 
Old 08-30-2009, 06:36 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
The encrypt/decrypt actually take an array of bits, not of bytes (ie, it is a 64 bit encryption block). The bits are stored in bytes (not packed). Your code was working, but the encryption ignored the high order bits, so the results at each stage consisted just of 'characters' 0 and 1. When you printed these, nothing was shown, because character 1 is unprintable and character 0 is the end of string.

Incidentally, you also have a bug in the assigment to the key. You have a pointer to a character array that is shorter than 64, so the fetch of the key will run off the end of the array.

Here is a changed version of your code:
Code:
#include <stdio.h>
#include <unistd.h>
#include <crypt.h>

int main(void) 
{
char key[64] =
{
	0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1,
};
char txt[64]=
{
	0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0,
	0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1,
	1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0,
	0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1,
};

size_t i;

printf("Before encrypting ");
for (i = 0; i < sizeof(txt); ++i)
	printf("%c", txt[i]+'0');
printf("\n");
setkey(key);

printf("After encrypting  ");
encrypt(txt, 0);
for (i = 0; i < sizeof(txt); ++i)
	printf("%c", txt[i]+'0');
printf("\n");


printf("After decrypting  ");
encrypt(txt, 1); 
for (i = 0; i < sizeof(txt); ++i)
	printf("%c", txt[i]+'0');
printf("\n");
return 0;
}
Incidentally, you should only use this single DES encryption for low security applications (because the key length is relatively short). You can get improved security with the same library calls by implementing triple-DES. Also, if you are encrypting multiple blocks, have a look at cbc_crypt.

Last edited by neonsignal; 08-30-2009 at 06:53 AM.
 
Old 08-30-2009, 09:16 AM   #4
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
neonsignal, please don't necropost. We aren't too fond of zombie threads around here. Anyone wishing to discuss the code generously provided by neonsignal please use this thread.

Last edited by win32sux; 08-30-2009 at 09:18 AM.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use kgpg to encrypt and decrypt emails? ginda Linux - Security 6 03-16-2005 09:42 PM
how to encrypt initrd.img and decrypt in vmlinuz. AshesOfTime Programming 1 12-09-2004 03:48 PM
crypt vs encrypt liguorir Linux - Software 3 05-20-2004 10:38 PM
2048-bit encrypt/decrypt help Nappa Slackware 1 11-20-2003 11:04 AM
Encrypt vivekind Linux - General 1 01-16-2002 03:23 PM


All times are GMT -5. The time now is 03:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration