专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »C语言教程 » md5加密解密vc:VC对密码加密和解密函数 »正文

md5加密解密vc:VC对密码加密和解密函数

来源: 发布时间:星期四, 2008年9月25日 浏览:200次 评论:0
//Crypt.cpp:implementationoftheCCryptclass.
//
//////////////////////////////////////////////////////////////////////
#include\"stdafx.h\"
#include\"Crypt.h\"
#ifdef_DEBUG
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#definenewDEBUG_NEW
#endif
//常量
#defineC152845
#defineC222719
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCrypt::CCrypt()
{
}
CCrypt::~CCrypt()
{
}
CStringCCrypt::Encrypt(CStringS,WORDKey)//加密函数
{
CStringResult,str;
inti,j;
Result=S;//初始化结果字符串
for(i=0;i<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=0;i<S.GetLength();i++)//对加密结果进行转换
{
j=(BYTE)S.GetAt(i);//提取字符
//将字符转换为两个字母保存
str=\"12\";//设置str长度为2
str.SetAt(0,65+j/26);
str.SetAt(1,65+j%26);
Result+=str;
}
returnResult;
}
CStringCCrypt::Decrypt(CStringS,WORDKey)//解密函数
{
CStringResult,str;
inti,j;
Result.Empty();//清楚结果
for(i=0;i<S.GetLength()/2;i++)//将字符串两个字母一组进行处理
{
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=0;i<S.GetLength();i++)//依次对字符串中各字符进行操作
{
Result.SetAt(i,(BYTE)S.GetAt(i)^(Key>>8));//将密钥移位后与字符异或
Key=((BYTE)S.GetAt(i)+Key)*C1+C2;//产生下一个密钥
}
returnResult;
}
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: