简单的字符串加密算法

简单的字符串加密算法

本文介绍的一种简单的字符串加密算法,主要有两部分组成:
1.加密钥匙字符串混淆;2.加密字符串与钥匙之间的异或加密;
先附上加密和解密的实现代码,后面有机会再详细分析一下。


加密常量(可以定义任意常量)

#define C1 1
#define C2 2

加密

CString  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.SetAt(0, 65 + j / 26);
        str.SetAt(1, 65 + j % 26);
        Result += str;
    }
    return Result;
}

解密

CString  DeCrypt(CString S, WORD key)
{
    int i, j;
    CString Result, str;
    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.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;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值