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 EncryptedFileParameters EncryptedFileParametersParser(string path, object locker) { var ext = Path.GetExtension(path); ext = ext?.Substring(1); SymmetricAlgorithm symmetricAlgorithm = EncryptController.CheckSymmetricAlgorithm(ext); string content; while (true) { try { FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); content = File.ReadAllText(path); fileStream.Close(); break; } catch (Exception) { Thread.Sleep(200); } } var match = Regex.Match(content, "ENCRYPTED SYMMETRIC KEY:\n(.*?)\n\n" + "ENCRYPTED HASH ALGORITHM:\n(.*?)\n\n" + "ENCRYPTED DATA SENDER:\n(.*?)\n\n" + "ENCRYPTED DIGITAL SIGNATURE:\n(.*?)\n\n" + "ENCRYPTED DATA:\n(.*?)\n\n"); var @params = new EncryptedFileParameters { EncryptedSymmetricKey = match.Groups[1].Value, EncryptedHashAlg = match.Groups[2].Value, EncryptedSenderName = match.Groups[3].Value, EncryptedSignature = match.Groups[4].Value, EncryptedData = match.Groups[5].Value, SymmetricAlgorithm = symmetricAlgorithm }; return(@params); }
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); }