加密解密 c++

#include "stdafx.h"

// 常量
#define C1 52845
#define C2 22719

CString Encrypt(CString csDeCode, WORD Key) // 加密函数
{
CString Result,str;
int i,j;

Result=csDeCode; // 初始化结果字符串
for(i=0; i<csDeCode.GetLength(); i++) // 依次对字符串中各字符进行操作
{
  Result.SetAt(i, csDeCode.GetAt(i)^(Key>>8)); // 将密钥移位后与字符异或
  Key = ((BYTE)Result.GetAt(i)+Key)*C1+C2; // 产生下一个密钥
}
csDeCode=Result; // 保存结果
Result.Empty(); // 清除结果
for(i=0; i<csDeCode.GetLength(); i++) // 对加密结果进行转换
{
  j=(BYTE)csDeCode.GetAt(i); // 提取字符
  // 将字符转换为两个字母保存
  str="12"; // 设置str长度为2
  str.SetAt(0, 65+j/26);//这里将65改大点的数例如256,密文就会变乱码,效果更好,相应的,解密处要改为相同的数
  str.SetAt(1, 65+j%26);
  Result += str;
}
return Result;
}


CString Decrypt(CString csEnCode, WORD Key) // 解密函数
{
CString Result,str;
int i,j;

Result.Empty(); // 清除结果
for(i=0; i < csEnCode.GetLength()/2; i++) // 将字符串两个字母一组进行处理
{
  j = ((BYTE)csEnCode.GetAt(2*i)-65)*26;//相应的,解密处要改为相同的数

  j += (BYTE)csEnCode.GetAt(2*i+1)-65;
  str="1"; // 设置str长度为1
  str.SetAt(0, j);
  Result+=str; // 追加字符,还原字符串
}
csEnCode = Result;       // 保存中间结果
for(i=0; i<csEnCode.GetLength(); i++) // 依次对字符串中各字符进行操作
{
  Result.SetAt(i, (BYTE)csEnCode.GetAt(i)^(Key>>8)); // 将密钥移位后与字符异或
  Key = ((BYTE)csEnCode.GetAt(i)+Key)*C1+C2; // 产生下一个密钥
}
return Result;
}

用法:
CString text=_T("192.168.18.14");//需要加密的字符串
WORD key=1314;//key
CString jiami=Encrypt(text,key);//加密
AfxMessageBox(_T("密文:")+jiami);
CString jiemi=Decrypt(jiami,key);//解密
AfxMessageBox(_T("原文:")+jiemi);
 

转自:http://blog.csdn.net/yaoohfox/archive/2009/07/17/4357325.aspx

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:仿射密码是一种替换加密方式,它通过将明文中的每个字符分别映射为一个新字符来进行加密,其加密公式如下: C = (a * P + b) mod m 其中,P为明文字符的ASCII码值,C为密文字符的ASCII码值,a和b为密钥参数,m为模数。解密则需要用到该加密公式的逆运算,即: P = a^-1 * (C - b) mod m 其中,a^-1为a在模m下的乘法逆元。 以下是仿射密码加密解密的C语言实现: ```c #include <stdio.h> // gcd(a, b):求a与b的最大公约数 int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // modInverse(a, m):求a在模m下的乘法逆元 int modInverse(int a, int m) { a = a % m; for (int x = 1; x < m; x++) if ((a * x) % m == 1) return x; return -1; } // encrypt:仿射密码加密 void encrypt(char* plain, char* cipher, int a, int b, int m) { int len = strlen(plain); for (int i = 0; i < len; i++) { int P = (int)plain[i]; int C = (a * P + b) % m; cipher[i] = (char)C; } cipher[len] = '\0'; } // decrypt:仿射密码解密 void decrypt(char* cipher, char* plain, int a, int b, int m) { int len = strlen(cipher); int aInverse = modInverse(a, m); for (int i = 0; i < len; i++) { int C = (int)cipher[i]; int P = aInverse * (C - b + m) % m; plain[i] = (char)P; } plain[len] = '\0'; } int main() { char plain[] = "Hello, World!"; // 明文 char cipher[strlen(plain)]; // 密文 char decrypt_plain[strlen(plain)]; // 解密后的明文 int a = 7, b = 3, m = 26; // 密钥参数 int aInverse = modInverse(a, m); // 密钥参数a的乘法逆元 if (gcd(a, m) != 1) { // 判断密钥参数a是否合法 printf("Invalid key parameter a!\n"); return 0; } encrypt(plain, cipher, a, b, m); // 加密 printf("Plain text: %s\n", plain); printf("Cipher text: %s\n", cipher); decrypt(cipher, decrypt_plain, a, b, m); // 解密 printf("Decrypted plain text: %s\n", decrypt_plain); return 0; } ``` 上述代码中,我们首先定义了gcd和modInverse两个函数,分别用于求两个整数的最大公约数和求一个整数在模m下的乘法逆元。 接着,我们定义了encrypt和decrypt两个函数,分别用于进行加密解密操作。 在main函数中,我们定义了明文字符串、密文字符串、密钥参数a、b和模数m,其中密钥参数a需满足与模数m互质的条件。然后,我们调用encrypt函数对明文进行加密,再调用decrypt函数对密文进行解密,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值