尝试解密我自己的密码时,我的C#程序正在冻结

时间:2014-02-20 16:52:30

标签: c# algorithm encryption matrix mono

所以我使用矩阵构建了自己的(玩具)加密算法。该程序应该用密钥加密明文,然后问你密钥并打印出解密的明文。一切正常,直到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;
        }
    }
}

稍作解释: 矩阵类是从互联网挖出来的,太大了,无法放在这里。 2.解释加密算法:

所有这一切都发生在代码中,代码生成两个矩阵,一个来自明文,另一个来自密钥(您可以猜测代码是如何生成的)。 然后它将密钥的每个元素和明文(不是矩阵乘法,只是每个元素)相乘,该算法也涉及随机数e.t.c.但我认为这里的问题是第二次没有正确重复将密钥转换为“密钥矩阵”的过程。无论如何,我不知道一个好的解决方案,所以请帮助。

1 个答案:

答案 0 :(得分:0)

好的,所以我解决了第一部分,它不再冻结我只是改变了

    for(int i = 0; i<times - 1; i++)
    {
        res = GetCharRep(GenMatrix(res));
    }

    for(int i = 0; i<times - 2; i++)
    {
        res = GetCharRep(GenMatrix(res));
    }

但这次ress矩阵和TwoMatrix矩阵的维度不匹配。