C#:如何将私钥保存在 PEM 文件中以便稍后在 OpenSSL 中使用

问题描述

我在 C# 中使用非对称密钥加密和解密一些数据。 我正在使用 C# 控制台应用程序执行以下操作:

  1. 生成公钥和私钥
  2. 使用公钥加密当前日期和时间
  3. 将公钥、私钥、加密数据保存在本地目录

在这种情况下,public 和 private 都保存为 .txt,这是不安全的,不能在 OpenSSL 命令中使用。加密文件为 .dat 文件格式。

最好以 .pem 文件格式拥有公共和私人文件,并以 .txt 格式拥有加密数据。那里的所有方法都很复杂,我只需要一种简单的方法即可。我一直在寻找一个简单的方法,但我还没有找到。

私钥和加密文件分别保存为.pem和.txt后,我希望能够执行以下任一命令:

openssl rsautl -decrypt -inkey /path/to/my/private_key.key -in /path/to/encrypted.txt -out /path/where/I/want/my/decrypted.txt

openssl rsautl -decrypt -in /path/to/encrypted.txt -out /path/where/I/want/my/decrypted.txt -inkey /path/to/my/private_key.pem

这是我的代码加解密:

static void Main(string[] args)
        {
            // get string: current time
            var currentDateTimeForLicense = DateTime.Now.ToString("hh:mm:ss dd/MM/yyyy");

            // initliaze dir
            string mainDir = "C:\\temp\\license\\";
            string encryptedLicenseDir = "C:\\temp\\license\\encryptedLicense.dat";
            // Check if folder already exists. If yes,delete it
            if (Directory.Exists(mainDir))
            {
                Directory.Delete(mainDir,true);
            }

            System.IO.Directory.CreateDirectory(mainDir); // create the folder

            // Create an instance of the RSA algorithm class
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            // Get the public key
            string publicKey = rsa.ToXmlString(false); // false to get the public key
            string privateKey = rsa.ToXmlString(true); // true to get the private key

            // Call the encryptText method
            EncryptText(publicKey,currentDateTimeForLicense,encryptedLicenseDir);

            File.AppendAllText(mainDir + "licensekey_OriginalNonEnrypter.txt",currentDateTimeForLicense); // save current datetime
            File.AppendAllText(mainDir + "publicKey.txt",publicKey); // save publicKey
            File.AppendAllText(mainDir + "privateKey.txt",privateKey); // save privateKey
            //Console.ReadKey();
        }

        // Create a method to encrypt a text and save it to a specific file using a RSA algorithm public key 
        static void EncryptText(string publicKey,string text,string fileName)
        {
            // Convert the text to an array of bytes 
            UnicodeEncoding byteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = byteConverter.GetBytes(text);

            // Create a byte array to store the encrypted data in it
            byte[] encryptedData;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                // Set the rsa pulic key
                rsa.FromXmlString(publicKey);

                // Encrypt the data and store it in the encyptedData Array 
                encryptedData = rsa.Encrypt(dataToEncrypt,false);
            }
            // Save the encypted data array into a file
            File.WriteallBytes(fileName,encryptedData);
            //File.WriteallBytes(fileName,encryptedData);

            Console.WriteLine("Data has been encrypted");
        }

        // Method to decrypt the data withing a specific file using a RSA algorithm private key 
        static string DecryptData(string privateKey,string fileName)
        {
            // read the encrypted bytes from the file
            byte[] dataToDecrypt = File.ReadAllBytes(fileName);

            // Create an array to store the decrypted data in it 
            byte[] decryptedData;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                // Set the private key of the algorithm 
                rsa.FromXmlString(privateKey);
                decryptedData = rsa.Decrypt(dataToDecrypt,false);
            }

            // Get the string value from the decryptedData byte array 
            UnicodeEncoding byteConverter = new UnicodeEncoding();
            return byteConverter.GetString(decryptedData);
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)