Thread: Strange issue utilizing encrypt() in libcrypt

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    Strange issue utilizing encrypt() in libcrypt

    Hello everybody, it's my first post here
    First of all I'd like to thank the entire board and cprogramming.com for solving so many of my problems with C.
    Now, as for my problem:
    I've been asked to use the encrypt function included in lybcrypt (I have to link it while compiling with -lcrypt) to encrypt some data. I've watched the documentation and everything is clear, but even with the example written there I encouter a strange issue. In order:

    I set the key for the encryption in key[64]
    I set the txt[64] with the characters to encrypt
    I print txt and everything is ok
    I set the key with setkey(key)
    I encrypt txt with encrypt(txt, 0)
    I print txt and nothing is printed, which I guess is ok because of the encrypt function
    I de-encrypt txt with encrypt(txt, 1)
    I print txt... and I get the correct length but no chars, only white spaces (' ')

    What could it be? Does it have something to do with the key?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    How about posting some code to have a look at?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kermit View Post
    How about posting some code to have a look at?
    Alright tough guy, since you are replying to a post from like eight hours ago, someone has to presume that you are an experienced libcrypt user. I'm not, but it looks to me like the OP wasn't asking for much. Why don't you leave this person a few real lines of code yourself as an example?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Considering that you have 600+ posts on this board, I am a little bit perplexed as to your problem here. Have I done something to warrant this sort of response from you? What is your goal? One would almost think that you have a personal problem with me. Do you?

    Generally speaking, when someone asks for help, it is reasonable to expect them to post some code. This is especially true when a post has been sitting for a day. Obviously some action needs to be taken; if someone could have solved this problem, don't you think it would have been done already? By asking for some code, I am attempting to facilitate some assistance for the OP. How does it help to have a post sit ignored for hours and hours?

    You stated that the original poster wasn't asking for much. Why, then, didn't you help? Why didn't you leave a few lines of code? Are you expecting more from me than you are willing to do yourself? Like you, I am not familiar with libcrypt. That is precisely the reason I asked to see some code. You don't have to be an expert in libcrypt to spot errors in C code, but you do need to be able to look at it.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kermit View Post
    Considering that you have 600+ posts on this board, I am a little bit perplexed as to your problem here. Have I done something to warrant this sort of response from you? What is your goal? One would almost think that you have a personal problem with me. Do you?
    Goodness no. Excuse my tone if it seemed brusque.

    Quote Originally Posted by kermit View Post
    By asking for some code, I am attempting to facilitate some assistance for the OP.
    Well, so was I, but of course hoping that you would be the one.

    Quote Originally Posted by kermit View Post
    You stated that the original poster wasn't asking for much. Why, then, didn't you help? Why didn't you leave a few lines of code? Are you expecting more from me than you are willing to do yourself?
    Quote Originally Posted by kermit View Post
    Like you, I am not familiar with libcrypt. That is precisely the reason I asked to see some code. You don't have to be an expert in libcrypt to spot errors in C code, but you do need to be able to look at it.
    Another excellent point. I'm sorry for upsetting you, kermit, as you can see I liked your signature joke, I hope that it is okay.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by MK27 View Post
    I'm sorry for upsetting you, kermit, as you can see I liked your signature joke, I hope that it is okay.
    Not upset - just trying to keep things civil.

    I noticed you had done a bit of a take on my sig. I find the ed joke to be particularly funny, especially the part demonstrating a 'typical novice's session' and following. Except for the 'eat flaming death' bit, that was totally my experience the first time I tried ed. I couldn't turn the darn thing off!
    Last edited by kermit; 01-16-2009 at 07:12 PM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Sorry, my bad
    here is the code

    Code:
    #define _XOPEN_SOURCE
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
    	char key[64] = "123";
    	char txt[64] = "Test sentence";
    	
    	printf("\n");
    	
    	printf("Txt: \"%s\"\n", txt);	/*txt gets printed, no problem*/
    	setkey(key);
    	encrypt(txt, 0);
    	printf("Txt: \"%s\"\n", txt);	/*don't see anything printed, as it should be due to the encryption*/
    	encrypt(txt, 1);
    	printf("Txt: \"%s\"\n", (char*)txt);	/*correct lenth, the "" are in the same place as the first printf, but every character is a ' '*/
    	
    	printf("\n");
    	
    	return 0;
    }

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    The man page for encrypt has to be the worst one *ever*. Have a look at this for a proper example of using encrypt... Note that there is a slight bug in the code though -

    Code:
    txt[ 9 ] = '\0';
    should be

    Code:
    txt[ 8 ] = '\0';
    Last edited by kermit; 01-17-2009 at 12:11 PM.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Thanks kermit, that solved the problem
    you're a pro!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange structure array issue...
    By IndioDoido in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 06:29 AM
  2. strange array issue
    By yaazz in forum C++ Programming
    Replies: 6
    Last Post: 05-09-2007, 11:23 PM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM