简体   繁体   中英

C - Encryption Algorithm

I'm trying to make a simple encryption program with C. My aim is to translate abc (it can be any word) to 123 . Then multiply 2 and get 246 then again translate to text, then write on screen bdf . Here is my algorithm which is not working correctly. I entered abc and I got cbc . Can you help me?

int main()
{
    int z,o,c,l,i,j,k,*D;
    char word[10];
char alfabe[24]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','y','z','\0'};

    printf("enter word");
    scanf("%s",word);

    c=strlen(word);
    printf("has %d letters ", c);
    D = (int *) malloc( sizeof(int)*c ); 
    for(i=0;i<c;i++) {
        for(j=0;j<26;j++) {
            if(word[i]==alfabe[j]) {  
                 D[i]=2*(j+1);
                 break;
            }
        }
    }
     printf("\nlast form before translation ");
     for(l=0;l<c;l++) {
       printf("%d",D[l]);  /*it s just for control */

    }

    for(z=0;z<c;z++){
printf("%c",alfabe[o]);
                      o=D[z];
                      word[z]=alfabe[o] ; break; }   




    printf("\nnew form of word: ");
    for(k=0;k<c;k++) {
       printf("%c",word[k]);

    }
scanf("%d");


}

The problem is in the following loop.

for(z=0;z<c;z++){
    printf("%c",alfabe[o]);
    o=D[z];
    word[z]=alfabe[o] ; 
    break; 
}   

Why did you break? It just translates first character. Second, you need to subtract 1 to get the right index of alphabet array(to redo the addition you did).

 word[z]=alfabe[o-1];

Third, you are using o before initializing it? Your compiler should warn you for this. Fourth, why are you storing 27 characters in char array of size 24?

char alfabe[24]={'a','b',...........,'\0'}

And last but not least you need to use modular arithmetic, this wont work if user enters something like xyz .

OK, first of all '\\0' marks the end of an inputed string, you don't need to encrypth this particular character, my suggestion is to place it first in the alfabet so you would get:

alfabet[24] = {'\0', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','y','z'};

This will save you the trouble of substracting 1, so you will have:

for (i = 0; i < c; i++)
{
    for (j = 0; j < 24; j++)
    {
        if (word[i] == alfabet[j])
        {  
            D[i] = 2 * j;
        }
    }
}

In the part where you encode the input. And when you generate the output word:

for (z = 0; z < c; z++)
{
    printf("%c", alfabet[D[z]]);
    word[z] = alfabet[D[z]];
}

No need for o and especially no break; in the loop.

A more efficient way would be to create a function that handles the encryption of the string passed:

char* getEncryptedWord(char* word)
{
    char alfabet[25]={'\0', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v', 'x', 'y','z'};

    int i, j;
    int size = strlen(word);

    char* encryptedWord = (char*) malloc(size + 1);

    for (i = 0; i < size; i++)
    {
            for (j = 0; j <= 25; j++)
            {
                    if (word[i] == alfabet[j])
                    {
                            if (2 * j > 25)
                                    encryptedWord[i] = alfabet[2 * j % 25];
                            else
                                    encryptedWord[i] = alfabet[2 * j];

                            break;
                    }
            }
    }

    encryptedWord[size] = '\0';

    return encryptedWord;
}

I've added 'x' in the alfabet - that is the reason why now there are 25 elements. I'm sure there is one character of the English alphabet missing, but it's 3 AM here and English isn't my primary language. Also, this solution is working on the assumption that the alfabet you provided are the only characters that are supposed to exist in the input and output.

(tip: If you only expect letters AZ, you don't need to loop through the array to find the corresponding number, you may simply get the number by subtracting the numerical value of 'a', and add 1, if you want the letter 'a' to map to 1.)

Modulo arithmetic is mentioned, but that will give you problems because you will loose the 1:1-mapping, ie two letters can end up being translated to the same number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM