【发布时间】:2014-02-20 16:52:29
【问题描述】:
所以我使用矩阵构建了自己的(玩具)加密算法。该程序应该使用密钥加密明文,然后询问您密钥并打印出解密的明文。一切正常,直到 C# 程序从密钥生成“密钥矩阵”以解密密码。请注意,它第一次从密钥生成“密钥矩阵”是可以的,但第二次它会冻结。这是代码。
using System;
namespace MatrixEncryption
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine("Enter a relatively short piece of text:");
string txt = Console.ReadLine();
txt += "L";
Console.WriteLine("Enter an encryption key:");
string ekey = Console.ReadLine();
int times = 0;
while(true)
{
if(txt.Length + 1 < (Math.Pow((double)ekey.Length,(double)times))) break;
times++;
}
times++;
Console.WriteLine(times);
Matrix keym = new Matrix(ekey.Length^times, ekey.Length^times);
string res = ekey;
for(int i = 0; i<times - 1; i++)
{
res = GetCharRep(GenMatrix(res));
}
keym = GenMatrix(res);
Console.WriteLine(keym.cols + ":" + keym.rows);
Matrix passm = new Matrix(keym.rows, keym.cols);
int unicnt = 0;
Random a = new Random();
for(int i = 0; i<(keym.rows); i++)
{
for(int j = 0; j<(keym.cols); j++)
{
if(unicnt == 0)
{
passm[i, j] = (byte)txt.Length;
unicnt++;
continue;
}
if(unicnt < txt.Length)
{
passm[i, j] = (byte)txt[unicnt - 1];
}
else
{
int aa = a.Next();
passm[i, j] = (byte)(aa % byte.MaxValue);
}
unicnt++;
}
}
Matrix cipherm = new Matrix(keym.rows, keym.cols);
Matrix TwoMatrix = new Matrix(cipherm.rows, cipherm.cols);
for(int i = 0; i<TwoMatrix.rows; i++)
{
for(int j = 0; j<TwoMatrix.cols; j++)
{
TwoMatrix[i, j] = 2;
}
}
cipherm = NotMul(passm + TwoMatrix, keym + TwoMatrix);
Console.WriteLine("Enciphered text : " + GetCharRep(cipherm));
Console.WriteLine("Enter key now to check if decryptable cipher : ");
string keyy = Console.ReadLine();
int timesx = cipherm.cols;
double aaa = (double)timesx;
double bbb = (double)keyy.Length;
timesx = (int)Math.Log(aaa, bbb);
Matrix keymx = new Matrix(timesx, timesx);
string resss = keyy;
for(int i = 0; i<timesx; i++)
{
resss = GetCharRep(GenMatrix(resss));
}
keymx = GenMatrix(resss);
if(keymx != keym)
{
Console.WriteLine("STUPID COMPUTER xD");
Console.WriteLine(cipherm.cols.ToString() + ":" + cipherm.rows.ToString());
}
Matrix ress = new Matrix(keymx.rows, keymx.cols);
keymx = keymx + TwoMatrix;
ress = NotDiv(cipherm, keymx);
ress = ress - TwoMatrix;
Console.WriteLine(GetCharRep(ress).Substring(0, (int)(ress[0, 0])));
}
public static Matrix NotMul(Matrix m1, Matrix m2)
{
Matrix result = Matrix.ZeroMatrix(m1.cols, m1.rows);
for(int i = 0; i<m1.rows; i++)
{
for(int j = 0; j<m2.cols; j++)
{
result[i, j] = (m1[i, j] * m2[i, j]);
}
}
return result;
}
public static Matrix NotDiv(Matrix m1, Matrix m2)
{
Matrix result = Matrix.ZeroMatrix(m1.cols, m1.rows);
for(int i = 0; i<m1.rows; i++)
{
for(int j = 0; j<m2.cols; j++)
{
result[i, j] = m1[i, j] * (1/m2[i, j]);
}
}
return result;
}
public static Matrix GenMatrix(string key)
{
Matrix ret = new Matrix(key.Length, key.Length);
for(int i = 0; i<key.Length; i++)
{
for(int j = 0; j<key.Length; j++)
{
ret[i, j] = (byte)(Math.Pow((double)key[i], (double)key[j]) % byte.MaxValue);
}
}
return ret;
}
public static string GetCharRep(Matrix INM)
{
string ret = "";
for(int i = 0; i<INM.rows; i++)
{
for(int j = 0; j<INM.cols; j++)
{
ret += (char)((byte)INM[i, j] % byte.MaxValue);
}
}
return ret;
}
}
}
稍微解释一下: 1. 矩阵类是从网上挖来的,太大了,放在这里。 2.解释加密算法:
简而言之,代码生成了两个矩阵,一个来自明文,一个来自密钥(您可以猜到它是如何由代码生成的)。 然后它将密钥的每个元素和明文相乘(不是矩阵乘法,只是每个元素),该算法还涉及随机数等。但是我认为这里的问题是第二次没有正确重复将密钥转换为“密钥矩阵”的过程。无论如何,我不知道一个好的解决方案,所以请帮忙。
【问题讨论】:
-
只是一个评论 - 在我稍微调整算法之前它就起作用了
-
如果您有一个非常相似的版本,那么最简单的故障排除方法可能是将工作版本与新的“调整”版本进行比较。例如,您可以一次添加一个“调整”,直到它中断,然后进入该部分代码。保存当前代码的单独副本,然后使用 CTRL-Z 恢复之前的工作以进行比较。您在这里提供的信息不足以让我们猜出答案。
-
我担心如果我提供了所有信息,可能会花太多时间来适应这里 xD。
标签: c# algorithm encryption matrix mono