Exemplo n.º 1
0
        public string ReceiveMessageFrom(SignCertificate senderCertificate, Message received, out bool signatureIsOk)
        {
            var messageNums        = received.GetMessage().ToList();
            var messageDecodedNums = RsaUtility.Decode(messageNums, m_decodingKey).ToList();

            string message = m_converter.FromNumbers(messageDecodedNums);

            var signatureNums = received.GetSignature();

            signatureIsOk = RsaSignature.SignatureIsValid(message, signatureNums, m_converter, senderCertificate);

            _ReleaseList(messageNums);
            _ReleaseList(messageDecodedNums);
            _ReleaseList(signatureNums);

            return(message);
        }
Exemplo n.º 2
0
        private static void SpeedTest()
        {
            if (File.Exists(c_speedTestFile))
            {
                File.Delete(c_speedTestFile);
            }

            List <string> testPoints = new List <string>();
            var           sb         = new StringBuilder();

            for (int i = 1; i <= 200; i++)
            {
                sb.Append(System.Guid.NewGuid());
                if (i % 20 == 0)
                {
                    testPoints.Add(sb.ToString());
                }
            }

            var keyGen = new KeyGenerator();
            var keys   = new List <KeyGenerator.Key>();

            for (int i = 0; i < 15; i++)
            {
                int byteLength = 16 + 8 * i;
                keys.Add(keyGen.GenerateKey(byteLength));

                Console.WriteLine($"Generated key with size: {byteLength * 8}");
            }

            string header = "keyBits, mode (enc/dec), ";

            for (int i = 0; i < testPoints.Count; i++)
            {
                header += $"{testPoints[i].Length} chars, ";
            }
            header = header.Substring(0, header.Length - 2);
            _LogToFile(header);

            var watch = new Stopwatch();

            for (int keyInd = 0; keyInd < keys.Count; keyInd++)
            {
                int keySize = 8 * (16 + 8 * keyInd);

                var    encripted = new List <List <OmgNum> >();
                string row       = $"{keySize}, enc, ";

                var converter = new MessageConverter(16 + 8 * keyInd - 1);

                for (int i = 0; i < testPoints.Count; i++)
                {
                    watch.Restart();

                    var mesageNums = converter.ToNumbers(testPoints[i]).ToList();
                    encripted.Add(RsaUtility.Encode(mesageNums, keys[keyInd].pub).ToList());
                    foreach (var n in mesageNums)
                    {
                        n.Release();
                    }

                    watch.Stop();
                    row += $"{watch.ElapsedMilliseconds}, ";
                }

                row = row.Substring(0, row.Length - 2);
                _LogToFile(row);

                row = $"{keySize}, dec, ";
                for (int i = 0; i < testPoints.Count; i++)
                {
                    watch.Restart();

                    var    decodedNums = RsaUtility.Decode(encripted[i], keys[keyInd].priv).ToList();
                    string text        = converter.FromNumbers(decodedNums);
                    foreach (var n in decodedNums)
                    {
                        n.Release();
                    }

                    watch.Stop();
                    row += $"{watch.ElapsedMilliseconds}, ";

                    if (text != testPoints[i])
                    {
                        Console.WriteLine($"Failed at index {i} with keylength {keySize}");
                    }
                }

                row = row.Substring(0, row.Length - 2);
                _LogToFile(row);

                Console.WriteLine($"Tested key {keyInd + 1} / {keys.Count}.");
            }
        }