c - 如何在 C 语言中对文件运行凯撒密码

标签 c encryption

例如,如何在文本文件上运行凯撒密码。在 C 语言中,我了解了如何使用字符串。我如何读取文本文件,然后接受它作为运行密码的文本?

最佳答案

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

int main()
    {
    int rCode=0;
    char *filePath="theTextFile";
    FILE *fp=NULL;
    struct stat statBuf;
    void *fileContent=NULL;

确定文件大小:

    if((-1) == stat(filePath, &statBuf))
       {
       rCode=errno;
       fprintf(stderr, "stat() failed.  errno[%d]\n", errno);
       goto CLEANUP;
       }

分配足够的内存来保存文件。

    fileContent=malloc(statBuf.st_size);
    if(NULL == fileContent)
       {
       rCode=ENOMEM;
       fprintf(stderr, "malloc() failed.\n");
       goto CLEANUP;
       }

打开文件。

    fp=fopen(filePath, "rb");
    if(NULL == fp)
       {
       rCode=errno;
       fprintf(stderr, "fopen() failed.  errno[%d]\n", errno);
       goto CLEANUP;
       }

将文件内容读入分配的内存中。

    if(statBuf.st_size != fread(fileContent, 1, statBuf.st_size, fp))
       {
       rCode=errno;
       fprintf(stderr, "fread() failed.  errno[%d]\n", errno);
       goto CLEANUP;
       }

对文件内容的内存镜像运行密码。

    CaesarCipher(fileContent);
    ...

CLEANUP:

关闭文件

    if(fp)
       fclose(fp);

释放分配的内存。

    if(fileContent)
       free(fileContent);

    return(rCode);
    }   

关于c - 如何在 C 语言中对文件运行凯撒密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23234865/

相关文章:

c - 为什么这段代码是正确的?

c# - 使用 BouncyCaSTLe PGP 解密文件时出现异常

php - 使 RSA 加密在 Python (PyCrypto) 和 PHP (OpenSSL) 中兼容

c++ - Caesar Cipher C++(使用字符指针和移位作为参数)

c - mac osx 开发环境

c - C 表达式 ((void(*)(void))0)(); 是什么意思?意思?

php - 我如何允许使用 CakePHP 访问我的 .well-known/目录?

php - 学说加密专栏

c - 使用字符串作为数组索引标识符(无循环)

c - 这是什么函数调用?