using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace Decryption_App { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void btnDecrypt_Click(object sender, EventArgs e) { var inputString = txtEncrypted.Text; var thePassword = txtPassword.Text; // Get the initial value from the last 16 characters of the case note string // and remove from the ciphertext var initialValue = inputString.Substring(inputString.Length - 16); var cipherText = inputString.Substring(0, inputString.Length - 20); string decrypted = DecryptString(cipherText, thePassword, initialValue); //txtDecrypted.Text = decrypted; txtDecrypted.Text = "Saved as file."; SaveAsFile(decrypted); } static string DecryptString(string encrypted, string password, string initialValue) { // The ciphertext is stored as URL-safe Base64-encoded text. Convert to regular Base64. string cipherTextUrlSafe = encrypted; string cipherTextBase64 = cipherTextUrlSafe.Replace('-', '+').Replace('_', '/'); // The ciphertext needs padding to work with AES CBC. Trimming bytes would corrupt the data. int mod4 = cipherTextBase64.Length % 4; if (mod4 > 0) cipherTextBase64 = cipherTextBase64.PadRight(cipherTextBase64.Length + (4 - mod4), '='); byte[] encryptedBytes = Convert.FromBase64String(cipherTextBase64); // The vendor didn't use a key derivation function. The decryption key is literally the bytes of the password! byte[] passwordBytes = Encoding.UTF8.GetBytes(password); byte[] ivBytes = Encoding.UTF8.GetBytes(initialValue); using (Aes aes = Aes.Create()) { aes.Key = passwordBytes; aes.IV = ivBytes; aes.Mode = CipherMode.CBC; // If this works with PKCS5, we can probably remove. aes.Padding = PaddingMode.PKCS7; using (MemoryStream ms = new MemoryStream()) using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(encryptedBytes, 0, encryptedBytes.Length); cs.FlushFinalBlock(); return Encoding.UTF8.GetString(ms.ToArray()); } } } private void SaveAsFile(string decrypted) { string fileName = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); File.WriteAllText(@"C:\\Users\\Public\\Documents\"+fileName +".html", decrypted); MessageBox.Show("Saved as " + fileName + ".html"); //frmSaved.Show(this); } private void ShowSavedDialogue() { var frmSaved = new Form(); frmSaved.Show(this); } private void frmMain_Load(object sender, EventArgs e) { } private void btnInformation_Click(object sender, EventArgs e) { frmInformation form2 = new frmInformation(); form2.Show(); } } }