Encryption in C using a Caesar Cipher

独自空忆成欢 提交于 2020-01-25 09:34:06

问题


I have been asked to create a program where I have to encrypt multiple pieces of information using a Caesar Cipher. I understand the concept behind it but what I'm having trouble visually is how to enter pieces of data within the function. For example, I have encrypted passwords saved in a file ("hrkk1" meaning "pass1" and so on). I have to create a cipher function to read the input from a scanf and strcmp so it matches what's in the file allowing the user to login.

Whats the best way to validate the user input and make "pass1" turn into "hrkk1" so it matches what's in the file and allows user login?

Thank you

This is the code I have so far:

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

void checkValid(void);
void loginDetails(char username[5][6], char password[5][9]);
void encryption(char username[5][6], char password[5][9]);

int main(void)
{
    FILE *EP;
    FILE *UN;
    char username[5][6];
    char password [5][9], ch, key;
    EP = fopen("encrypted_passwords.txt", "r");
    fscanf(EP, "%s %s %s %s %s", password[0], password[1], 
    password[2], password[3], password[4]);
    fclose(EP);
    UN = fopen("username.txt", "r");
    fscanf(UN, "%s %s %s %s %s", username[0], username[1], username[2], 
    username[3], username[4]);
    fclose(UN);

    printf("Welcome.");
        loginDetails(username, password);

    return 0;
    }

void loginDetails(char username[5][6], char password[5][9])
{
    int i;
    char nurseUsername[6];
    char nursePassword[6];
    bool useValid = 0;
    bool passValid = 0;

    printf("Please Enter your username: \n");
    scanf("%s", nurseUsername);
    for (i = 0; i < 5; i++)
    {
        if(strcmp(nurseUsername, username[i]) == 0)
        {
            useValid = 1;
        }
    }
    if(useValid != 1)
    {
        printf("\nError. Invalid Username. Returning to menu.\n");
        Sleep(1000);
        system("cls");
        main();
    }
    else
    {
        printf("\nPlease enter your password: \n");
        scanf("%s", nursePassword);
    }
    for(i = 0; i < 5; i++)
    {
        if((strcmp(nurseUsername, username[i]) == 0) && 
        (strcmp(nursePassword, password[i]) == 0))
        {
            passValid = 1;
        }
        if(passValid != 1)
        {
            printf ("Error. Invalid Password. Returning to menu.\n");
            Sleep(1000);
            system("cls");
            main();
        }
        else 
        {
            printf("\nLogin Successful. Loading menu.\n");
            Sleep(1000);
            system("cls");
            patientEntry();
        }                   
    }   

}

回答1:


You need to use the shifting of the character in c. This is possibile with a simple addition( or subtraction) on a char value.

Pay attention your example doesn't shift the number character and maybe also the character doesn't go out the alphabet, and it take in consideration also the capital letters. So pay attention when you do the addition to not exceed the range of capital or non capital letter. My suggestion is to use ascii table.



来源:https://stackoverflow.com/questions/59253431/encryption-in-c-using-a-caesar-cipher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!