C Text Encryption - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C Text Encryption

psychofox19psychofox19 Posts: 41Member
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.

Comments

  • psychofox19psychofox19 Posts: 41Member
    Is there anybody that is willing to explain how I can do some simple encryption algorithms on strings in C?
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]
    The simplest way is to XOR each character of text with some value, say 0xAB:
    [/color]
    [code]
    void encrypt (char* text, int len)
    {
    unsigned char mask = 0xAB;
    int i;

    for (i=0; i<len; i++)
    {
    text [i] ^= mask;
    }
    }
    [/code]
    [color=Blue]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:
    [/color]
    [code]
    void encrypt (char* text, int len)
    {
    unsigned char mask = 0xAB;
    int i;

    for (i=0; i<len; i++, mask += 9)
    {
    text [i] ^= mask;
    }
    }
    [/code]
    [color=Blue]In professional programs usually MD5 algorithm is used to get a signature of the data:

    [link=http://en.wikipedia.org/wiki/MD5]http://en.wikipedia.org/wiki/MD5[/link]
    [/color]
  • psychofox19psychofox19 Posts: 41Member
    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.

    [code]
    #include
    #include
    #include
    #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("
    The new password is: "%s" and the length is %d |",password2, strlen(password2));
    return 0;
    }
    [/code]

    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)

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Try that:[/color]
    [code]
    char pass [10];
    int len;

    strcpy (pass1, "password");
    len = strlen (pass1);
    encrypt (pass1, len);
    [/code]
    [color=Blue]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)[/color]
  • psychofox19psychofox19 Posts: 41Member
    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
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : 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
  • psychofox19psychofox19 Posts: 41Member
    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:
Sign In or Register to comment.