C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C Text Encryption Posted by psychofox19 on 9 Apr 2009 at 4:23 PM
Hi, I have a program in C that takes in a users password of type:
char *userPass;

I need to store the password for the program inside the source code in an encrypted format that can be compared by the user's password entry once that too has been encrypted.

I need any kind of algorithm be it simple or otherwise as I can just choose the best one to suit my needs and experimentation. I've tried finding an inbuilt function in C but the only one i found works best only with numbers.

Any help will be appreciated even if you just point me in the right direction.
Report
Re: C Text Encryption Posted by psychofox19 on 13 Apr 2009 at 5:45 PM
Is there anybody that is willing to explain how I can do some simple encryption algorithms on strings in C?
Report
Re: C Text Encryption Posted by AsmGuru62 on 14 Apr 2009 at 4:49 AM

The simplest way is to XOR each character of text with some value, say 0xAB:

void encrypt (char* text, int len)
{
    unsigned char mask = 0xAB;
    int i;

    for (i=0; i<len; i++)
    {
        text [i] ^= mask;
    }
}

After encryption, you need to keep the length of the text together with the encrypted text - to make comparisons, because after encryption text may be no longer terminated with zero and strcmp() type of functions will be useless and dangerous to use - you will have to use the memcmp() with length.

XOR-ing with the same value is not strong. Try to add some value to the mask for each character to make it stronger:

void encrypt (char* text, int len)
{
    unsigned char mask = 0xAB;
    int i;

    for (i=0; i<len; i++, mask += 9)
    {
        text [i] ^= mask;
    }
}

In professional programs usually MD5 algorithm is used to get a signature of the data:

http://en.wikipedia.org/wiki/MD5
Report
Re: C Text Encryption Posted by psychofox19 on 14 Apr 2009 at 3:39 PM
Okay i'm using what you gave me to firstly convert the original password thats to be stored in the source code so i can record it and keep that in there instead.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define password "password"

char* encrypt (char* text, int len)
{
    unsigned char mask = 0xAB;
    int i;

    for (i=0; i<len; i++, mask += 9)
    {
        text [i] ^= mask;
    }
    return text;
}

int main()
{
  char* password2 = encrypt(password,8);
  printf("\nThe new password is: \"%s\" and the length is %d |",password2, strlen(password2));
  return 0;
}


I get this error when i run it:

10 [main] testenc 3276 _cygtls::handle_exceptions: Error while dumping stat
e (probably corrupted stack)
Segmentation fault (core dumped)

Report
Re: C Text Encryption Posted by AsmGuru62 on 15 Apr 2009 at 4:40 AM
Try that:
char pass [10];
int len;

strcpy (pass1, "password");
len = strlen (pass1);
encrypt (pass1, len);

After encryption - you can't use any string functions on it (you tried to use "%s" to print it, but "%s" assumes the proper string and it is no longer a proper string - it is just a line of bytes)
Report
Re: C Text Encryption Posted by psychofox19 on 15 Apr 2009 at 8:17 AM
I'm having trouble matching the encryptedPass to userPass coz I can't use "password" inside the code, but I've encrypted the userPass and a 3rd one that is an encrypted version of "password" and they match fine.

The only thing i can think of is that i need to use an escape character to make ÛÕε¸·"Z usable in the strcpy() so it's ÛÕε¸·\"Z

  int i = 1;
  char *userPass;
  char encryptedPass[20];
  char testPass[20];
  
  strcpy(encryptedPass,"ÛÕε¸·\"Z");
  strcpy(testPass,"password");

  while(i < 4){
    if(i < 3)
      printf("\nAttempt %d\n", i);
    else
      printf("\nLast attempt!\n");  
    
    userPass = getpass("Enter password: ");
    encrypt(userPass,strlen(userPass));
    encrypt(testPass,strlen(testPass));
   
    
    if(memcmp(userPass,encPass,8) == 0)
      return;
      
    i++;  
  }
Report
Re: C Text Encryption Posted by AsmGuru62 on 16 Apr 2009 at 4:16 AM
: I'm having trouble matching the encryptedPass to userPass coz I
: can't use "password" inside the code, but I've encrypted the
: userPass and a 3rd one that is an encrypted version of "password"
: and they match fine.
:
: The only thing i can think of is that i need to use an escape
: character to make ÛÕε¸·"Z usable in the strcpy() so it's ÛÕε¸·\"Z
:
:
: 
:   int i = 1;
:   char *userPass;
:   char encryptedPass[20];
:   char testPass[20];
:   
:   strcpy(encryptedPass,"ÛÕε¸·\"Z");
:   strcpy(testPass,"password");
:   encrypt(testPass,strlen(testPass));
: 
:   while(i < 4){
:     if(i < 3)
:       printf("\nAttempt %d\n", i);
:     else
:       printf("\nLast attempt!\n");  
:     
:     userPass = getpass("Enter password: ");
:     encrypt(userPass,strlen(userPass));
:     //encrypt(testPass,strlen(testPass));
:     // This ^^^ should be moved out of loop
:    
:     
:     if(memcmp(userPass,encPass,8) == 0)
:       return;
:       
:     i++;  
:   }
: 
:
There are few more problems: the memcmp() uses 8 as a length, but if user enters a password of 6 chars - then the check may fail in some cases.

The pseudo-code of this looks like this:

char correctPass [20];
char enteredPass [20];
int lenCorrectPass;
int nAttempts = 3;
int bMatch = 0;

strcpy (correctPass, "password");
lenCorrectPass = strlen (correctPass);
encrypt (correctPass, lenCorrectPass);

while (nAttempts--)
{
	// read the text from user into 'enteredPass'

	encrypt (enteredPass, strlen (enteredPass));
	if (memcmp (enteredPass, correctPass, lenCorrectPass) == 0)
	{
		bMatch++; break;
	}
}

if (! bMatch) printf ("\n\nUnable to login after 3 attempts!");
else printf ("\n\nCongrats! You are logged in!");
Report
Re: C Text Encryption Posted by psychofox19 on 16 Apr 2009 at 4:49 AM
I know i shouldn't have testPass i was just using it in my debugging to find out whether i was comparing them wrong, but it seems like encryptedPass is changed from just an encrypted version of the text "password" because writng it in the source code requires an escape character for the " in: ÛÕε¸·"Z



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.