仿射加密与解密 Z26(含代码)

仿射加密与解密

代码

int dx, y, q;
void extend_Eulid(int aa, int bb)
{
	if (bb == 0) {
		dx = 1; y = 0; q = aa;
	}
	else {
		extend_Eulid(bb, aa % bb);
		int temp = dx;
		dx = y;
		y = temp - aa / bb * y;
	}
}

int main()
{
	int a, b, YN, i, l;
	char c[100];
	int x[100], y[100];
	char ex[100];

	cout << "请依次输入k =( a, b )的a, b值,其中 a,b ∈ Z/(26),gcd( a,26) = 1 :" << endl;
	cin >> a >> b;
	cout << "那么你的加密函数就是 ex = " << a << "*x + " << b << endl;
	cout << endl << "接下来输入你要加密的明文(小写字母):" << endl;
	cin >> c;//明文
	l = strlen(c);

	for (i = 0; i < l; i++)
	{
		x[i] = c[i] - 'a';
		ex[i] = (a * x[i] + b) % 26;//数字
	}
	cout << "加密后的字母为:";

	for (i = 0; i < l; i++)
	{
		cout << char(ex[i] + 'a');//转字符
	}
	cout << endl << endl;
	cout << "是否要解密原文(输入1则确定,输入其他则取消):";
	cin >> YN;

	while (YN == 1)
	{
		extend_Eulid(a, 26);//取逆
		dx = (dx + 26) % 26;
		for (i = 0; i < l; i++)
		{
			y[i] = (dx * int(ex[i]) - dx * b) % 26;
			y[i] = (y[i] + 26) % 26;//+26取正
			cout << char(y[i] + 'a');
		}
		break;
	}
	system("pause");
}

结果:

代码实现结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以通过仿射变换对数据进行加密解密仿射变换是一种线性变换,可以用于对字符或数据进行简单的置换和替换操作。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 加密函数 char* encrypt(char* message, int a, int b) { int len = strlen(message); char* encrypted = (char*)malloc(len * sizeof(char)); for (int i = 0; i < len; i++) { if (message[i] >= 'a' && message[i] <= 'z') { encrypted[i] = (char)((((a * (message[i] - 'a') + b) % 26) + 26) % 26 + 'a'); } else if (message[i] >= 'A' && message[i] <= 'Z') { encrypted[i] = (char)((((a * (message[i] - 'A') + b) % 26) + 26) % 26 + 'A'); } else { encrypted[i] = message[i]; } } return encrypted; } // 解密函数 char* decrypt(char* encrypted, int a, int b) { int len = strlen(encrypted); char* decrypted = (char*)malloc(len * sizeof(char)); int aInverse = 0; // 计算a的模逆 for (int i = 0; i < 26; i++) { if ((a * i) % 26 == 1) { aInverse = i; break; } } for (int i = 0; i < len; i++) { if (encrypted[i] >= 'a' && encrypted[i] <= 'z') { decrypted[i] = (char)((((aInverse * ((encrypted[i] - 'a') - b + 26)) % 26) + 26) % 26 + 'a'); } else if (encrypted[i] >= 'A' && encrypted[i] <= 'Z') { decrypted[i] = (char)((((aInverse * ((encrypted[i] - 'A') - b + 26)) % 26) + 26) % 26 + 'A'); } else { decrypted[i] = encrypted[i]; } } return decrypted; } int main() { char message[100]; int a, b; printf("请输入要加密的消息:"); scanf("%[^\n]", message); getchar(); // 清除输入缓冲区的换行符 printf("请输入加密参数a和b(以空格分隔):"); scanf("%d %d", &a, &b); char* encrypted = encrypt(message, a, b); printf("加密结果:%s\n", encrypted); char* decrypted = decrypt(encrypted, a, b); printf("解密结果:%s\n", decrypted); free(encrypted); free(decrypted); return 0; } ``` 以上代码实现了仿射变换的加密解密功能。用户可以输入要加密的消息和加密参数a、b,程序将对消息进行加密并输出加密结果,然后再对加密结果进行解密并输出解密结果。加密过程中,对于字母字符,将其根据仿射变换的置换和替换公式进行操作;对于非字母字符,直接保留不变。解密过程中,先通过计算a的模逆找到解密用的参数,再对加密结果进行反操作,恢复原始消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值