VC对密码加密和解密函数

// 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

// 常量
#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值