最近在做课程设计,在附带光盘资料里有源程序代码,运行的时候提示输入用户名和密码。我找到了加密函数,但是看不懂,请大家给我解释一下,谢谢!// Crypt.cpp: implementation of the CCrypt class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "Attendance.h"
#include "Crypt.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif// 常量
#define C1 52845
#define C2 22719//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CCrypt::CCrypt()
{}CCrypt::~CCrypt()
{}CString CCrypt::Encrypt(CString S, WORD Key) // 加密函数
{
CString Result,str;
int i,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;
}
return Result;
}CString CCrypt::Decrypt(CString S, WORD Key) // 解密函数
{
CString Result,str;
int i,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; // 产生下一个密钥
}
return Result;
}
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "Attendance.h"
#include "Crypt.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif// 常量
#define C1 52845
#define C2 22719//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CCrypt::CCrypt()
{}CCrypt::~CCrypt()
{}CString CCrypt::Encrypt(CString S, WORD Key) // 加密函数
{
CString Result,str;
int i,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;
}
return Result;
}CString CCrypt::Decrypt(CString S, WORD Key) // 解密函数
{
CString Result,str;
int i,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; // 产生下一个密钥
}
return Result;
}
解决方案 »
- 这种调色取色器怎么弄。
- 关于父窗口DestroyWindow问题
- 给了一个c写的驱动程序的.h 和 .cpp文件,在我的VC工程里该怎么调用其中某些函数?
- 关于按钮同时按下的消息传递,高手请进~~~~~
- 谁有生成 zip 或解开zip 文件的源码?
- up有分!
- VB的问题:请大虾们指点方向!!!!谢谢!!!!
- 菜单的打开文件项,在打开文件的时候,会触发WM_PAINT消息吗
- 在远程通讯过程中的容错算法
- windows中怎么将注册表数据从LPBYTE类型中提取为REG_BINARY,REG_DWORD,REG_QWORD
- 请教大家了,谁有windows自带的画图程序的源代码呀?急用呀
- 如何用OCI把ORACLE的多条记录放入一个字符数组中?
WORD Key 是你的密钥,你可以随便设定,比如“98”(注意类型,是Unsigned Short),密钥就像一把钥匙,是解开这个密码(相当于锁)的关键,你在这里设置的密钥不同生成的密码文本也就不同
for(i=0; i<S.GetLength(); i++)
{
Result.SetAt(i, S.GetAt(i)^(Key>>8));
Key = ((BYTE)Result.GetAt(i)+Key)*C1+C2;
} 假设我们输入的用户名是"Mark","Mark"一共有4个字母,这个函数就执行4次,
第一次是对"M"这个字母进行操作,假设把它变成了"A";(实际变成什么,你把代码Copy到VC中一RUN就知道了)
第二次是对"a"这个字母进行操作,假设把它变成了"k";
第三次是对"r"这个字母进行操作,假设把它变成了"M";
第四次是对"k"这个字母进行操作,假设把它变成了"r"; return Result;就是返回变形后的内容,你输入的"Mark"已经变成了"AkMr"2 CString CCrypt::Decrypt(CString S, WORD Key) // 解密函数 CString S这里传入的就应该是密文,就是加密后的内容,即"AkMr",密钥 一定要是 “98”,因为加密的时候是根据“98”这个密钥生成的密文。 后面的解密工作就是加密工作的逆向工作了。PS: 我抛开了具体代码,说明了一下“按位加密”是如何做到的(也就是你这个例子中的加密方法,它略微复杂)
实际这个代码它把一个字母,比如“M”,变成了两个字母,比如“AM”,但是这个都没有关系,抓住本质就可以了。