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

在C程序中给我相同文本的替换密码,c,encryption,C,Encryption,在过去的几周里,我一直在处理C语言中的加密。我一直在使用一个简单的替换密码,但我遇到了以下代码的问题。虽然程序运行平稳,但文本文件“Message”的内容始终更改为同一段文本:C=Øž挈†。我希望将文件中字符串的每个字符都更改为随机字母 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <type

在过去的几周里,我一直在处理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;
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#包括
#包括
常数int MAXSIZE=50;
无效加密(文件*文件,字符文件[MAXSIZE],int i,int j)
{
File=fopen(“message.txt”、“r+”);
对于(i=0;i<6;i++)
{
文件[i]=rand()+26;
fputc(文件[i],文件);
}
printf(“%s”,文件);
fclose(文件);
返回;
}
int main()
{
int i=0;
int j=0;
字符文件[MAXSIZE];
FILE*FILE=0;
加密(文件,文件,i,j);
系统(“暂停”);
返回0;
}

共享的代码有很多问题,它们是:

  • 如果使用rand()/srand()函数,将如何解密加密文本
  • 文件[i]=rand()+26; 这里您只是转储任何字符值,然后转储到文件中,这没有任何意义,您需要加密一些文本
  • 我附上一个简单的样本代码与凯瑟密码[类型的替代密码]供您参考。其输出如下:

    testtest 123123 testtesttest
    tbwxatzd 164904 AOWPTBWX
    测试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;
    }
    
    #包括
    #包括
    静态字符键[]=“helloworld”;
    int加密(char*aText,char*aKey)
    {
    int lTextlen=strlen(aText);
    int-lKeylen=strlen(aKey);
    int lCount,lShift;
    int lCount1=0;
    对于(lCount=0;lCount如果(aText[lCount]>='a'&&aText[lCount]='a'&&aText[lCount]='0'&&aText[lCount]='a'&&aText[lCount]='a'&&aText[lCount]='0'&&aText[lCount]共享的代码有很多问题,它们是:

  • 如果使用rand()/srand()函数,将如何解密加密文本
  • 文件[i]=rand()+26; 这里您只是转储任何字符值,然后转储到文件中,这没有任何意义,您需要加密一些文本
  • 我随信附上一个简单的Ceaser密码示例代码[替换密码类型],供您参考。其输出如下:

    testtest 123123 testtesttest
    tbwxatzd 164904 AOWPTBWX
    测试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;
    }
    
    #包括
    #包括
    静态字符键[]=“helloworld”;
    int加密(char*aText,char*aKey)
    {
    int lTextlen=strlen(aText);
    int-lKeylen=strlen(aKey);
    int lCount,lShift;
    int lCount1=0;
    对于(lCount=0;lCount如果(aText[lCount]>='a'&aText[lCount]='a'&aText[lCount]='0'&aText[lCount]='a'&aText[lCount]='a'&aText[lCount]='0'&aText[lCount]需要吗?有几件事你需要知道:第一件事是你不必为函数中应该是局部变量的变量使用参数。第二件事是没有参数,你总是会得到相同的“随机变量”第三,你不替换文件中的任何内容,你只是无条件地覆盖文件的内容。最后,使用“随机”数字使文件无法解密。你怎么知道减去什么值才能得到原始字符?总之,我认为你需要后退几步,然后重新开始。哦,最后一件事,(最常见的字符编码方案)仅使用七位,这意味着最大值为127。该函数返回一个介于
    0
    和之间的数字(保证至少
    32767
    )。该范围超出了有效ASCII字符的范围。例如,整数值
    5623
    代表什么字符?需要吗?有几件事你需要知道:第一件事是,你不必为函数中应该是局部的变量使用参数。第二件事是,没有它,你将始终得到相同的结果“随机”数字。第三个是,您不替换文件中的任何内容,只需无条件地覆盖文件内容。最后,使用“随机”数字使文件无法解密。你怎么知道减去什么值才能得到原始字符?总之,我认为你需要后退几步,然后重新开始。哦,最后一件事,(最常见的字符编码方案)仅使用七位,这意味着最大值为127。该函数返回介于
    0
    和(保证至少
    32767
    )之间的数字。该范围超出了有效ASCII字符的范围。例如,整数值
    5623
    代表什么字符?