我有一个任务是编写一个与Hill密码一起工作的程序。并不难让它加密我的信息,但我无法让它解开它。 如果我使用此处的输入数据:http://en.wikipedia.org/wiki/Hill_cipher 它给出了相同的结果。
但是如果我更改了一个密钥 - 它将无法加密该消息。
我认为问题是由错误的矩阵反转造成的,但我不确定。它有点奇怪为什么它可以用一个键正常工作,而不能与另一个键一起工作。
string alphabet_eng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private void button2_Click(object sender, EventArgs e)
{
float m11 = 0, m12 = 0, m13 = 0, m21 = 0, m22 = 0, m23 = 0, m31 = 0, m32 = 0, m33 = 0;
float deter = 0;
m11 = alphabet_eng.IndexOf(matrix_11.Text);
m12 = alphabet_eng.IndexOf(matrix_12.Text);
m13 = alphabet_eng.IndexOf(matrix_13.Text);
m21 = alphabet_eng.IndexOf(matrix_21.Text);
m22 = alphabet_eng.IndexOf(matrix_22.Text);
m23 = alphabet_eng.IndexOf(matrix_23.Text);
m31 = alphabet_eng.IndexOf(matrix_31.Text);
m32 = alphabet_eng.IndexOf(matrix_32.Text);
m33 = alphabet_eng.IndexOf(matrix_33.Text);
deter = (m11 * m22 * m33 + m12 * m23 * m31 + m13 * m21 * m32) - (m13 * m22 * m31 + m12 * m21 * m33 + m11 * m23 * m32);
deter %= alphabet_eng.Length;
if (deter == 0)
{
MessageBox.Show("deter = 0");
}
float a11 = 0, a12 = 0, a13 = 0, a21 = 0, a22 = 0, a23 = 0, a31 = 0, a32 = 0, a33 = 0;
a11 = (m22 * m33 - m23 * m32);
a12 = -1*(m21 * m33 - m23 * m31);
a13 = (m21 * m32 - m22 * m31);
a21 = -1 * (m12 * m33 - m13 * m32);
a22 = (m11 * m33 - m13 * m31);
a23 = -1 * (m11 * m32 - m12 * m31);
a31 = (m12 * m23 - m13 * m22);
a32 = -1 * (m11 * m23 - m13 * m21);
a33 = (m11 * m22 - m12 * m21);
float Ta11 = 0, Ta12 = 0, Ta13 = 0, Ta21 = 0, Ta22 = 0, Ta23 = 0, Ta31 = 0, Ta32 = 0, Ta33 = 0;
Ta11 = a11;
Ta12 = a21;
Ta13 = a31;
Ta21 = a12;
Ta22 = a22;
Ta23 = a32;
Ta31 = a13;
Ta32 = a23;
Ta33 = a33;
Ta11 *= deter;
Ta12 *= deter;
Ta13 *= deter;
Ta21 *= deter;
Ta22 *= deter;
Ta23 *= deter;
Ta31 *= deter;
Ta32 *= deter;
Ta33 *= deter;
Ta11 = normalize_element(Ta11);
Ta12 = normalize_element(Ta12);
Ta13 = normalize_element(Ta13);
Ta21 = normalize_element(Ta21);
Ta22 = normalize_element(Ta22);
Ta23 = normalize_element(Ta23);
Ta31 = normalize_element(Ta31);
Ta32 = normalize_element(Ta32);
Ta33 = normalize_element(Ta33);
Ta11 = Math.Abs(Ta11);
Ta12 = Math.Abs(Ta12);
Ta13 = Math.Abs(Ta13);
Ta21 = Math.Abs(Ta21);
Ta22 = Math.Abs(Ta22);
Ta23 = Math.Abs(Ta23);
Ta31 = Math.Abs(Ta31);
Ta32 = Math.Abs(Ta32);
Ta33 = Math.Abs(Ta33);
Tmatrix_11.Text = Ta11.ToString();
Tmatrix_12.Text = Ta12.ToString();
Tmatrix_13.Text = Ta13.ToString();
Tmatrix_21.Text = Ta21.ToString();
Tmatrix_22.Text = Ta22.ToString();
Tmatrix_23.Text = Ta23.ToString();
Tmatrix_31.Text = Ta31.ToString();
Tmatrix_32.Text = Ta32.ToString();
Tmatrix_33.Text = Ta33.ToString();
}
float normalize_element(float f)
{
if (f >= alphabet_eng.Length)
{
while (f >= alphabet_eng.Length)
f -= alphabet_eng.Length;
}
else if ((f + alphabet_eng.Length) < 0)
{
while ((f + alphabet_eng.Length) < 0)
f += alphabet_eng.Length;
f = alphabet_eng.Length + f;
}
return f;
}
请求你的帮助。