Example #1
0
        public LatticeND ToLatticeND()
        {
            LatticeND latticeND = new LatticeND(cols, rows, false);

            for (int i = 0; i < cols; i++)
            {
                latticeND.Vectors[i] = new VectorND(rows);
                for (int j = 0; j < rows; j++)
                {
                    latticeND.Vectors[i].values[j] = new BigInteger(mat[j, i]);
                }
            }
            return(latticeND);
        }
Example #2
0
        public LatticeND TransformToLattice(BigInteger sum)
        {
            LatticeND lattice = new LatticeND(publicKey.dim + 1, publicKey.dim + 1, false);

            for (int i = 0; i < publicKey.dim; i++)
            {
                lattice.Vectors[i]                       = new VectorND(publicKey.dim + 1);
                lattice.Vectors[i].values[i]             = 1;
                lattice.Vectors[i].values[publicKey.dim] = publicKey.values[i];
            }
            lattice.Vectors[publicKey.dim] = new VectorND(publicKey.dim + 1);
            lattice.Vectors[publicKey.dim].values[publicKey.dim] = sum * (-1);

            return(lattice);
        }
Example #3
0
        public void GenerateLattice()
        {
            lattice = new LatticeND(dim, dim, false);

            //Umwandlung der Matrizeneinträge von double zu BigInt und damit Umwandlung der Matrizen zu einem Gitter

            VectorND[] privateVectors = new VectorND[dim];
            for (int i = 0; i < dim; i++)
            {
                BigInteger[] privateBigInts = new BigInteger[dim];
                for (int j = 0; j < dim; j++)
                {
                    privateBigInts[j] = new BigInteger(privateKeyR[j, i]);
                }
                privateVectors[i] = new VectorND(privateBigInts);
            }
            lattice.ReducedVectors = privateVectors;

            VectorND[] publicVectors = new VectorND[dim];
            for (int i = 0; i < dim; i++)
            {
                BigInteger[] publicBigInts = new BigInteger[dim];
                for (int j = 0; j < dim; j++)
                {
                    publicBigInts[j] = new BigInteger(publicKeyB[j, i]);
                }
                publicVectors[i] = new VectorND(publicBigInts);
            }
            lattice.Vectors = publicVectors;

            if (transU == null)
            {
                return;
            }

            VectorND[] transVectors = new VectorND[dim];
            for (int i = 0; i < dim; i++)
            {
                BigInteger[] transBigInts = new BigInteger[dim];
                for (int j = 0; j < dim; j++)
                {
                    transBigInts[j] = new BigInteger(transU[j, i]);
                }
                transVectors[i] = new VectorND(transBigInts);
            }
            lattice.UnimodTransVectors = transVectors;
        }
Example #4
0
        public string Cryptanalysis(VectorND cipher, Paragraph paragraph)
        {
            string messageBinary = "";

            foreach (BigInteger value in cipher.values)
            {
                //BigInteger tempValue = value;

                LatticeND lattice = TransformToLattice(value);

                //Anmerkung: Der zweite Versuch nach LAGARIAS/ODLYZKO, S.232 funktioniert leider nicht,
                //daher wurde dieser Teil auskommentiert

                //for (int j = 0; j <= 1; j++)
                //{
                lattice.LLLReduce();

                BigInteger[] vector = FindBinaryVector(lattice.ReducedVectors, value);

                //    if (vector != null)
                //        break;

                //    //Zweiter Versuch
                //    tempValue = Util.Sum(publicKey.ToList()) - tempValue;
                //    lattice = TransformToLattice(tempValue);
                //}

                if (vector == null)
                {
                    throw new Exception();
                }

                if (paragraph != null)
                {
                    paragraph.Inlines.Add("--------------------\r\n");
                    paragraph.Inlines.Add(new Bold(new Run(Languages.labelCurrentBlock)));
                    paragraph.Inlines.Add(" " + value + "\r\n");
                    paragraph.Inlines.Add(new Bold(new Run(Languages.labelLatticeBasis)));
                    paragraph.Inlines.Add(" " + lattice.LatticeToString() + "\r\n");
                    paragraph.Inlines.Add(new Bold(new Run(Languages.labelReducedLatticeBasis)));
                    paragraph.Inlines.Add(" " + lattice.LatticeReducedToString() + "\r\n");
                    paragraph.Inlines.Add(new Bold(new Run(Languages.labelFoundVector)));
                    paragraph.Inlines.Add(" " + new VectorND(vector) + "\r\n");
                }

                for (int k = 0; k < dim; k++)
                {
                    messageBinary += vector[k];
                }
            }

            int redudandBits = (messageBinary.Length) % maxBitLength;

            if (redudandBits > 0)
            {
                messageBinary = messageBinary.Remove(messageBinary.Length - redudandBits);
            }
            if (messageBinary.Length >= maxBitLength && messageBinary.Substring(messageBinary.Length - maxBitLength).IndexOf('1') == -1)
            {
                messageBinary = messageBinary.Remove(messageBinary.Length - maxBitLength);
            }

            if (paragraph != null)
            {
                paragraph.Inlines.Add("--------------------\r\n");
                paragraph.Inlines.Add(new Bold(new Run(Languages.labelPlainTextBinary)));
                paragraph.Inlines.Add(" " + messageBinary + "\r\n");
            }

            List <byte> bytes = new List <byte>();

            for (int i = 0; i < messageBinary.Length; i += maxBitLength)
            {
                bytes.Add(Convert.ToByte(messageBinary.Substring(i, maxBitLength), 2));
            }

            string messageUTF8 = Encoding.UTF8.GetString(bytes.ToArray());

            if (paragraph != null)
            {
                paragraph.Inlines.Add(new Bold(new Run(Languages.labelPlainTextUTF8)));
                paragraph.Inlines.Add(" " + messageUTF8 + "\r\n");
            }

            return(messageUTF8);
        }