【问题标题】:Hill Cipher Encrypt希尔密码加密
【发布时间】:2016-01-15 03:07:46
【问题描述】:

所以我尝试使用希尔密码来用给定的密钥加密我的 3x3 矩阵。它对于输出 n 的第一个值正常工作,但是在那个值之后我得到很大的值,它永远不会接受它们的 mod。我添加了 cout 语句来帮助我调试并查看出了什么问题,但我仍然无法修复它。第二个 mod 26 也在那里,因为当我没有它时,我得到的是负 13 而不是正 13。这是一个家庭作业程序,我们的密钥作为数字提供给我们,以防万一这很重要。

#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>

using std::endl;
using std::string;


void inverse_matrix();
string encryption(string x);

int main()
{
std::string one = "paymoremoney";
// inverse_matrix();

encryption(one);

system("pause");
return 0;
}



string encryption(string x)
{
int encrypted[4][4];
int key[3][3];
key[0][0] = 4;
key[0][1] = 9;
key[0][2] = 15;
key[1][0] = 15;
key[1][1] = 17;
key[1][2] = 6;
key[2][0] = 24;
key[2][1] = 0;
key[2][2] = 17;

int test = 0;
char str[] = "";
char c;


int length = (int)x.length();
for (int i = 0; i < length; i++)
{
    x[i] = tolower(x[i]);

}
/*


while (str[test])
{
    c = str[test];
    putchar(tolower(c));
    test++;

}
*/


int encrypt[4][4];
encrypt[0][0] = x[0];
encrypt[0][1] = x[1];
encrypt[0][2] = x[2];
encrypt[1][0] = x[3];
encrypt[1][1] = x[4];
encrypt[1][2] = x[5];
encrypt[2][0] = x[6];
encrypt[2][1] = x[7];
encrypt[2][2] = x[8];
encrypt[3][0] = x[9];
encrypt[3][1] = x[10];
encrypt[3][2] = x[11];



encrypted[0][0] = (key[0][0] * encrypt[0][0]) + (key[1][0] * encrypt[0][1])              + (key[3][0] * encrypt[0][2]) % 26;
encrypted[0][0] %= 26;



encrypted[0][1] = (key[0][1] * encrypt[0][0]) + (key[1][1] * encrypt[0][1]) + (key[2][1] * encrypt[0][2])%26;
encrypted[0][0] %= 26;

encrypted[0][2] = (key[0][2] * encrypt[0][0]) + (key[1][2] * encrypt[0][1]) + (key[2][2] * encrypt[0][2]) % 26;
encrypted[0][0] %= 26;

std::cout << encrypted[0][0];
std::cout << endl;
std::cout << encrypted[0][1];
std::cout << endl;
std::cout << encrypted[0][2];


std::cout << endl;

}

【问题讨论】:

    标签: c++ encryption cryptography


    【解决方案1】:

    由于这是家庭作业,我会指出正确的方向,而不是直接给出答案。您正在分配给encrypted[0][1]。分配给它后,您如何处理该值?

    【讨论】:

    • 哇,谢谢,我想我需要一双新鲜的眼睛。一直在研究加密和逆一整天的愚蠢错误,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2012-08-02
    • 2012-05-06
    相关资源
    最近更新 更多