A file encrypted using the following openssl command:

openssl  smime  -encrypt -aes256  -in InFilePath -binary  -outform DEM  
-out OutFilePath PublicKeyFilePath
Decrypt the file
	AsymmetricKeyParameter Key;

	using (var Stream1 = File.OpenRead("C:\\My Folder\\my_private_key1.pem"))
	{
		using (var Reader1 = new StreamReader(Stream1))
		{
			var Pem1 = new PemReader(Reader1);

			var KeyObject = Pem1.ReadObject();

			if (KeyObject is AsymmetricCipherKeyPair pair)
			{
				Key = pair.Private;
			}
			else if (KeyObject is AsymmetricKeyParameter)
			{
				Key = (AsymmetricKeyParameter)KeyObject;
			}
			else
			{
				return;
			}
		}
	}

	var EncryptedData = File.ReadAllBytes("C:\\My Folder\\EncryptedFile.bin");

	var Parser = new CmsEnvelopedDataParser (EncryptedData);
	var Recipients = Parser.GetRecipientInfos ();
	byte[] DecryptedData = new Byte[0];

	foreach (RecipientInformation recipient in Recipients.GetRecipients())
	{
		DecryptedData = recipient.GetContent(Key);
		break;
	}

	if (DecryptedData.Length > 0)
	{
		FileStream File2 = new FileStream("C:\\My Folder\\DecruptedFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
		File2.Write(DecryptedData);
		File2.Close();
	}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *