求帮写一段位移加密的解密代码并添加到原代码中,顺便问一问文件乱码的形式怎么解决






#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
 
void printMenu(void);
int checkPwd(int type);
int encryptFile(char *sourcefile, char *secretKey, char *targetFile, int encryptionMethod);
char redo(void);
 
int main() {
    char sourcefile[30], targetFile[30], secretKey[21], action;
    int encryptionMethod; // 加密方式
 
    while (1) {
        system("cls");
        printMenu();
        scanf("%c", &action);
        fflush(stdin);
        system("cls");
 
        switch (action) {
            case 'z':
                exit(0);
                break;
            case 'a':
                while (1) {
                    printf("输入要加密的文件名(含路径):");
                    scanf("%s", sourcefile);
                    printf("输入密钥:");
                    scanf("%s", secretKey);
                    printf("加密后的文件名(含路径):");
                    scanf("%s", targetFile);
 
                    printf("选择加密方式:\n");
                    printf("1. 异或加密\n");
                    printf("2. 其他加密方式\n");
                    printf("请选择:");
                    scanf("%d", &encryptionMethod);
 
                    if (encryptFile(sourcefile, secretKey, targetFile, encryptionMethod)) {
                        printf("文件[%s]加密成功,保存在[%s]。\n", sourcefile, targetFile);
                    }
 
                    if (redo() == 'a') {
                        system("cls");
                    } else {
                        break;
                    }
                }
                break;
            case 'b':
                while (1) {
                    printf("输入要解密的文件名(含路径):");
                    scanf("%s", sourcefile);
                    printf("输入密钥:");
                    scanf("%s", secretKey);
                    printf("解密后的文件名(含路径):");
                    scanf("%s", targetFile);
 
                    printf("选择解密方式:\n");
                    printf("1. 异或解密\n");
                    printf("2. 其他解密方式\n");
                    printf("请选择:");
                    scanf("%d", &encryptionMethod);
 
                    if (encryptFile(sourcefile, secretKey, targetFile, encryptionMethod)) {
                        printf("文件[%s]解密成功,保存在[%s]。\n", sourcefile, targetFile);
                    }
 
                    if (redo() == 'a') {
                        system("cls");
                    } else {
                        break;
                    }
                }
                break;
            case 'c':
                               while(1){    
struct stu
{
    char name[10];
    int num;
    int age;
    char addr[15];
}boya[2],boyb[2],*pp,*qq;
{
    FILE *fp;
    char ch;
    int i;
    pp=boya;
    qq=boyb;
    if((fp=fopen("D:\ceshi.txt","wb+"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
    }
    printf("\ninput data\n");
    for(i=0;i<2;i++,pp++)
    {
        scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr);
    }
    pp=boya;
    fwrite(pp,sizeof(struct stu),2,fp);
    //将文件指针重新指向一个流的开头
    rewind(fp);
    fread(qq,sizeof(struct stu),2,fp);
    printf("\n\nname\tnumber      age      addr\n");
    for(i=0;i<2;i++,qq++)
    {
        printf("%s\t%5d%7d     %s\n",qq->name,qq->num,qq->age,qq->addr);
    }
    fclose(fp);
}
    if(redo() == 'a'){  // 重新输入信息
                        system("cls");
                    }else{  // 回到主菜单
                        break;
                    }
                }
                break;
            default:
                printf("没有相应的菜单!按任意键回到主菜单...");
                getch();
        }
    }
 
    return EXIT_SUCCESS;
}
 
void printMenu() {
    printf("***************** 文本加密解密软件 *****************\n");
    printf("*                                                  *\n");
    printf("*      请从下面的菜单中选择你要进行的操作:        *\n");
    printf("*      a. 文件加密                                 *\n");
    printf("*      b. 文件解密                                 *\n");
    printf("*      c. 将信息存放到文件中                       *\n");
    printf("*      z. 退出系统                                 *\n");
    printf("*                                                  *\n");
    printf("****************************************************\n");
}
 
/**
 * 加密/解密文件
 *
 * @param   sourcefile    要加密/解密的文件名
 * @param   secretKey     密钥
 * @param   targetFile    加密/解密后要保存的文件名
 *
 * @return  加密成功或失败的数字表示
     0:加密失败
     1:加密成功
**/
int encryptFile(char *sourcefile, char *secretKey, char *targetFile, int encryptionMethod) {
    FILE *fpSource, *fpTarget;
    char buffer[21];
    int readCount, keyLen = strlen(secretKey), i;
 
    fpSource = fopen(sourcefile, "rb");
    if (fpSource == NULL) {
        printf("文件[%s]打开失败,请检查文件路径和名称是否输入正确!\n", sourcefile);
        return 0;
    }
 
    fpTarget = fopen(targetFile, "wb");
    if (fpTarget == NULL) {
        printf("文件[%s]创建/写入失败!请检查文件路径和名称是否输入正确!\n", fpTarget);
        return 0;
    }
 
    while ((readCount = fread(buffer, 1, keyLen, fpSource)) > 0) {
        if (encryptionMethod == 1) { // 异或加密
            for (i = 0; i < readCount; i++) {
                buffer[i] ^= secretKey[i];
            }
        } else {
         //另一种加密方式  
        }
 
        fwrite(buffer, 1, readCount, fpTarget);
    }
 
    fclose(fpSource);
    fclose(fpTarget);
    return 1;
}
 
char redo() {
    char action;
    printf("\n接下来你希望:\n");
    printf("a. 继续当前操作\n");
    printf("b. 回到主菜单\n");
 
    while (1) {
        fflush(stdin);
        scanf("%c", &action);
        fflush(stdin);
 
        if (action != 'a' && action != 'b') {
            printf("没有相应的菜单,请重新选择。\n");
        } else {
            return action;
            break;
        }
    }
}

或者说直接从原位置再写一段用到结构体的类似代码

img

img

在//另一种方式里写一段加密代码,你可以使用以下代码来实现异或加密:

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

void encrypt(char *str, char *key) {
    int keyLen = strlen(key);
    int strLen = strlen(str);
    
    for (int i = 0; i < strLen; i++) {
        str[i] ^= key[i % keyLen];
    }
}

int main() {
    char str[] = "Hello, world!";
    char key[] = "secret";
    
    encrypt(str, key);
    
    printf("Encrypted string: %s\n", str);
    
    return 0;
}

这段代码使用异或运算对字符串进行加密。加密函数encrypt接受两个参数,分别是要加密的字符串和密钥。在循环中,逐个字符对字符串进行异或运算,密钥循环使用。最后打印出加密后的字符串。

你可以根据自己的需求修改这段代码,将其嵌入到你的程序中。

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7469770
  • 这篇博客也不错, 你可以看下【C语言链地址法】散列表/哈希表的创建、插入、查找、删除
  • 除此之外, 这篇博客: 哈夫曼编/译码器的设计与实现(结合文件)中的 一、问题描述: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。但是,这要求在发送端通过一个编码系统对待传数据预先编码,在接收端将传来的数据进行译码。系统应该具有如下的几个功能:接收原始数据、编码、译码、打印编码规则。

  • 以下回答由chatgpt基于相关博客总结生成:

    根据问题描述,你想在一个程序中实现加密代码,并且解决输出到文件时出现乱码的问题。目前,你给出的参考资料是关于冒泡排序的代码。接下来,我将给出解决方案。

    首先,我们需要明确加密的具体要求和方式。对于加密代码的实现,有多种方式可供选择,例如替换、移位、异或等。请提供更具体的加密要求。

    其次,对于输出到文件时出现乱码的问题,常见的原因是编码不匹配。你可以尝试使用不同的编码方式来解决该问题。具体的解决方案如下:

    1. 加密代码实现:

    根据具体的加密要求,我们可以选择合适的加密方式。以下是一种示例的加密方法(移位加密)的代码实现:

    #include <stdio.h>
    
    void encrypt(char *str, int shift) {
        char *ptr = str;
    
        while (*ptr != '\0') {
            if (*ptr >= 'a' && *ptr <= 'z') {
                *ptr = (*ptr - 'a' + shift) % 26 + 'a';
            } else if (*ptr >= 'A' && *ptr <= 'Z') {
                *ptr = (*ptr - 'A' + shift) % 26 + 'A';
            }
            ptr++;
        }
    }
    
    int main() {
        char str[100];
        int shift;
    
        printf("请输入要加密的字符串:");
        fgets(str, sizeof(str), stdin);
    
        printf("请输入移位的大小:");
        scanf("%d", &shift);
    
        encrypt(str, shift);
    
        printf("加密后的字符串:%s\n", str);
    
        return 0;
    }
    

    上述代码实现了一个移位加密函数encrypt,它接受一个字符串和一个移位大小作为参数,并将字符串中的字母进行移位加密。主函数main用于接受输入,并调用encrypt函数进行加密操作。最后输出加密后的字符串。

    1. 解决输出到文件时乱码问题:

    通常情况下,输出到文件会使用文本文件的方式,而文本文件有不同的编码方式。常见的编码方式有ASCII、UTF-8、GBK等。如果输出到文件时出现乱码,可能是因为编码方式不匹配。

    你可以尝试使用不同的编码方式来解决该问题。以下是一个示例的使用UTF-8编码方式输出文件的代码:

    #include <stdio.h>
    
    int main() {
        FILE *file;
        file = fopen("output.txt", "w, ccs=UTF-8");
    
        if (file == NULL) {
            printf("无法打开文件");
            return 1;
        }
    
        fprintf(file, "这是一段中文文本");
    
        fclose(file);
    
        return 0;
    }
    

    上述代码使用fopen函数打开一个名为"output.txt"的文件,并指定编码方式为UTF-8("ccs=UTF-8")。然后使用fprintf函数将一段中文文本输出到文件中。最后使用fclose函数关闭文件。

    确保文件编码方式与你的系统或其他读取文件的程序所使用的编码方式一致,可以避免输出乱码的问题。

    以上是针对你提出的问题给出的解决方案。如果你有更具体的加密要求或其他疑问,请提供更多信息,我将尽力帮助你。

另一种加密方式。以下是一个示例,位移加密算法:

else {
    // 位移加密
    for (i = 0; i < readCount; i++) {
        buffer[i] = buffer[i] + keyLen;
    }
}

这段代码将密钥的长度(keyLen)与每个输入字符进行相加,实现了一种简单的位移加密。
至于输出文件乱码的问题,如果你希望以文本格式进行输出,可以尝试打开输出文件时指定编码方式,并使用文本输入输出函数进行读写操作。以下是一个示例:


#include <locale.h>
// 在主函数中设置本地化环境
setlocale(LC_ALL, "");
// ...
// 打开输出文件时指定编码方式
fpTarget = fopen(targetFile, "w, ccs=UTF-8");

通过使用fopen函数的"w, ccs=UTF-8"参数,以将输出文件以UTF-8编码方式打开,这应该能够正确处理各种文本字符。

帮一下吧

img

参考如下

img

 
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
 
void printMenu(void);
int checkPwd(int type);
int encryptFile(char *sourcefile, char *secretKey, char *targetFile, int encryptionMethod);
char redo(void);
 
int main() {
    char sourcefile[30], targetFile[30], secretKey[21], action;
    int encryptionMethod; // 加密方式
 
    while (1) {
        system("cls");
        printMenu();
        scanf("%c", &action);
        fflush(stdin);
        system("cls");
 
        switch (action) {
            case 'z':
                exit(0);
                break;
            case 'a':
                while (1) {
                    printf("输入要加密的文件名(含路径):");
                    scanf("%s", sourcefile);
                    printf("输入密钥:");
                    scanf("%s", secretKey);
                    printf("加密后的文件名(含路径):");
                    scanf("%s", targetFile);
 
                    printf("选择加密方式:\n");
                    printf("1. 异或加密\n");
                    printf("2. 其他加密方式\n");
                    printf("请选择:");
                    scanf("%d", &encryptionMethod);
 
                    if (encryptFile(sourcefile, secretKey, targetFile, encryptionMethod)) {
                        printf("文件[%s]加密成功,保存在[%s]。\n", sourcefile, targetFile);
                    }
 
                    if (redo() == 'a') {
                        system("cls");
                    } else {
                        break;
                    }
                }
                break;
            case 'b':
                while (1) {
                    printf("输入要解密的文件名(含路径):");
                    scanf("%s", sourcefile);
                    printf("输入密钥:");
                    scanf("%s", secretKey);
                    printf("解密后的文件名(含路径):");
                    scanf("%s", targetFile);
 
                    printf("选择解密方式:\n");
                    printf("1. 异或解密\n");
                    printf("2. 其他解密方式\n");
                    printf("请选择:");
                    scanf("%d", &encryptionMethod);
 
                    if (encryptFile(sourcefile, secretKey, targetFile, encryptionMethod)) {
                        printf("文件[%s]解密成功,保存在[%s]。\n", sourcefile, targetFile);
                    }
 
                    if (redo() == 'a') {
                        system("cls");
                    } else {
                        break;
                    }
                }
                break;
            case 'c':
                               while(1){    
struct stu
{
    char name[10];
    int num;
    int age;
    char addr[15];
}boya[2],boyb[2],*pp,*qq;
{
    FILE *fp;
    char ch;
    int i;
    pp=boya;
    qq=boyb;
    if((fp=fopen("D:\ceshi.txt","wb+"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
    }
    printf("\ninput data\n");
    for(i=0;i<2;i++,pp++)
    {
        scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr);
    }
    pp=boya;
    fwrite(pp,sizeof(struct stu),2,fp);
    //将文件指针重新指向一个流的开头
    rewind(fp);
    fread(qq,sizeof(struct stu),2,fp);
    printf("\n\nname\tnumber      age      addr\n");
    for(i=0;i<2;i++,qq++)
    {
        printf("%s\t%5d%7d     %s\n",qq->name,qq->num,qq->age,qq->addr);
    }
    fclose(fp);
}
    if(redo() == 'a'){  // 重新输入信息
                        system("cls");
                    }else{  // 回到主菜单
                        break;
                    }
                }
                break;
            default:
                printf("没有相应的菜单!按任意键回到主菜单...");
                getch();
        }
    }
 
    return EXIT_SUCCESS;
}
 
void printMenu() {
    printf("***************** 文本加密解密软件 *****************\n");
    printf("*                                                  *\n");
    printf("*      请从下面的菜单中选择你要进行的操作:        *\n");
    printf("*      a. 文件加密                                 *\n");
    printf("*      b. 文件解密                                 *\n");
    printf("*      c. 将信息存放到文件中                       *\n");
    printf("*      z. 退出系统                                 *\n");
    printf("*                                                  *\n");
    printf("****************************************************\n");
}
 
/**
 * 加密/解密文件
 *
 * @param   sourcefile    要加密/解密的文件名
 * @param   secretKey     密钥
 * @param   targetFile    加密/解密后要保存的文件名
 *
 * @return  加密成功或失败的数字表示
     0:加密失败
     1:加密成功
**/
int encryptFile(char *sourcefile, char *secretKey, char *targetFile, int encryptionMethod) {
    FILE *fpSource, *fpTarget;
    char buffer[21];
    int readCount, keyLen = strlen(secretKey), i;
 
    fpSource = fopen(sourcefile, "rb");
    if (fpSource == NULL) {
        printf("文件[%s]打开失败,请检查文件路径和名称是否输入正确!\n", sourcefile);
        return 0;
    }
 
    fpTarget = fopen(targetFile, "wb");
    if (fpTarget == NULL) {
        printf("文件[%s]创建/写入失败!请检查文件路径和名称是否输入正确!\n", fpTarget);
        return 0;
    }
 
    while ((readCount = fread(buffer, 1, keyLen, fpSource)) > 0) {
        if (encryptionMethod == 1) { // 异或加密
            for (i = 0; i < readCount; i++) {
                buffer[i] ^= secretKey[i];
            }
        } else {
         //另一种加密方式  
         for (i = 0; i < readCount; i++) {
        // 进行替换加密,将字符替换为其后继字符
        if (isalpha(buffer[i])) {
            if (buffer[i] == 'z') {
                buffer[i] = 'a';
            } else if (buffer[i] == 'Z') {
                buffer[i] = 'A';
            } else {
                buffer[i]++;
            }
        }
    }
        }
 
        fwrite(buffer, 1, readCount, fpTarget);
    }
 
    fclose(fpSource);
    fclose(fpTarget);
    return 1;
}
 
char redo() {
    char action;
    printf("\n接下来你希望:\n");
    printf("a. 继续当前操作\n");
    printf("b. 回到主菜单\n");
 
    while (1) {
        fflush(stdin);
        scanf("%c", &action);
        fflush(stdin);
 
        if (action != 'a' && action != 'b') {
            printf("没有相应的菜单,请重新选择。\n");
        } else {
            return action;
            break;
        }
    }
}

//在注释“//另一种加密方式”后写一段加密代码

引用GPT


简单的替换加密算法代码作为示例。这个算法使用了替换表来将明文字符替换为密文字符。请注意,这只是一个示例,并不是一个安全可靠的加密算法。

c
Copy Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void substitutionEncrypt(char *source, char *target, char *substituteTable);
void substitutionDecrypt(char *source, char *target, char *substituteTable);

int main() {
    char source[100];
    char target[100];
    char substituteTable[256] = "bcdefghijklmnopqrstuvwxyza";

    printf("请输入要加密的文本:");
    fgets(source, sizeof(source), stdin);
    substitutionEncrypt(source, target, substituteTable);
    printf("加密后的文本:%s\n", target);

    substitutionDecrypt(target, source, substituteTable);
    printf("解密后的文本:%s\n", source);

    return 0;
}

void substitutionEncrypt(char *source, char *target, char *substituteTable) {
    int i;
    int length = strlen(source);
    for (i = 0; i < length; i++) {
        if (isalpha(source[i])) {
            if (isupper(source[i])) {
                target[i] = toupper(substituteTable[source[i] - 'A']);
            } else {
                target[i] = tolower(substituteTable[source[i] - 'a']);
            }
        } else {
            target[i] = source[i];
        }
    }
    target[length] = '\0';
}

void substitutionDecrypt(char *source, char *target, char *substituteTable) {
    int i;
    int length = strlen(source);
    for (i = 0; i < length; i++) {
        if (isalpha(source[i])) {
            if (isupper(source[i])) {
                target[i] = toupper(strchr(substituteTable, tolower(source[i])) - substituteTable + 'A');
            } else {
                target[i] = tolower(strchr(substituteTable, source[i]) - substituteTable + 'a');
            }
        } else {
            target[i] = source[i];
        }
    }
    target[length] = '\0';
}
在这个示例中,我们定义了两个函数substitutionEncrypt和substitutionDecrypt来分别进行加密和解密操作。substituteTable是替换表,用于将明文字符替换为密文字符。

请注意,这只是一个简单的示例,不适用于真正的安全加密场景。在实际应用中,需要使用更强大和安全的加密算法来保护数据的隐私和机密性。