c - 替换密码在 C 程序中给我相同的文本

标签 c encryption

在过去的几周里,我一直在用 C 语言进行加密。我一直在使用简单的替换密码,但我在使用以下代码时遇到了问题。尽管程序运行顺利,但文本文件“Message”的内容总是变为同一段文本:C=Øžû†。我希望将文件中字符串的每个字符更改为随机字母。

#define _CRT_SECURE_NO_WARNINGS
#include  <stdio.h>
#include  <stdlib.h>
#include  <Windows.h>
#include  <type.h>
#include <string.h>

const int MAXSIZE = 50;

void Encrypt(FILE *File, char file[MAXSIZE], int i, int j)
{
    File = fopen("message.txt", "r+");
    for (i = 0; i < 6; i++)
    {
        file[i] = rand() + 26;
        fputc(file[i], File);
    }

    printf("%s", file);

    fclose(File);
    return;
}

int main()
{
    int i = 0;
    int j = 0;
    char file[MAXSIZE];
    FILE *File = 0;

    Encrypt(File, file, i, j);

    system("pause");
    return 0;
}

最佳答案

分享的代码有不少问题,它们是:

  1. 如果您使用 rand()/srand() 函数,您将如何解密加密文本。
  2. 文件[i] = rand() + 26; 在这里,您只是转储任何字符值,而不是转储到文件中,这没有任何意义,您需要加密一些文本。

我附上一个带有 Ceaser cipher[type of substitution ciper] 的简单示例代码供您引用。其输出如下:

测试测试 123123 测试测试
tbwxatzd 164904 AOWPTBWX
testtest 123123 测试测试

#include <stdio.h>
#include <string.h>

static char key[] = "helloworld";

int Encrypt(char *aText,char *aKey)
{
    int lTextlen    = strlen(aText);
    int lKeylen     = strlen(aKey);

    int lCount,lShift;
    int lCount1 = 0;

    for(lCount = 0; lCount < lTextlen;lCount++)
    {
        if(aText[lCount] >= 'a' && aText[lCount] <= 'z')
        {
            lShift = aKey[lCount1] % 26;
            aText[lCount] = ((aText[lCount] + lShift - 97) % 26) + 97;
        }
        else if(aText[lCount] >= 'A' && aText[lCount] <= 'Z')
        {
            lShift = aKey[lCount1] % 26;
            aText[lCount] = ((aText[lCount] + lShift - 65) % 26) + 65;
        }
        else if(aText[lCount] >= '0' && aText[lCount] <= '9')
        {
            lShift = aKey[lCount1] % 10;
            aText[lCount] = ((aText[lCount] + lShift - 48) % 10) + 48;
        }
        else
        {
        }
        lCount1 = (lCount1 + 1) % lKeylen;
    }
}

int Decrypt(char *aText,char *aKey)
{
    int lTextlen    = strlen(aText);
    int lKeylen     = strlen(aKey);

    int lCount,lShift;
    int lCount1 = 0;

    for(lCount = 0; lCount < lTextlen;lCount++)
    {
        if(aText[lCount] >= 'a' && aText[lCount] <= 'z')
        {
            lShift = 26 - (aKey[lCount1] % 26);
            aText[lCount] = ((aText[lCount] + lShift - 97) % 26) + 97;
        }
        else if(aText[lCount] >= 'A' && aText[lCount] <= 'Z')
        {
            lShift = 26 - (aKey[lCount1] % 26);
            aText[lCount] = ((aText[lCount] + lShift - 65) % 26) + 65;
        }
        else if(aText[lCount] >= '0' && aText[lCount] <= '9')
        {
            lShift = 10 - (aKey[lCount1] % 10);
            aText[lCount] = ((aText[lCount] + lShift - 48) % 10) + 48;
        }
        else
        {
        }
        lCount1 = (lCount1 + 1) % lKeylen;
    }
}
int main()
{
    char plaintext[] = "testtest 123123 TESTTEST";
    printf("%s\n",plaintext);
    Encrypt(plaintext,key);
    printf("%s\n",plaintext);
    Decrypt(plaintext,key);
    printf("%s\n",plaintext);
    return 0;
}

关于c - 替换密码在 C 程序中给我相同的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301060/

相关文章:

在 C 中有效地将十六进制字符串转换为整数?

encryption - AES加密,什么是公钥和私钥?

algorithm - 这是某种形式的加密数据吗?

android - 堆损坏 - Android native 代码中的 SEGV_MAPERR

php - 将加密 key 从 Laravel 5.2 迁移到 5.3

连接有错误的字符串

c - 如何使用调色板设置 libpng 背景透明度?

c - ld 在链接动态共享库时做什么?

c - 为什么我的程序循环次数太多?

java - 尝试使用 Secretkey 解密数据时返回错误