使用凯撒密码在C中进行加密 [英] Encryption in C using a Caesar Cipher

查看:66
本文介绍了使用凯撒密码在C中进行加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求创建一个程序,在该程序中,我必须使用凯撒密码对多条信息进行加密.我了解其背后的概念,但视觉上遇到的麻烦是如何在函数中输入数据.例如,我已将加密的密码保存在文件中("hrkk1"表示"pass1",依此类推).我必须创建一个密码函数才能从scanf和strcmp读取输入,以便它与文件中的内容匹配,从而允许用户登录.

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.

验证用户输入并将"pass1"转换为"hrkk1"以使其与文件中的内容匹配并允许用户登录的最佳方法是什么?

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?

谢谢

这是我到目前为止的代码:

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();
        }                   
    }   

}

推荐答案

您需要使用c中字符的移位.可以在char值上进行简单的加(或减)操作.

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

请注意,您的示例不会移动 数字字符 ,也许该字符也不会超出字母,并且还要考虑大写字母.因此,添加时请注意不要超出大写字母或非大写字母的范围.我的建议是使用ascii表.

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.

这篇关于使用凯撒密码在C中进行加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆