Beispiel #1
0
        public static void DecryptDataWithMicrosoft(string fileName, RSAKey key)
        {
            string[] temp         = fileName.Split('.');
            string   saveFileName = temp[0] + "_decryptedECB";

            if (temp.Length == 2)
            {
                saveFileName = temp[0] + "_decryptedECB." + temp[1];
            }

            FileStream readStream  = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            FileStream writeStream = new FileStream(saveFileName + "microsoft", FileMode.Create, FileAccess.Write);

            using (StreamWriter writer = new StreamWriter(writeStream))
            {
                using (StreamReader sr = new StreamReader(readStream))
                {
                    using (RSACryptoServiceProvider crypto = new RSACryptoServiceProvider())
                    {
                        RSAParameters rsaKey = new RSAParameters();
                        rsaKey.Modulus  = key.n.ToByteArray();
                        rsaKey.Exponent = key.e.ToByteArray();
                        rsaKey.D        = key.d.ToByteArray();
                        rsaKey.P        = key.p.ToByteArray();
                        rsaKey.Q        = key.q.ToByteArray();
                        rsaKey.DQ       = (key.d % (key.q - 1)).ToByteArray();
                        rsaKey.DP       = (key.d % (key.p - 1)).ToByteArray();
                        rsaKey.InverseQ = RSAKeyGenerator.ModularInverse(key.q, key.p).ToByteArray();
                        crypto.ImportParameters(rsaKey);

                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            byte[] bytes         = Encoding.UTF8.GetBytes(line);
                            string testowy       = Encoding.UTF8.GetString(bytes);
                            byte[] decryptedData = crypto.Decrypt(Convert.FromBase64String(testowy), RSAEncryptionPadding.Pkcs1);
                            writer.Write(Encoding.UTF8.GetString(decryptedData));
                        }
                    }
                }
            }
            readStream.Close();
            writeStream.Close();
        }
Beispiel #2
0
        public void SaveConfig()
        {
            FileStream stream = new FileStream("config.rsa", FileMode.OpenOrCreate);

            using (StreamWriter sw = new StreamWriter(stream))
            {
                sw.WriteLine("asn1 = SEQUENCE:rsa_key");
                sw.WriteLine("");
                sw.WriteLine("[rsa_key]");
                sw.WriteLine("version=INTEGER:0");
                sw.WriteLine($"modulus=INTEGER:{n}");
                sw.WriteLine($"pubExp=INTEGER:{e}");
                sw.WriteLine($"privExp=INTEGER:{d}");
                sw.WriteLine($"p=INTEGER:{p}");
                sw.WriteLine($"q=INTEGER:{q}");
                sw.WriteLine($"e1=INTEGER:{d % (p - 1)}");
                sw.WriteLine($"e2=INTEGER:{d % (q - 1)}");
                sw.WriteLine($"coeff=INTEGER:{RSAKeyGenerator.ModularInverse(q, p)}");
            }
            string cmdText = $"/C openssl asn1parse -genconf config.rsa -out newkey.der";

            Process.Start("CMD.exe", cmdText);
            Process proc = new Process();

            proc.StartInfo.UseShellExecute        = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError  = true;
            proc.StartInfo.FileName  = "CMD.exe";
            proc.StartInfo.Arguments = "/C openssl rsa -in newkey.der -inform der -text -check";
            proc.EnableRaisingEvents = true;
            proc.OutputDataReceived += Proc_OutputDataReceived;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.WaitForExit();
            cmdText = $"/C openssl rsa -in newkey.der -inform DER -outform PEM -out id_rsa";
            proc    = Process.Start("CMD.exe", cmdText);
            proc.WaitForExit();
            cmdText = $"/C openssl rsa -in id_rsa -pubout > id_rsa.pub";
            Process.Start("CMD.exe", cmdText);
        }