首 页 行业热点 新车 试驾评测 养车用车 车型库

C语言英文文本加密

发布网友

我来回答

2个回答

热心网友

#include "stdio.h"

#include <stdlib.h>

int main(int argc,char *argv[]){

FILE *fp,*fq;

int k,t;

fp=fopen("AAA123456701.txt","w+");

if(!fp || (fq=fopen("tmp.txt","w"))==NULL){

printf("Failed to open the file and exit...\n");

return 0;

}

printf("Please enter a short passage(letters+space+punctuation,'Enter' end)...\n");

while((t=getchar())!='\n')//为文件输入内容

fputc(t,fp);

printf("Please enter the encryption key(int >0)...\nk=");

while(scanf("%d",&k)!=1 || k<1){//输入加密密钥并判断是否正确

printf("Input error, redo: ");

fflush(stdin);

}

rewind(fp);

while(t=fgetc(fp),!feof(fp))//加密

if(t>='A' && t<='Z')

fputc(((t-'A')+k)%26+'A',fq);

else if(t>='a' && t<='z')

fputc(((t-'a')+k)%26+'a',fq);

else

fputc(t,fq);

fclose(fp);//关闭原文件

fclose(fq);//关闭加密后的文件

remove("AAA123456701.txt");//删除原文件

rename("tmp.txt","AAA123456701.txt");//将加密后的文件更换为原文件名

printf("\n");

if(fp=fopen("AAA123456701.txt","r")){

while((t=fgetc(fp))!=EOF)

printf("%c",t);

printf("\nEncryption success!\n");

}

else

printf("\nFailed to open the encrypted file...\n");

fclose(fp);

return 0;

}

代码格式和运行样例图片:

热心网友

刚好,这学期做了一个DES的加密程序。其中DES算法主体不是我写的,我只写了调用它的部分。

程序开源在Github上,你搜InformationSecurityTechnology就能搜到。

至于你的要求嘛,123点都没有直接达到,不过自己改一改不难的。主要就是第4点,实现了DES的CBC模式加密。

CBC.c:

#include "Common.c"

int CBCEncrypt(Context *con)
{
    InitScope();
    char buffer[8]; // 因为DES_EncryptBlock允许明文和密文相同,其实也可以直接使用密文数组,但仍需memcpy一次
    for (int i = 0; i < con->length; i += 8)
    {
        memcpy(buffer, plaintext + i, 8);
        DES_XOR(buffer, prep, 8);
        DES_EncryptBlock(buffer, subkeys, ciphertext + i);
        prep = ciphertext + i;
    }
    memcpy(con->pre, prep, 8);
    return 0;
}

int CBCDecrypt(Context *con)
{
    InitScope();
    for (int i = 0; i < con->length; i += 8)
    {
        DES_DecryptBlock(ciphertext + i, subkeys, plaintext + i);
        DES_XOR(plaintext + i, prep, 8);
        prep = ciphertext + i;
    }
    memcpy(con->pre, prep, 8);
    return 0;
}

int main(void)
{
    Run(CBCEncrypt, CBCDecrypt);
    return 0;
}

Common.c:

#ifndef _DES_c
#define _DES_c
#include "DES.c"
#endif
#include "MakeKeys.c"
#include <assert.h>

typedef struct
{
    size_t length;
    ElemType plainText[BUFSIZ];
    ElemType cipherText[BUFSIZ];
    ElemType subKeys[16][48];
    ElemType pre[8];
} Context;

#define InitScope()                         \
    assert(con->length % 8 == 0);           \
    ElemType *prep = con->pre;              \
    ElemType *plaintext = con->plainText;   \
    ElemType *ciphertext = con->cipherText; \
    ElemType(*subkeys)[48] = con->subKeys

// void SetPre(ElemType pre[8]);
int InitContext(Context *con, const char *pw)
{
    static_assert(BUFSIZ % 8 == 0, "BUFSIZ is not an integer multiple of eight");

    // TODO: pw长度大于7
    if (strlen(pw) > 7)
    {
        perror("pw too long!");
        return 1;
    }
    char pwbuffer[7] = {0};
    strncpy(pwbuffer, pw, 7);
    MakeSubKeys(pwbuffer, con->subKeys);
    // SetPre(con->pre);
    memcpy(con->pre, "12345678", 8);

    return 0;
}

int (*Encrypter)(Context *con);
int (*Decrypter)(Context *con);

#define InitIO(srcf, destf)                  \
    FILE *srcfile = fopen((srcf), "rb");     \
    FILE *destfile = fopen((destf), "wb");   \
    if (srcfile == NULL || destfile == NULL) \
    {                                        \
        perror("File opening failed.");      \
        return 1;                            \
    }

int DES_Encrypt(char *plainFile, char *keyStr, char *cipherFile)
{
    InitIO(plainFile, cipherFile);
    Context context;
    InitContext(&context, keyStr);

    while (0 != (context.length = fread(context.plainText, sizeof(char), BUFSIZ - 1, srcfile)))
    {
        // context.plainText[context.length++] = '\0';
        // if (context.length % 8 != 0)
        // {
        //     int rest = 8 - context.length % 8;
        //     memset(context.plainText + context.length, '\0', rest);
        //     context.length += rest;
        // }

        int rest = 8 - context.length % 8;
        memset(context.plainText + context.length, '\0', rest);
        context.length += rest;

        assert(context.length % 8 == 0);
        assert(Encrypter != NULL);

        Encrypter(&context);
        fwrite(context.cipherText, sizeof(ElemType), context.length, destfile);
    }

    fclose(srcfile);
    fclose(destfile);
    return 0;
}

int DES_Decrypt(char *cipherFile, char *keyStr, char *plainFile)
{
    InitIO(cipherFile, plainFile);
    Context context;
    InitContext(&context, keyStr);

    while (0 != (context.length = fread(context.cipherText, sizeof(char), BUFSIZ, srcfile)))
    {
        assert(context.length % 8 == 0);
        assert(Decrypter != NULL);

        Decrypter(&context);
        // fwrite(context.plainText, sizeof(ElemType), context.length, destfile);
        // fprintf(destfile, "%s", context.plainText);
        fputs(context.plainText, destfile);
    }

    fclose(srcfile);
    fclose(destfile);
    return 0;
}

int Run(int (*encrypter)(Context *con), int (*decrypter)(Context *con))
{
    Encrypter = (encrypter);
    Decrypter = (decrypter);
    char *plainfile = "plain.txt";
    char *cipherfile = "cipher.bin";
    char *decreptedfile = "restored.txt";
    char *pw = "1234567";

    DES_Encrypt(plainfile, pw, cipherfile);
    DES_Decrypt(cipherfile, pw, decreptedfile);

    return 0;
}

MakeKeys.c:

#ifndef _DES_c
#define _DES_c
#include "DES.c"
#endif

int MakeRawKey(const char pw[7], char rawkey[56])
{
    for (int i = 0; i < 7; i++)
        ByteToBit(pw[i], rawkey + i * 8);
    return 0;
}

int MakeKey(const char rawkey[56], ElemType key[])
{
    for (int i = 0; i < 8; i++)
    {
        memcpy(key + 8 * i, rawkey + 7 * i, 7);
        char check = 0;
        for (int j = 8 * i; j < 8 * i + 7; j++)
            check ^= key[j];
        key[8 * i + 7] = check; // 奇数个1时check为1,即偶校验
    }
    return 0;
}

int MakeSubKeys(const char pw[7], ElemType subkeys[16][48])
{
    char rawkey[56];
    MakeRawKey(pw, rawkey);
    ElemType key[];
    MakeKey(rawkey, key);
    DES_MakeSubKeys(key, subkeys);
    return 0;
}

#define GetProperSize(x) ((x) % 8 == 0 ? (x) : ((x) + 8 - (x) % 8))
#define Length(x) (sizeof(x) / sizeof(*x))

// 不需要使用,DES_DecryptBlock中已经反向使用子密钥了
int SwapSubKeys(ElemType subkeys[16][48])
{
    ElemType temp[48];
    for (int i = 0; i < 8; i++)
    {
        memcpy(temp, subkeys[i], Length(temp));
        memcpy(subkeys[i], subkeys[15 - i], 48);
        memcpy(subkeys[15 - i], temp, Length(temp));
    }
    return 0;
}

而DES.c不是我写的,就不发了。要看去Github看。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com