public static string DecryptEncryptedFile(EncryptedFileParameters @params) { //======================================================================== // Decrypt encrypted symmetric key with private key. var rsa = EncryptController.ImportPrivateKey(Directory.GetCurrentDirectory() + $"/../../CurrentUsers/{AccountsController.GetInstance().CurrentAccount.Username}.key"); var symmetricKey = Encoding.Unicode.GetString(rsa.Decrypt(Convert.FromBase64String(@params.EncryptedSymmetricKey), false)); //======================================================================== // Decrypt encrypted sendername. var decryptedName = Cipher.Decrypt(@params.EncryptedSenderName, symmetricKey, @params.SymmetricAlgorithm); //======================================================================== // Validate sender and receiver certificate. var senderAccount = AccountsController.GetInstance().Accounts[decryptedName]; var senderCertificate = new X509Certificate2(); senderCertificate.Import(senderAccount.PathToCertificate); var receiverAccount = AccountsController.GetInstance().CurrentAccount; var receiverCertificate = new X509Certificate2(); receiverCertificate.Import(receiverAccount.PathToCertificate); if (!ValidateController.ValidateCertificates(receiverCertificate)) { return(""); } if (!ValidateController.ValidateCertificates(senderCertificate)) { return(""); } //======================================================================== // Decrypt digital signature with symmetric key. var digitalSignature = Convert.FromBase64String( Cipher.Decrypt(@params.EncryptedSignature, symmetricKey, @params.SymmetricAlgorithm)); //======================================================================== // Decrypt data with symmetric key. var decryptedData = Cipher.Decrypt(@params.EncryptedData, symmetricKey, @params.SymmetricAlgorithm); //======================================================================== // Verify messagehash with signature. var decryptedHashAlg = Cipher.Decrypt(@params.EncryptedHashAlg, symmetricKey, @params.SymmetricAlgorithm); var rsaCrypto = (RSACryptoServiceProvider)senderCertificate.PublicKey.Key; if (!VerifyData(Encoding.Unicode.GetBytes(decryptedData), digitalSignature, rsaCrypto.ExportParameters(false), decryptedHashAlg)) { return(null); } //======================================================================== // Return a message. return(decryptedData); }
public static Account ReadSessionRequest(Account receiver, bool isResponse, object locker) { var file = Directory.GetFiles(Directory.GetCurrentDirectory() + @"\..\..\ChatRequests", isResponse ? "*.sesres" : "*.sesreq") .FirstOrDefault(e => Path.GetFileNameWithoutExtension(e) == receiver.Username); if (file == null) { return(null); } string content; lock (locker) { while (true) { try { content = Steganography.ExtractText( new Bitmap(Image.FromFile(file)) ); break; } catch (Exception) { Task.Delay(1000); } } } //======================================================================== // Decrypt encrypted sender username with private key. var rsa = EncryptController.ImportPrivateKey(Directory.GetCurrentDirectory() + $@"\..\..\CurrentUsers\{AccountsController.GetInstance().CurrentAccount.Username}.key"); var senderUsername = Encoding.Unicode.GetString(rsa.Decrypt(Convert.FromBase64String(content), false)); //======================================================================== // Validate sender and receiver certificate. var senderAccount = AccountsController.GetInstance().CurrentAccount; var senderCertificate = new X509Certificate2(); senderCertificate.Import(senderAccount.PathToCertificate); var receiverCertificate = new X509Certificate2(); receiverCertificate.Import(receiver.PathToCertificate); if (!ValidateController.ValidateCertificates(receiverCertificate)) { return(null); } if (!ValidateController.ValidateCertificates(senderCertificate)) { return(null); } return(AccountsController.GetInstance().Accounts[senderUsername]); }
public static string DecryptEncryptedFile(EncryptedFileParameters @params) { //======================================================================== // Decrypt encrypted symmetric key with private key. var rsa = EncryptController.ImportPrivateKey(Directory.GetCurrentDirectory() + "/../../CurrentUser/private.key"); var symmetricKey = Encoding.Unicode.GetString(rsa.Decrypt(Convert.FromBase64String(@params.EncryptedSymmetricKey), false)); //======================================================================== // Decrypt encrypted sender and filename. var decryptedName = Cipher.Decrypt(@params.EncryptedName, symmetricKey, @params.SymmetricAlgorithm); //======================================================================== // Validate sender and receiver certificate. var senderAccount = AccountsController.GetInstance().Accounts[decryptedName]; var senderCertificate = new X509Certificate2(); senderCertificate.Import(senderAccount.PathToCertificate); var receiverAccount = AccountsController.GetInstance().CurrentAccount; var receiverCertificate = new X509Certificate2(); receiverCertificate.Import(receiverAccount.PathToCertificate); if (!ValidateController.ValidateCertificates(receiverCertificate)) { return(""); } if (!ValidateController.ValidateCertificates(senderCertificate)) { return(""); } //======================================================================== // Decrypt digital signature with symmetric key. var digitalSignature = Convert.FromBase64String( Cipher.Decrypt(@params.EncryptedSignature, symmetricKey, @params.SymmetricAlgorithm)); //======================================================================== // Decrypt file with symmetric key. var decryptedFileName = Cipher.Decrypt(@params.EncryptedFileName, symmetricKey, @params.SymmetricAlgorithm); var decryptedFile = Encoding.Unicode.GetBytes( Cipher.Decrypt(@params.EncryptedData, symmetricKey, @params.SymmetricAlgorithm)); //======================================================================== // Verify filehash with signature. var decryptedHashAlg = Cipher.Decrypt(@params.EncryptedHashAlg, symmetricKey, @params.SymmetricAlgorithm); var rsaCrypto = (RSACryptoServiceProvider)senderCertificate.PublicKey.Key; if (!VerifyData(decryptedFile, digitalSignature, rsaCrypto.ExportParameters(false), decryptedHashAlg)) { MessageBox.Show("File is changed! Unsuccessfully decrypt. 😐"); return(""); } //======================================================================== // Write file on filesystem. var parentPath = Directory.GetCurrentDirectory() + "/../../DecryptedMessages"; var currentUsername = AccountsController.GetInstance().CurrentAccount.Username; var path = $"{parentPath}/{currentUsername}/{decryptedName}/{Path.GetFileName(decryptedFileName)}"; if (!Directory.Exists($"{parentPath}/{currentUsername}/{decryptedName}/")) { Directory.CreateDirectory($"{parentPath}/{currentUsername}/{decryptedName}/"); } File.WriteAllBytes(path, decryptedFile); return(path); }