VC 如何加密解密 ini 文本文档

VC 如何加密解密 ini 文本文档,第1张

C++加密解密函数及用法示例

// 常量

#define C1 52845

#define C2 22719

CString Encrypt(CString S, WORD Key) // 加密函数

{

CString Result,str

int i,j

Result=S// 初始化结果字符

for(i=0i<S.GetLength()i++) // 依次对字符串中各字符进行 *** 作

{

Result.SetAt(i, S.GetAt(i)^(Key>>8))// 将密钥移位后与字符异或

Key = ((BYTE)Result.GetAt(i)+Key)*C1+C2// 产生下一个密钥

}

S=Result// 保存结果

Result.Empty()// 清除结果

for(i=0i<S.GetLength()i++) // 对加密结果进行转换

{

j=(BYTE)S.GetAt(i)// 提取字符

// 将字符转换为两个字母保存

str="12"// 设置str长度为2

str.SetAt(0, 65+j/26)//这里将65改大点的数例如256,密文就会变乱码,效果更好,相应的,解密处要改为相同的数

str.SetAt(1, 65+j%26)

Result += str

}

return Result

}

CString Decrypt(CString S, WORD Key) // 解密函数

{

CString Result,str

int i,j

Result.Empty()// 清除结果

for(i=0i <S.GetLength()/2i++) // 将字符串两个字母一组进行处理

{

j = ((BYTE)S.GetAt(2*i)-65)*26)//相应的,解密处要改为相同的数

j += (BYTE)S.GetAt(2*i+1)-65

str="1"// 设置str长度为1

str.SetAt(0, j)

Result+=str// 追加字符,还原字符串

}

S=Result// 保存中间结果

for(i=0i<S.GetLength()i++) // 依次对字符串中各字符进行 *** 作

{

Result.SetAt(i, (BYTE)S.GetAt(i)^(Key>>8))// 将密钥移位后与字符异或

Key = ((BYTE)S.GetAt(i)+Key)*C1+C2// 产生下一个密钥

}

return Result

}

用法

CString text=_T("192.168.18.14")//需要加密的字符串

WORD key=1314//key

CString jiami=Encrypt(text,key)//加密

AfxMessageBox(_T("密文:")+jiami)

CString jiemi=Decrypt(jiami,key)//解密

AfxMessageBox(_T("原文:")+jiemi)

c语言文件加密和解密方法如下:

1、首先打开VC++6.0;

2、选择文件,新建;

3、选择C++ source file 新建一个空白文档;

4、声明头文件

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

首先写个加密函数,算法就是简介里说的

void EncryptFile(FILE *sfp,FILE *dfp,char pwd)

{

char ch

if(sfp==0||dfp==0)

{

printf("ERROR!\n")

return

}

while((ch=fgetc(sfp))!=EOF)

{

if((ch>='a')&&(ch<='z'))

{

ch=(ch-'a'+1)%26+'a'

ch=ch^pwd

}

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

{

ch=(ch-'A'+1)%26+'A'

ch=ch^pwd

}

fputc(ch,dfp)

}

}

写解密子函数:与加密的过程相反

void DecryptFile(FILE *sfp,FILE *dfp,char pwd)

{

char ch

while((ch=fgetc(sfp))!=EOF)

{

if((ch>='a')&&(ch<='z'))

{

ch=ch^pwd

ch=(ch-'a'+25)%26+'a'

}

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

{

ch=ch^pwd

ch=(ch-'A'+25)%26+'A'

}

fputc(ch,dfp)

}

}

输出函数,输出文件内容

void OutputFile(FILE *fp)

{

char ch

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

putchar(ch)

}

 主函数,主要调用这几个函数

int main()

{

/*用户输入的要加密的文件名*/

char sfilename[20]

/*用户输入加密后保存的文件名*/

char dfilename[20]

/*用来保存密码字符*/

char pwd

FILE *sfp,*dfp

printf("\nPlease input filename to be encrypted:\n")

/*得到要加密的文件名*/

gets(sfilename)

/*得到加密后你要的文件名*/

printf("input filename to save the encrypted file:\n")

gets(dfilename)

/*得到加密字符*/

printf("Please input your Password:\n")

//scanf("%c",&pwd)

pwd=getch()

/*屏幕以*来表示输入的加密字符*/

printf("*\n")

/*以只读方式打开要加密的文件*/

if((sfp=fopen(sfilename,"r"))==0)

{

printf("Can't open the file :%s\n",sfilename)

exit(0)

}

/*输出要加密的文件*/

printf("\nThe the text of file to be encrypted is:\n")

OutputFile(sfp)

/*建立加密后的文件*/

if((dfp=fopen(dfilename,"w+"))==0)

{

printf("Can't open or create the file :%s\n",dfilename)

//exit(0)

}

/*文件加密*/

fseek(sfp,0L,SEEK_SET)

EncryptFile(sfp,dfp,pwd)

printf("\n\nEncrypted the file successfully!\n")

/*输出加密后的文件*/

printf("\nAfter encrypting the text of file is:\n")

fseek(dfp,0L,SEEK_SET)

OutputFile(dfp)

fclose(sfp)

fclose(dfp)

getch()

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11517863.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存