加密与解密的简单实现

  1. 简单字符加密
#include<stdio.h>
#include<stdlib.h>

void main()
{
	char strsrc[20] = "i love you";

	printf("\n加密前:%s",strsrc);

	int i;
	for (i = 0; i < 20; i++)
	{
		strsrc[i] += i;
	}

	printf("\n加密后:%s", strsrc);
	for (i = 0; i < 20; i++)
	{
		strsrc[i] -= i;
	}

	printf("\n解密后:%s", strsrc);

	system("pause");
}

2.按照密码加密

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

void encryption(char *src, char *code)
{
	int len1 = strlen(src);
	int len2 = strlen(code);
	int i, j;
	for (i = 0; i < len1 / len2; i++)
	{
		for (j = 0; j < len2; j++)
		{
			src[i*len2 + j] += code[j];
		}
	}

	for (i = 0; i < len1%len2; i++)
	{
		src[len1/len2*len2+1] += code[i];
	}

	printf("\n%s", src);
}


void decryption(char *src, char *code)
{

	int len1 = strlen(src);
	int len2 = strlen(code);
	int i, j;
	for (i = 0; i < len1 / len2; i++)
	{
		for (j = 0; j < len2; j++)
		{
			src[i*len2 + j] -= code[j];
		}
	}

	for (i = 0; i < len1%len2; i++)
	{
		src[i] -= code[i];
	}

	printf("\n%s", src);
}

void main()
{
	char strsrc[30] = "please encrypt this sentence";
	char password[8] = { 0 };

	printf("\n加密前:%s",strsrc);
	printf("\n加密后:");
	encryption(strsrc, "1314521");
	printf("\n解密后:");
	decryption(strsrc, "1314521");

	system("pause");
}


3.加密文本文件

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


char *srcpath = "C:\\Users\\lucifer\\Desktop\\src.txt";
char *encryptedpath = "C:\\Users\\lucifer\\Desktop\\encrypted.txt";
char *decryptedpath = "C:\\Users\\lucifer\\Desktop\\decrypted.txt";

int  GetFileSize(char *path)
{
	FILE *fp = fopen(path, "r");
	if(fp)
	{
		fseek(fp, 0, SEEK_END);
		int size = ftell(fp);
		return size;

	}

	return -1;

}

void encryption(char *srcpath, char *codepath)
{
	FILE *pSrc = fopen(srcpath, "r");
	FILE *pCode = fopen(codepath, "w");
	int i;

	if (!pSrc || !pCode)
	{
		printf("\n文件打开失败");
		return;
	}

	int len1 = GetFileSize(srcpath);

	for (i = 0; i < len1; i++)
	{
		char ch = fgetc(pSrc);
		fputc(ch+4, pCode);

	}

	fclose(pCode);
	fclose(pSrc);
	system(codepath);
}

void decryption(char *srcpath, char *codepath)
{
	FILE *pSrc = fopen(srcpath, "r");
	FILE *pCode = fopen(codepath, "w");
	int i;

	if (!pSrc || !pCode)
	{
		printf("\n文件打开失败");
		return;
	}

	int len1 = GetFileSize(srcpath);
	//int len2 = GetFileSize(codepath);

	for (i = 0; i < len1; i++)
	{
		char ch = fgetc(pSrc);
		fputc(ch-4 , pCode);
	}

	fclose(pCode);
	fclose(pSrc);
	system(codepath);
}

void main()
{
	encryption(srcpath, encryptedpath);

	decryption(encryptedpath, decryptedpath);

	system("pause");

}


4.加密二进制文件

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

char *srcpath = "C:\\Users\\lucifer\\Desktop\\电脑管家.lnk";
char *encryptedpath = "C:\\Users\\lucifer\\Desktop\\1.txt";
char *decryptedpath = "C:\\Users\\lucifer\\Desktop\\2.lnk";

int GetFileSize(char *path)
{
	FILE *fp = fopen(path, "rb");

	if (fp)
	{
		fseek(fp, 0, SEEK_END);
		int len = ftell(fp);
		return len;
	}
	return -1;
}

void encryption(char *srcpath, char *codepath)
{
	FILE *pSrc = fopen(srcpath, "rb");
	FILE *pCode = fopen(codepath, "wb");
	int i;
	if (!pSrc || !pCode)
	{
		printf("文件打开失败");
		return;
	}

	int size = GetFileSize(srcpath);
	char *p = (char *)malloc(sizeof(char)*size);
	fread(p, 1, size, pSrc);

	for (i = 0; i < size; i++)
	{
		p[i] += 1;
	}
	fwrite(p, 1, size, pCode);

	fclose(pCode);
	fclose(pSrc);
	system(codepath);
}

void decryption(char *srcpath, char *codepath)
{
	FILE *pSrc = fopen(srcpath, "rb");
	FILE *pCode = fopen(codepath, "wb");
	int i;
	if (!pSrc || !pCode)
	{
		printf("文件打开失败");
		return;
	}

	int size = GetFileSize(srcpath);
	char *p = (char *)malloc(sizeof(char)*size);
	fread(p, 1, size, pSrc);

	for (i = 0; i < size; i++)
	{
		p[i] -= 1;
	}
	fwrite(p, 1, size, pCode);

	fclose(pCode);
	fclose(pSrc);
}

void main()
{
	encryption(srcpath, encryptedpath);
	decryption(encryptedpath,decryptedpath);
	system(decryptedpath);
	system("pause");
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值