C语言写的一个简单文件加密程序

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

int decrypt(FILE *in,FILE *out);
int encrypt(FILE *in,FILE *out);
unsigned char atoh(char *hexstr);

int main(int argc,char **argv)
{
	if(argc > 1 && strcmp(argv[1],"--help") == 0)
	{
		printf("\nusage: linkrules_god [-e|-d] inputfile outputfile\n");
		printf("    -e encrypt inputfile to outputfile.\n");
		printf("    -d decrypt inputfile to outputfile.\n\n");
		exit(0);
	}
	if(argc != 4)
	{
		fprintf(stderr,"%s\n","please using --help go get help.");
		exit(-1);
	}

    
	FILE *in = fopen(argv[2],"rb");
	FILE *out = fopen(argv[3],"w");
    if(in == NULL || out == NULL)
	{
		fprintf(stderr,"%s\n","open file error!");
        exit(1);
	}

    if(argv[1][1] == 'e')
    {
        encrypt(in,out);
    }
    else if(argv[1][1] == 'd')
    {
        decrypt(in,out);
    }

    fclose(in);
    fclose(out);
	return 0;
}

int encrypt(FILE *in,FILE *out)
{
    if(in == NULL || out == NULL)
    {
        fprintf(stderr,"%s\n","file error!\n");
        return -1;
    }

    unsigned char hex;
    while(fread(&hex,1,1,in))
    {
        hex = ~hex^0x92;
        fprintf(out,"%02X",hex);
    }
    return 0;
}



int decrypt(FILE *in,FILE *out)
{
    if(in == NULL || out == NULL)
    {
        fprintf(stderr,"%s\n","file error!");
        return -1;
    }
    
    unsigned char hexstr[3];
    unsigned char hex = 0;
    int i = 0;
    while(fread(&hexstr,2,1,in))
    {
        hex = atoh(hexstr);
        hex = ~(hex ^ 0x92);
        fwrite(&hex,1,1,out);
    }

    return 0;
}


/* convert string to hex */
unsigned char atoh(char *hexstr)
{
    int hextodec[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    char chtodec[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    unsigned char hexnum = 0;
    for(int i = 0; i < sizeof(chtodec); ++i)
    {
        if(hexstr[0] == chtodec[i])
        {
            hexnum += hextodec[i]*16;
        }
    }

    for(int i = 0; i < sizeof(chtodec); ++i)
    {
        if(hexstr[1] == chtodec[i])
        {
            hexnum += hextodec[i];
        }
    }

    return hexnum;
}



encrypt -e sourcefile outputfile   加密

encrypt -d sourcefile outputfile   解密

encrypt --help    帮助


输出的加密文件中的内容:





  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言加密程序的实现方式有很多种,下面我介绍一种简单的方法: 1. 将需要加密的明文按照一定规则转化为一串二进制数字(也可以是16进制或者64进制等等)。 2. 使用一个密钥(也是一串二进制数字)对明文进行加密加密的方式可以是按位异或、移位、代换等等。 3. 将加密后的密文输出或者存储到文件中。 4. 如果需要解密,使用相同的密钥对密文进行解密,得到原始的明文。 下面是一个示例代码,使用按位异或进行加密和解密: ```c #include <stdio.h> void encrypt(char *input, char *output, char *key) { // 加密函数 int i = 0; while (input[i] != '\0') { // 对输入字符串的每个字符进行处理 output[i] = input[i] ^ key[i % 4]; // 按位异或 i++; } output[i] = '\0'; // 输出字符串以'\0'结尾 } void decrypt(char *input, char *output, char *key) { // 解密函数 int i = 0; while (input[i] != '\0') { // 对输入字符串的每个字符进行处理 output[i] = input[i] ^ key[i % 4]; // 按位异或 i++; } output[i] = '\0'; // 输出字符串以'\0'结尾 } int main() { char input[100], output[100], key[5]; printf("请输入需要加密的字符串:"); scanf("%s", input); printf("请输入密钥(4个字符):"); scanf("%s", key); encrypt(input, output, key); printf("加密后的字符串为:%s\n", output); decrypt(output, input, key); printf("解密后的字符串为:%s\n", input); return 0; } ``` 在这个示例代码中,我们使用了一个简单的按位异或方式进行加密和解密。密钥的长度为4个字符,如果需要更高的安全性,可以使用更长的密钥,或者使用其他加密方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值