📄 crypt.cpp
字号:
// Crypt.cpp: implementation of the CCrypt class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Crypt.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// 常量
BYTE KEY[]={0x01,0x2E,0x6e,0x6d};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCrypt::CCrypt()
{
}
CCrypt::~CCrypt()
{
}
CString CCrypt::Encrypt(CString S) // 加密函数
{
CString Result,str;
int i,j;
int ikey=0;
BYTE bM;
Result=S; // 初始化结果字符串
for(i=0; i<S.GetLength(); i++) // 依次对字符串中各字符进行操作
{
bM=KEY[ikey]+S.GetAt(i);
if(bM>255)bM-=256;
Result.SetAt(i,bM);
ikey++;
if(ikey>=4)ikey=0;
}
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) // 解密函数
{
CString Result,str;
int i,j;
int ikey=0;
BYTE bM;
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++) // 依次对字符串中各字符进行操作
{
bM=S.GetAt(i);
if(bM>=KEY[ikey])
bM=bM-KEY[ikey];
else
bM=256+bM-KEY[ikey];
Result.SetAt(i,bM); // 将密钥移位后与字符异或
ikey++;
if(ikey>=4)ikey=0;
}
return Result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -