DEV Community

Jochem Stoel
Jochem Stoel

Posted on

[C language] Can you write a string encode function based on this simple decode function?

Hi. From the documentation of CCProxy, I got this function that decodes a user password. (string with numbers to string with characters)
I need the exact opposite as well, the code needed to encode the password. (to a 'number string').

To someone who knows a little C, this should be fairly trivial. If you can reproduce this function in JavaScript that would be even more terrific. That way I don't have to use a command line application child process to do this simple encoding/decoding.

Can you help me? Thanks!

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define _MAX_BUF_LEN 2014

static char* PasswordDecode(char * szPassword)
    char szEncode[1024];
    char strDecodePass[_MAX_BUF_LEN + 1] = { "" }, strPass[_MAX_BUF_LEN + 1] = { "" };

    strcpy(strDecodePass, szPassword);

    for (unsigned int i = 0; i < strlen(strDecodePass) / 3; i++)
        char szCode[_MAX_BUF_LEN + 1];
        strcpy(szCode, strDecodePass + i * 3);
        szCode[3] = 0;
        int nCode = atoi(szCode);
        nCode = 999 - nCode;
        sprintf(szEncode, "%c", nCode);
        strcat(strPass, szEncode);

    strcpy(szPassword, strPass);
    return szPassword;
Enter fullscreen mode Exit fullscreen mode
int main(int argc, char **argv)

    return 0;
Enter fullscreen mode Exit fullscreen mode

To test if your Encode function is correct, the following plain text passwords and their encoded equivalent.



Enter fullscreen mode Exit fullscreen mode

Top comments (3)

courier10pt profile image
Bob van Hoove

What's holding you back from translating the function yourself?

My honest advice is to give it a go, try and see if can google just enough C to understand the code and translate it, line by line. Once you have done that there may still be a bug. If you can post that code it will be much easier to help you get to the finish. Finding a bug is fun, like a little puzzle. Also a much smaller effort than translating the whole thing.

alephnaught2tog profile image
Max Cerrina

Are the only allowed characters letters and digits? Case?

jochemstoel profile image
Jochem Stoel

I am not sure. I think most utf8 characters are allowed. Some would have to check the CCProxy documentation to be sure. Can't you tell by the code in my post?

In my case, if a-zA-Z0-9 works then that is all I need.