简体   繁体   中英

Encrypt/Decrypt - C Language

So, I'm here to ask about an encrypt and decrypt program.

That's the deal, I will ask for a key in the start of the program like: AgFvcD So after the key I'll ask for the message that they want to be encrypted: Hello World!

Then I think about use ASCII code to make this as

I'll take the value of each key(AgFvcD) character but as decimal value and multiply for each message character as decimal too.

(A * H) - (g * e) - (F * l) - (v * l) - (D * o) and so on.. even if I set the key with numbers(123) it still having their Decimal ASCII value, not the number in itself.

The problem is in the multiplication, because I was thinking to use two vectors one to receive the key and the other to multiply the key over the message. But I don't know how to make the vector multiplication, even if the key is shorter than the message, it should go back to the first key character and keep going, till the message get all encrypted.

Got it?

If it is not well explained, I'll try to make it simpler than that.

Thanks since then.

My code:

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

void Encrypt(){
    char text[256] = {0};
    int textc[256] = {0};
    char key[] = {0};
    int keyc[] = {0};
    int i;
    system("cls");
    printf("Type the encrypt key: ");
    fgets(key, 256, stdin);   //Recebe a chave de criptografia
    printf("\n*\n");   //Avisa sobre o limite de caracteres
    printf("Type the message: ");
    fgets(text, 256, stdin);  //Recebe a mensagem a ser criptografada
    printf("Encrypting...\n");    //Charme ; )
    //sleep(3);
    for(i=0; i<strlen(text); i++){
    textc[i]=text[i];
    key[i]= keyc[i];
    printf("\n %d \n", textc[i]*keyc[i]);
    }
    system("Pause");
    return Encrypt();
}

Some characters on the promp when I printf get a memory trash but the most of them bring me the right value, test with encript key: AAA and message: AAAAAAA

it will return some 4225 that's the result of 65 * 65.

The problem now is to not get back these values...

There are a few problems here:

char key[] = {0};
int keyc[] = {0};

Because you don't specify a size for these arrays, it allocated just enough size for the initialized elements, meaning each array is of length 1. When you write your key, you overrun the length of these arrays. So you need to explicitly set the size:

char key[256] = {0};
int keyc[256] = {0};

When you use fgets the trailing newline gets appended to the string, so you need to strip it off:

fgets(key, 256, stdin);
key[strlen(key)-1] = '\0';   // strip the newline added by fgets
...
fgets(text, 256, stdin);
text[strlen(text)-1] = '\0';  // strip the newline added by fgets

In order to reuse key material when the key is shorter than the message, you need to separately keep track of the key index and reset it to zero when it gets too big:

for(i=0,j=0; i<strlen(text); i++){
    textc[i]=text[i];
    keyc[i]= key[j];   // You had these mixed up.  You were doing key[i] = keyc[i]
    printf("\n %d \n", textc[i]*keyc[i]);
    j++;
    if (j >= strlen(key)) j=0;
}

For efficiency's sake, you probably want to store strlen(text) and strlen(key) in separate variables before the loop so you don't recalculate them each time. Just be sure to do this after you strip the newlines added by fgets .

One more thing: get rid of the return Encrypt() call at the end. That has the function call itself recursively with no escape clause, so you get an infinite loop.

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