private void buttonVerifyDecrypt_Click(object senderObject, EventArgs e) { outputPath = textBoxPath.Text; if (outputPath.Equals("")) { MessageBox.Show("Bir çıktı dosyası belirtiniz."); return; } if (File.Exists(outputPath)) { MessageBox.Show("Aynı isimde başka bir dosya mevcut. Lütfen başka bir isim seçiniz."); return; } // Gonderen ve alici id'lerinin alinarak hangi sertifikalara ait oldugunun belirlenmesi try { string recipientID = File.ReadAllText(signedEncryptedDir + "recipient.txt"); foreach (Certificate user in currentUsers) { if (user.ID.Equals(recipientID)) { recipient = user; break; } } string importedSenderID = File.ReadAllText(signedEncryptedDir + "sender.txt"); foreach (Certificate user in currentImportedUsers) { if (user.ID.Equals(importedSenderID)) { importedSender = user; break; } } } catch { MessageBox.Show("Kullanıcı sertifikaları bulunamadı."); return; } // Dosyalari desifreleme ve dogrulama if (File.Exists(signedEncryptedDir + fileName + ".enc") && File.Exists(signedEncryptedDir + fileName + ".sig")) { fileFound = true; try { recipient.rsaPrivateKeyInfo = recipient.GetPrivateKey("Recipient\\", this); encryptedData = File.ReadAllBytes(signedEncryptedDir + fileName + ".enc"); encryptedPassphrase = File.ReadAllBytes(signedEncryptedDir + "password"); byte[] decryptedData = recipient.DecryptFile(encryptedData, encryptedPassphrase); File.WriteAllBytes(outputPath, decryptedData); byte[] signedBytes = File.ReadAllBytes(signedEncryptedDir + fileName + ".sig"); decryptionSuccess = 1; try { verificationSuccess = recipient.VerifyData(decryptedData, signedBytes, importedSender.rsaPublicKeyInfo) ? 1 : -1; if (verificationSuccess == -1) { MessageBox.Show("Dosya doğrulanamadı."); } else if (decryptionSuccess == -1) { MessageBox.Show("Dosya deşifrelenemedi."); } } catch { verificationSuccess = -1; MessageBox.Show("Dosya doğrulanamadı."); } } catch { decryptionSuccess = -1; verificationSuccess = -1; } } // Sadece desifreleme if (File.Exists(signedEncryptedDir + fileName + ".enc") && !File.Exists(signedEncryptedDir + fileName + ".sig")) { fileFound = true; try { recipient.rsaPrivateKeyInfo = recipient.GetPrivateKey("Recipient\\", this); encryptedData = File.ReadAllBytes(filepath); encryptedPassphrase = File.ReadAllBytes(signedEncryptedDir + "password"); byte[] decryptedData = recipient.DecryptFile(encryptedData, encryptedPassphrase); File.WriteAllBytes(outputPath, decryptedData); decryptionSuccess = 1; } catch { decryptionSuccess = -1; } verificationSuccess = 0; } // Sadece imza dogrulama if (!File.Exists(signedEncryptedDir + fileName + ".enc") && File.Exists(signedEncryptedDir + fileName + ".sig")) { fileFound = true; try { byte[] bytesToVerify = File.ReadAllBytes(signedEncryptedDir + fileName); byte[] signedBytes = File.ReadAllBytes(filepath); verificationSuccess = recipient.VerifyData(bytesToVerify, signedBytes, importedSender.rsaPublicKeyInfo) ? 1 : -1; if (verificationSuccess == 1) { File.WriteAllBytes(outputPath, bytesToVerify); } } catch { verificationSuccess = -1; } decryptionSuccess = 0; } if (!fileFound) { decryptionSuccess = -1; verificationSuccess = -1; } Form7Results formResults = new Form7Results(); ShowResults(formResults.labelTitle, formResults.labelResult, formResults.labelStrikeout, formResults.panel, formResults.progressBar, formResults.buttonOpenFile); formResults.buttonOpenFile.Visible = true; formResults.ShowDialog(); if (formResults.DialogResult == DialogResult.OK) { try { System.Diagnostics.Process.Start(outputPath); } catch { MessageBox.Show("Dosya açılamıyor."); } } Close(); }
private void buttonSignEncrypt_Click(object senderObject, EventArgs e) { if (textBoxPath.Text.Equals("")) { MessageBox.Show("Bir çıktı dosyası belirtiniz."); return; } outputPath = textBoxPath.Text.Remove(textBoxPath.Text.LastIndexOf('.')); signedEncryptedDirectory = outputPath.Remove(outputPath.LastIndexOf('\\')) + "\\"; if (File.Exists(textBoxPath.Text)) { MessageBox.Show("Bu dizinde aynı isimli dosyalar mevcut. Lütfen başka bir dizin seçiniz."); return; } importedRecipient = currentImportedUsers[comboBoxRecipient.SelectedIndex]; sender = currentUsers[comboBoxSender.SelectedIndex]; // Gonderen ve alici sertifika id'leri, alicinin kendi sertifiklari ve import ettigi sertifiklar arasindan // hangileriyle islem yapilacaginin belirlenmesi icin sifrelenen dosyalarla birlikte gonderiliyor. try { File.WriteAllText(signedEncryptedDirectory + "sender.txt", sender.ID); File.WriteAllText(signedEncryptedDirectory + "recipient.txt", importedRecipient.ID); } catch { MessageBox.Show("Kullanıcı dizini bulunamadı."); } byte[] originalData = null; try { originalData = File.ReadAllBytes(filepath); } catch { MessageBox.Show("Seçilen dosya bulunamadı."); return; } // Veriyi sifrele if (checkBoxEncrypt.Checked && !checkBoxSign.Checked) { try { byte[][] encryptedData = sender.EncryptFile(originalData, importedRecipient.rsaPrivateKeyInfo); File.WriteAllBytes(outputPath + ".enc", encryptedData[0]); File.WriteAllBytes(signedEncryptedDirectory + "password", encryptedData[1]); encryptionSuccess = 1; } catch { encryptionSuccess = -1; } signingSuccess = 0; } // Veriyi imzala if (checkBoxSign.Checked && !checkBoxEncrypt.Checked) { try { sender.rsaPrivateKeyInfo = sender.GetPrivateKey("Sender\\", this); byte[] signedData = sender.SignData(originalData); File.WriteAllBytes(outputPath + ".sig", signedData); File.Copy(filepath, outputPath); signingSuccess = 1; } catch { signingSuccess = -1; } encryptionSuccess = 0; } // Sifrele & imzala if (checkBoxSign.Checked && checkBoxEncrypt.Checked) { try { sender.rsaPrivateKeyInfo = sender.GetPrivateKey("Sender\\", this); byte[] signedData = sender.SignData(originalData); File.WriteAllBytes(outputPath + ".sig", signedData); signingSuccess = 1; } catch { signingSuccess = -1; } try { byte[][] encryptedData = sender.EncryptFile(originalData, importedRecipient.rsaPublicKeyInfo); File.WriteAllBytes(outputPath + ".enc", encryptedData[0]); File.WriteAllBytes(signedEncryptedDirectory + "password", encryptedData[1]); encryptionSuccess = 1; } catch { encryptionSuccess = -1; } } Form7Results formResults = new Form7Results(); ShowResults(formResults.labelTitle, formResults.labelResult, formResults.labelStrikeout, formResults.panel, formResults.progressBar, formResults.buttonOpenFile); formResults.Show(); Close(); }