private void displayChainVisual_Click(object sender, EventArgs e) { //Opens a popup window displaying the chain in pretty format SimpleReportViewer.ShowDialog(Program.ProjectD.ReadChainPretty(), "Chain data (pretty)", this); }
public static void Decrypt(string encryptedMessage, string secretKeyPath, string publicKeyPath, bool gui = false, string privatePassWord = "") { string plainTextExtracted; if (privatePassWord == "") { if (!gui) { Console.Write("Please enter the passphrase of the chosen private key: "); privatePassWord = Console.ReadLine(); } else { privatePassWord = Prompt.ShowDialog("Enter the password of the chosen secret key", "Password entry", false, false, false); } } // create an instance of the library PGPLib pgp = new PGPLib(); // decrypt and verify try { SignatureCheckResult signatureCheck = pgp.DecryptAndVerifyString(encryptedMessage, new FileInfo(secretKeyPath), //secret key path privatePassWord, //this is the password of the secret key new FileInfo(publicKeyPath), out string plainTextExtract); plainTextExtracted = plainTextExtract; // print the results if (signatureCheck == SignatureCheckResult.SignatureVerified) { Console.WriteLine("Signature OK"); if (gui) { MessageBox.Show("Signature OK"); } } else if (signatureCheck == SignatureCheckResult.SignatureBroken) { Console.WriteLine("Signature of the message is either broken or forged"); if (gui) { MessageBox.Show("Signature of the message is either broken or forged"); } } else if (signatureCheck == SignatureCheckResult.PublicKeyNotMatching) { Console.WriteLine("The provided public key doesn't match the signature"); if (gui) { MessageBox.Show("The provided public key doesn't match the signature"); } } else if (signatureCheck == SignatureCheckResult.NoSignatureFound) { Console.WriteLine("This message is not digitally signed"); if (gui) { MessageBox.Show("This message is not digitally signed"); } } if (!gui) { Console.WriteLine("Extracted message: \n" + plainTextExtracted); } else { SimpleReportViewer.ShowDialog(plainTextExtracted, "Decrypted data", Program.genericGUIForm); } } catch (Exception e) { if (e is DidiSoft.Pgp.Exceptions.WrongPrivateKeyException) { Console.WriteLine("The chosen private key is either not a private key or not suited to decrypt this message."); if (gui) { MessageBox.Show("The chosen private key is either not a private key or not suited to decrypt this message."); } //The supplied private key source is not a private key at all } else if (e is DidiSoft.Pgp.Exceptions.WrongPasswordException) { Console.WriteLine("The entered passphrase is incorrect, please try again."); if (!gui) { Decrypt(encryptedMessage, secretKeyPath, publicKeyPath); } else { MessageBox.Show("The entered passphrase is incorrect, please try again."); } } else if (e is DidiSoft.Pgp.Exceptions.WrongPublicKeyException) { if (!gui) { Console.WriteLine("The chosen public key is either not a public key or not suited to verify this message."); } else { MessageBox.Show("The chosen public key is either not a public key or not suited to verify this message."); } } else if (e is DidiSoft.Pgp.Exceptions.KeyIsExpiredException) { Console.WriteLine("The public key you want to encrypt for is expired and cannot be used."); if (gui) { MessageBox.Show("The public key you want to encrypt for is expired and cannot be used."); } //Can be worked around by setting UseExpiredKeys to true } else if (e is DidiSoft.Pgp.Exceptions.KeyIsRevokedException) { Console.WriteLine("The public key you want to encrypt for appears to be revoked and cannot be used."); if (gui) { MessageBox.Show("The public key you want to encrypt for appears to be revoked and cannot be used."); } //Can be worked around by setting UseRevokedKeys to true } else if (e is DidiSoft.Pgp.Exceptions.NonPGPDataException) { Console.WriteLine("The data you want to decrypt is not encrypted with PGP."); if (gui) { MessageBox.Show("The data you want to decrypt is not encrypted with PGP."); } //Can be worked around by setting UseRevokedKeys to true } else if (e is IOException) { Console.WriteLine("IO Exception has occured, decrypting of unencrypted data is not possible."); if (gui) { MessageBox.Show("IO Exception has occured, decrypting of unencrypted data is not possible."); } //Can be worked around by setting UseRevokedKeys to true } else { throw new ApplicationException("Something unexpected went wrong, contact support and explain your actions in detail and chronological order."); } } }
private void DisplayChainFromGUI(object sender, EventArgs e) { // Opens a popup window displaying the entire chain SimpleReportViewer.ShowDialog(JsonConvert.SerializeObject(Program.ProjectD, Formatting.Indented), "Chain data", this); }