Vigenere密码C++

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str,str1;
	char word = 'a',word1='A';
	for (int i = 0;i <=25 ;i++)
	{
		str1 += word1;
		str += word;
		word++;
		word1++;
	}
	str += str;//获得a到z+a到z
	str1 += str1;//获得A到Z+A到Z
	cout << str << " " << str1 << endl;
	string k, C,ck;//k是密钥,C是密文,ck用来存k扩大几倍后的东东
	cin >> k >> C;
	int lk = k.size();
	int lc = C.size();
	for (int i = 0;i < lk;i++)
	{
		k[i] = tolower(k[i]);//让k全变小写
	}
	for (int i=0;i<=lc/lk;i++)
	{
		ck += k;
	}
	for (int i = 0;i < lc;i++)//将C一个一个转成明文
	{
		for (int j = 26;j <= 41;j++)
		{
			if (C[i] == str[j])
			{
				C[i] = str[j - ck[i] + 97];
			}
			if (C[i] == str1[j])
			{
				C[i] = str1[j - ck[i] + 97];
			}
		}
	}
	for (int i = 0;i < lc;i++)
	{
		cout << C[i];
	}
	return 0;
}

更新一下,debug成功了。

我发现我26+25算成了41。。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str, str1;
	char word = 'a', word1 = 'A';
	for (int i = 0;i <= 25;i++)
	{
		str1 += word1;
		str += word;
		word++;
		word1++;
	}
	str += str;//获得a到z+a到z
	str1 += str1;//获得A到Z+A到Z
	string k, C, ck;//k是密钥,C是密文,ck用来存k扩大几倍后的东东
	cin >> k >> C;
	int lk = k.size();
	int lc = C.size();
	for (int i = 0;i < lk;i++)
	{
		k[i] = tolower(k[i]);//让k全变小写
	}
	for (int i = 0;i <= lc / lk;i++)
	{
		ck += k;
	}
	for (int i = 0;i < lc;i++)//将C一个一个转成明文
	{
		for (int j = 26;j <= 51;j++)
		{
			if (C[i] == str[j])
			{
				C[i] = str[j - ck[i] + 97];
				break;
			}
			if (C[i] == str1[j])
			{
				C[i] = str1[j - ck[i] + 97];
				break;
			}
		}
	}
	for (int i = 0;i < lc;i++)
	{
		cout << C[i];
	}
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vigenere密码是一种多表密码,它使用多个不同的凯撒密码表。具体来说,对于明文中的每个字母,使用一个不同的凯撒密码表来将其加密。这样,相同的明文在不同位置上会被加密成不同的密文,增强了密码的安全性。 以下是使用C++实现Vigenere密码的示例代码: ```cpp #include <iostream> #include <string> using namespace std; string encrypt(string plaintext, string key) { string ciphertext = ""; int keyLen = key.length(); int i = 0; for (char& c : plaintext) { if (isalpha(c)) { int shift = key[i % keyLen] - 'a'; int base = isupper(c) ? 'A' : 'a'; c = (c - base + shift) % 26 + base; i++; } ciphertext += c; } return ciphertext; } string decrypt(string ciphertext, string key) { string plaintext = ""; int keyLen = key.length(); int i = 0; for (char& c : ciphertext) { if (isalpha(c)) { int shift = key[i % keyLen] - 'a'; int base = isupper(c) ? 'A' : 'a'; c = (c - base - shift + 26) % 26 + base; i++; } plaintext += c; } return plaintext; } int main() { string plaintext = "The quick brown fox jumps over the lazy dog"; string key = "lemon"; string ciphertext = encrypt(plaintext, key); string decrypted = decrypt(ciphertext, key); cout << "Plaintext: " << plaintext << endl; cout << "Key: " << key << endl; cout << "Ciphertext: " << ciphertext << endl; cout << "Decrypted: " << decrypted << endl; return 0; } ``` 在这个示例中,我们定义了两个函数:encrypt和decrypt。encrypt函数将明文和密钥作为输入,并返回加密后的密文。decrypt函数将密文和密钥作为输入,并返回解密后的明文。 在encrypt函数中,我们迭代明文中的每个字符。如果字符是字母,则我们计算出密钥中对应位置的偏移量,并使用凯撒密码的加密公式来加密该字符。注意,我们要将字符转换为大写或小写形式,以便在计算时使用ASCII编码中的正确偏移量。最后,我们将加密后的字符添加到密文中,然后返回整个密文字符串。 在decrypt函数中,我们执行与encrypt函数相反的操作。对于每个密文中的字符,我们计算出密钥中对应位置的偏移量,并使用凯撒密码的解密公式来解密该字符。最后,我们将解密后的字符添加到明文中,然后返回整个明文字符串。 在main函数中,我们定义了一个样例明文和密钥,并使用encrypt函数加密该明文。然后,我们使用decrypt函数解密该密文,并将结果打印到控制台上。 以上是一种简单的实现方法,Vigenere密码还有其他更高级的实现方式,例如使用矩阵代替凯撒密码表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值