Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-23-2004, 01:13 PM
|
#1
|
LQ Newbie
Registered: Jun 2004
Posts: 23
Rep:
|
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.
|
|
|
07-23-2004, 11:04 PM
|
#2
|
Member
Registered: Mar 2004
Posts: 71
Rep:
|
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
|
|
|
08-30-2009, 06:36 AM
|
#3
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
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.
|
|
|
08-30-2009, 09:16 AM
|
#4
|
LQ Guru
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870
|
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.
|
|
|
All times are GMT -5. The time now is 09:36 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|