- 简单字符加密
#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");
}