维尔南密码
Vernam cipher code
本文关键字:密码 更新时间:2023-10-16
我正试图编写代码来实现c++中的vernam密码,但我的代码不运行。我不知道问题出在哪里。代码也会获得0,1和密钥中的消息,然后实现它们的异或以创建密文和相同的解密方法,当我运行它时,它会给我一个警告并停止运行。
#include<iostream>
#include<string>
using namespace std;
void encrypt(string msg, string key)
{
while (msg.length() > key.length())
{
key += key;
}
string encrypt_Text = "";
for (size_t i = 0; i <= msg.length(); i++)
{
encrypt_Text += msg[i] ^ key[i];
}
cout << "the cipher text is:" << encrypt_Text << endl;
}
void decrypt(string cipher, string key)
{
while (cipher.length() > key.length())
{
key += key;
}
string decrypt_Text = "";
for (int i = 0; i <= cipher.length(); i++)
{
decrypt_Text += cipher[i] ^ key[i];
}
cout << "the messege is:" << decrypt_Text << endl;
}
void main()
{
string msg, key;
cout << "enter your messege in boolean : " << endl;
cin >> msg;
cout << "enter your key in boolean : " << endl;
cin >> key;
encrypt(msg, key);
}
在encrypt()
函数中,尝试如下:
encrypt_Text+=((msg[i]-'0')^(key[i]-'0')+'0');
否则使用字符的ASCII码。此外,修改循环,使其使用<
,而不是<=
。
for(size_t i=0; i<msg.length(); i++)
// ^ smaller, not smaller or equal
并将main()
的返回类型修改为int
,这就是c++。
相关文章:
- c++中的oop(密码生成)
- 密码登录程序将永远循环并显示不正确的结果
- OpenSSL没有共享密码
- Cryptopp:获取密码输入的填充字符串
- 密码长度验证 (c++)
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 仿射密码解密,输出大小写不同
- 替换密码:哪一个?
- 无法使用 openssl 服务器,因为密码已过时...什么的...ERR_SSL_VERSION_OR_CIPHER_MISMATCH
- 如何使用 c++ 在 sqlite3 中打开受密码保护的数据库?
- 根据用户名和密码 Qt C++重新访问数据库值
- C++密码问题
- 无法在 Cocos2dx 中使用受密码保护的 zip 文件
- 如何使用librdkafka设置明文协议(无SASL)的用户名和密码?
- 如何将凯撒密码的元素从字符串更改为字符串
- 如何在C++的凯撒密码程序中包含空格?
- 运行密钥密码解密知道密钥?
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- 当PSO细粒度策略对使用AdsGetObject MSDN API的Windows操作系统生效时,如何获取用户密码到期日
- 密码显示双字母 c++ 的问题