Exemplo n.º 1
0
        private static void ConsoleTest()
        {
            Console.WriteLine("Введите простые числа P и Q");
            var p = ulong.Parse(Console.ReadLine() ??
                                throw new InvalidOperationException("Пустая строка - число"));
            var q = ulong.Parse(Console.ReadLine() ??
                                throw new InvalidOperationException("Пустая строка - число"));

            var encryptor = new RSACryptor(p, q);

            Console.WriteLine("Введите строку для шифрования");
            var input = Console.ReadLine();

            var inputBytes = Encoding.UTF8.GetBytes(input ?? string.Empty);

            Console.WriteLine(input);
            Console.WriteLine("Input Bytes \r\n" + BitConverter.ToString(inputBytes));

            var encrypted = encryptor.Encrypt(inputBytes);

            Console.WriteLine(Encoding.UTF8.GetString(encrypted));
            Console.WriteLine("Encrypted Bytes \r\n" + BitConverter.ToString(encrypted));

            var decrypted = encryptor.Decrypt(encrypted);

            Console.WriteLine(Encoding.UTF8.GetString(decrypted));
            Console.WriteLine("Decrypted Bytes \r\n" + BitConverter.ToString(decrypted));

            Console.WriteLine(
                $"Public Key {encryptor.GetPublicKey()}\r\nPrivate Key {encryptor.GetPrivateKey()}\r\n");
        }
Exemplo n.º 2
0
        public static void EncryptionDecryptionTest(string p, string q, string input)
        {
            var rsa            = new RSACryptor(ulong.Parse(p), ulong.Parse(q));
            var inputBytes     = Encoding.UTF8.GetBytes(input);
            var decryptedBytes = rsa.Decrypt(rsa.Encrypt(inputBytes));

            Assert.AreEqual(inputBytes, decryptedBytes);
        }
Exemplo n.º 3
0
        public void Test_EncryptAndDecrypt()
        {
            var opensslPrivateKey =
                "MIICWwIBAAKBgQDCBtp6c2QRrJo+Z7zGpm9/9nCK83m0CUnDgiwa5eyQ4ltBDbncGztm9U1HY0lI+GkZgSIUNe0is0wP/iYe30ANWE8s73kS3P5MuA24nPI4R//BL7AgmhlLll9a+FBYaUrpCjZcNI4w1qXyR0n6X5HeU3UzybOdie1o35stHmCgBQIDAQABAoGAaD0YWVru8xPg1hATekHmez/h3LTLuK6Yw4GGwniuLHR/hCakqJy0wC6fcu/jamGSzVH0BhmmqdLb1We8AS/9j330Xc/0Kqw/7OdMwr+6qXjLXJ9oEgW2BePWQMavUibD5cvOwKBYReCsvGDBwI0tdlU/SgN9Cg5hU62VsHcd32ECQQDsXUU+W6k1vebv1NqFkGz2Xly9VHrSvwSqiByG+kFFHJ8/pJOJ5K54f3rc8jUeNcXoJ4zO6+RSD7n3eGuPH8tpAkEA0iUyCL5ZTFFWbKXi925Kl5PQi11f+t+p/OPTSP1SUXw4eWaZWN2YnBf18IEEE3zcidRFPwgLf+VJwysCFwboPQJAAY0DHUugqpeaYkx1Opcd/+fSl/Nr8uIJ98x403Hk570uVk6QIUF825GKjtSQAKi9qa5IwDrP/rHXuIXzvraosQJAAyiN9PWvb+c1DlL7804UDu0o0D9qBuI/ss5VyZ4NE65zRtfU7DIAbjAqASBfSE+zHNs04zqiuZxfnHBUCraO3QJAeBsgY+ECntfPBN2Hky5T29Zpl2P1aWStihR9LePUiAwTQXZLa8F3D5MX43YJ4Pf2MqMkI/fsLioTXHj/b38tMA==";
            var opensslPublicKey =
                "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCBtp6c2QRrJo+Z7zGpm9/9nCK83m0CUnDgiwa5eyQ4ltBDbncGztm9U1HY0lI+GkZgSIUNe0is0wP/iYe30ANWE8s73kS3P5MuA24nPI4R//BL7AgmhlLll9a+FBYaUrpCjZcNI4w1qXyR0n6X5HeU3UzybOdie1o35stHmCgBQIDAQAB";
            var source        = "只是告诉素数生成器帮我生成一个接近位的素数而已然后生成这是什么鬼额额额为啥呢呢";
            var encryptResult = RSACryptor.Encrypt(source, opensslPublicKey);

            Output.WriteLine("加密结果:" + encryptResult);
            var decryptResult = RSACryptor.Decrypt(encryptResult, opensslPrivateKey);

            Output.WriteLine("加密结果:" + decryptResult);
        }
Exemplo n.º 4
0
 public string Decrypt(string base64String)
 {
     return(RSACryptor.Decrypt(base64String, RSACryptor.Key));
 }
Exemplo n.º 5
0
        /*
         * This is entry point for PollardAlgo, it should
         * find all needed value for cryptografic functions
         * and take care of correct values in GUI
         */
        private void decryptButton_Click(object sender, EventArgs eventArg)
        {
            BigInteger N, e;

            try
            {
                e = BigInteger.Parse(eInput.Text);
                N = BigInteger.Parse(nInput.Text);
            }
            catch
            {
                MessageBox.Show("N or E is incorrect!");
                return;
            }

            if (N < 2 || e < 2)
            {
                MessageBox.Show("N and E should be more than 1!");
                return;
            }

            RhoPollard factorer = new RhoPollard();

            BigInteger factor;
            BigInteger iterations;
            Logger     timeLog = new Logger(reportOutput);


            /* TestOptimizations print time and iterations
             * in timeLog ( special widget )
             */
            //(factor, iterations) = factorer.TestOptimizations( N, timeLog );


            Stopwatch timer = new Stopwatch();

            timer.Start();
            (factor, iterations) = factorer.GetOneDivider(N);
            timer.Stop();

            timeLog.Log($"All optimizations timing = {timer.Elapsed}\n");


            BigInteger p, q;

            p = factor;
            q = N / p;
            RSACryptor rsa = RSACryptor.UnsafeFactory(p, q, e);

            pOutput.Text = p.ToString();
            qOutput.Text = q.ToString();
            dOutput.Text = rsa.D.ToString();

            BigInteger ciphered;

            try
            {
                ciphered = BigInteger.Parse(cipheredText.Text);
            }
            catch
            {
                MessageBox.Show("Bad ciphered text");
                return;
            }

            BigInteger encodedText = new BigInteger(rsa.Decrypt(ciphered));

            uncipheredText.Text = BigIntToMubarakov(encodedText);
        }