public bool DecryptData(string signed, string msg = "") { if (Init()) { if (_type == 1) { //SHA256 sha256Hash = SHA256.Create(); //byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(msg)); byte[] bsigned = StringToByteArray(signed); byte[] bytes = Encoding.UTF8.GetBytes(msg); var rsa = RSA.Create(); rsa.KeySize = _keySize; rsa.ImportSubjectPublicKeyInfo(Convert.FromBase64String(_publicKey), out int byteReads); var result = rsa.VerifyData(bytes, bsigned, HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1); return(result); } else { PGPLib pgp = new PGPLib(); byte[] bytes = Encoding.UTF8.GetBytes(_publicKey); var stream = new MemoryStream(bytes); SignatureCheckResult signatureCheck = pgp.VerifyString(signed, stream, out string plainText); var result = signatureCheck == SignatureCheckResult.SignatureVerified; return(result); } } else { return(false); } }
public FileContentInfo(PackageFile file, string name, object content, bool isTextFile, long size, IReadOnlyList <AuthenticodeSignature> signatures, SignatureCheckResult signatureStatus) { File = file; Name = name; Content = content; IsTextFile = isTextFile; Size = size; SignatureStatus = signatureStatus; Signatures = signatures; }
public void Decreypt(string encrypt) { //Get Public File GetKey objKey = new GetKey(); int ID = Convert.ToInt32(Session["ID"].ToString()); DataRow drKey = objKey.drSearchStudentKey(ID); string PublicKey1 = drKey[0].ToString(); // obtain an OpenPGP signed message String signedString = encrypt; // Extract the message and check the validity of the signature String plainText; // create an instance of the library PGPLib pgp = new PGPLib(); //SignatureCheckResult signatureCheck = pgp.VerifyString(signedString, // new FileInfo(@"C:\Users\Dua'a-Orcas\Desktop\finalProject\WebApplication1 - Copy (2)\WebApplication1\Sig\public_key_exported.asc"), // out plainText); SignatureCheckResult signatureCheck = pgp.VerifyString(signedString, new FileInfo(PublicKey1), out plainText); string strData1 = plainText; // Print the results Console.WriteLine("Extracted plain text message is " + plainText); if (signatureCheck == SignatureCheckResult.SignatureVerified) { Console.WriteLine("Signature OK"); Result = true; } else if (signatureCheck == SignatureCheckResult.SignatureBroken) { Console.WriteLine("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"); } else if (signatureCheck == SignatureCheckResult.NoSignatureFound) { Console.WriteLine("This message is not digitally signed"); } }
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."); } } }