如果你想在Linux或任何平臺使用單聲道的PGP電子郵件,說真的,我建議在http://github.com/jstedfast/MimeKit
API文檔看我MimeKit庫可以在http://jstedfast.github.io/MimeKit/docs
所有你需要找到做的是子類MimeKit.Cryptography.GnuPGContext覆蓋抽象getPassword來()方法,然後調用:
MimeKit.Cryptography.CryptographyContext.Register (typeof (MyGnuPGContext));
從此,來加密消息,使用MimeKit.Cryptography.MultipartEncrypted像這樣:
var message = new MimeMessage();
// TODO: set the From, To, Subject, etc...
// create the message body
var body = new TextPart() { Text = "This is the message text..." };
// encrypt the body of the message
var encrypted = MultipartEncrypted.Create (message.To.Mailboxes, body);
message.Body = encrypted;
現在,您的郵件正文以RFC兼容的方式加密。
幾個月前,在編寫我的MimeKit庫時,我想添加PGP支持(如果可以的話,通過GnuPG),而我遇到的問題是使用System.Diagnostics.Process的靈活性不足以調用gpg我需要的方式(爲了以安全的方式使用gpg,您需要打開多個管道到gpg,以便您不必先將密碼和其他上下文寫入磁盤)。我已經在C中完成了這個工作,但是例如Process.Start()只允許我管理標準輸入,標準輸出和標準錯誤,這還不夠。
GnuPG項目有一個名爲libgpgme的庫,如果最終用C編寫代碼,那麼它真的很棒,但在C#中沒有任何完整的綁定。我能找到的最好的是https://github.com/danm-de/gpgme-sharp,但它只適用於32位,現在很多系統都是64位。
無論如何... MimeKit可以執行pgp簽名,驗證,加密,解密,導入/導出密鑰以及諸如sign + encrypt和decrypt + verify等組合。再加上它實現了所有的S/MIME(包括我沒有找到的其他S/MIME庫支持的東西,例如證書專用和壓縮數據消息)。
自述文件提供了有關如何構建MimeKit的指導(這很簡單,但確實需要您克隆我的bc-csharp github存儲庫以及所有較低級別的加密邏輯)。
希望有所幫助。
P.S.我忘了提及MimeKit是完全跨平臺的 - 它可以在Windows,Linux和Mac上運行。 MimeKit本身(減去加密)也可以在iOS和Android上使用。我這個週末的目標是讓加密的東西在iOS和Android上工作併爲此製作軟件包。
Now ... MimeKit不幸的是還沒有兼容的SmtpClient,但是你可以使用MimeKit來做你的PGP加密,然後提取加密的內容在System.Net中使用。郵件是這樣的:
// create a list of recipients
var recipients = new List<MailboxAddress>();
recipients.Add (new MailboxAddress ("Joe Sixpack", "[email protected]"));
// create the message body
var body = new TextPart() { Text = "This is the message text..." };
// encrypt the body of the message
var multipart = MultipartEncrypted.Create (recipients, body);
// get the part that contains the ascii-armored pgp content
// (the second mime part of the multipart/encrypted)
var encrypted = (MimePart) multipart[1];
// get the ascii-armored encrypted content
var memory = new MemoryStream();
encrypted.ContentObject.WriteTo (memory);
memory.Position = 0;
// at this point, 'memory' has the "--- BEGIN PGP MESSAGE ---" data
// and you can create your System.Net.Mail.MimeMessage and add a
// mime part with the memory stream as its content
我可能會考慮看到的它會涉及到添加一個方法或強制轉換運算符的MimeKit.MimeMessage轉換成System.Net.Mail.MimeMessage,但我可能不會得到周圍這個週末取決於我需要多長時間才能獲得在iOS和Android上運行的加密工具。
更新:我現在有另一個名爲MailKit的庫,可用於通過SMTP發送消息。它還支持POP3和IMAP。
我現在也已經完成了將MimeKit的加密支持移植到iOS和Android ...所以MimeKit現在完全支持Windows,Mac,Linux,iOS和Android。聲稱有多少其他MIME庫可以製作? :-)
你想執行什麼樣的加密? .Net和mono爲非對稱加密提供RSACryptoServiceProvider和DSACryptoServiceProvider,那麼爲什麼要使用extertal程序呢? – Deffiss
有趣!但RSACryptoServiceProvider可以使用GnuPG公鑰文件(.asc)嗎? GnuPG電子郵件客戶端能夠解密使用RSACryptoServiceProvider加密的郵件嗎? – Boris
我不能使用.asc直接使用RSACryptoServiceProvider,但我相信你可以將.asc文件轉換爲xml,然後使用xml創建RSACryptoServiceProvider(請參閱MSDN上的FromXmlString()方法)。是的,您可以使用RSACryptoServiceProvider在一側使用公鑰對消息進行加密,並使用任何其他庫或工具在另一側使用私鑰對其解密。 – Deffiss