⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypt2.cpp

📁 两个加密小程序
💻 CPP
字号:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"

/*创建源文件以及临时文件,并记录相关信息于各文件中*/
void CreateSourceFile(void)
{
    FILE *fp, *fp1;
    char ch, filename[100];
    printf("Pleace Enter File Name(Ex:SourceFileName.txt)\n");
    scanf("%s", filename);
    if((fp = fopen(filename, "w")) == NULL) {
        printf("cannot open file\n");
        getch();
        exit(0);
    }else {
        printf("%s Create Successful!\n\n", filename);
    }
    printf("Pleace Enter Text:\n");
    ch = getchar();
    ch = getchar();
    while(ch != '#') {
        fputc(ch, fp);
        ch=getchar();
    }
    fclose(fp);
    printf("%s Save Successful!\n", filename);

    if((fp1 = fopen("Temp.JT","w")) == NULL) {
        printf("cannot open file\n");
        getch();
        exit(0);
    }else {
        printf("Temp.JT Create Successful!\n");
    }
    fputs(filename, fp1);
    fclose(fp1);
    printf("Temp.JT Save Successful!\n");
    printf("Create source file successful!\n\n");
}

/*对文本进行加密,创建加密信息文件以及密文,并记录相关文件信息于各文件中*/
void Encrypt(void)
{
    FILE *in, *outPwd, *outFile;
    char ch, ch1, SourceFile[100], PWDfile[10]="PWD.JT", OutFile[100]="Outfile.txt";
    printf("Enter the source file name:\n");
    scanf("%s", SourceFile);

    if((in=fopen(SourceFile, "r"))==NULL)
    {
        printf("cannot open infile.\n");
        getch();
        exit(0);
    }else {
        printf("Source file open successful!\n");
    }
    if((outFile=fopen(OutFile, "w"))==NULL)
    {
        printf("cannot open outfile.\n");
        getch();
        exit(0);
    }else {
        printf("PWD.JT Create successful!\n");
    }
    if((outPwd=fopen(PWDfile, "w"))==NULL)
    {
        printf("cannot open pwdfile.\n");
        getch();
        exit(0);
    }else {
        printf("Outfile.txt Create successful!\n");
    }

    while(!feof(in)) {
        ch = fgetc(in);
        ch1 = rand();
        fputc(ch1,outPwd);
        ch = ch ^ ch1;
        fputc(ch,outFile);
    }

    fclose(in);
    printf("Source file close successful!\n");
    fclose(outPwd);
    printf("PWD.JT file save successful!\n");
    fclose(outFile);
    printf("Outfile.txt file save successful!\n");
}

/*对密文进行解密,并创建解密后的新文件*/
void Revivification(void)
{
    FILE *inOut, *inPwd, *outNewfile;
    char ch, outfile[100]="Outfile.txt", Pwdfile[100]="PWD.JT", Newfile[100]="Newfile.txt";

    if((inOut=fopen(outfile, "r"))==NULL)
    {
        printf("cannot open cryptograph.\n");
        getch();
        exit(0);
    }else {
        printf("Cryptograph open successful!\n");
    }
    if((inPwd=fopen(Pwdfile, "r"))==NULL)
    {
        printf("cannot open password file.\n");
        getch();
        exit(0);
    }else {
        printf("Password file open successful!\n");
    }
    if((outNewfile=fopen(Newfile, "w"))==NULL)
    {
        printf("cannot open Newfile.txt file.\n");
        getch();
        exit(0);
    }else {
        printf("New file open successful!\n");
    }

    while(!feof(inOut)&&!feof(inPwd))fputc(fgetc(inOut)^fgetc(inPwd),outNewfile);

    fclose(inOut);
    printf("Cryptograph close successful!\n");
    fclose(inPwd);
    printf("Password file close successful!\n");
    fclose(outNewfile);
    printf("New file save successful!\n");
}

/*显示源文件*/
void ShowSourceFile(void)
{
    /*略*/
}

/*显示临时文件*/
void ShowTempFile(void)
{
    /*略*/
}

/*显示加密信息文件*/
void ShowPWDFile(void)
{
    /*略*/
}

/*显示密文*/
void ShowCryptograph(void)
{
    /*略*/
}

/*显示解密后的新文件*/
void ShowNewFile(void)
{
    /*略*/
}

int main(void)
{
    char tmp;

    printf("|-----------------------------------------------------------|\n");
    printf("|Choose:----------------------------------------------------|\n");
    printf("|Enter 'a': Create source file------------------------------|\n");
    printf("|Enter 'b': Encrypt file------------------------------------|\n");
    printf("|Enter 'c': Revivification file-----------------------------|\n");
    printf("|Enter 'd': Show source file--------------------------------|\n");
    printf("|Enter 'e': Show temp file----------------------------------|\n");
    printf("|Enter 'f': Show PWD file-----------------------------------|\n");
    printf("|Enter 'g': Show cryptograph file---------------------------|\n");
    printf("|Enter 'h': Show new file-----------------------------------|\n");
    printf("|-----------------------------------------------------------|\n\n");

    scanf("%c", &tmp);
    switch(tmp) {
        case 'a':
            printf("Create source file\n");
            CreateSourceFile();
            break;
        case 'b':
            printf("Encrypt file\n");
            Encrypt();
            break;
        case 'c':
            printf("Revivification file\n");
            Revivification();
            break;
        case 'd':
            printf("Show source file\n");
            ShowSourceFile();
            break;
        case 'e':
            printf("Show temp file\n");
            ShowTempFile();
            break;
        case 'f':
            printf("Show PWD file\n");
            ShowPWDFile();
            break;
        case 'g':
            printf("Show cryptograph file\n");
            ShowCryptograph();
            break;
        case 'h':
            printf("Show new file\n");
            ShowNewFile();
            break;
        default:
            printf("This End!\n");
    }

    getch();
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -