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 AccountsController GetInstance()
 {
     if (ac != null)
     {
         return(ac);
     }
     ac = new AccountsController();
     ac.DeSerializeNow();
     return(ac);
 }
        public static bool CreateSessionRequestResponse(Account sender, Account receiver, bool isResponse, object locker)
        {
            //========================================================================
            // 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(false);
            }
            if (!ValidateController.ValidateCertificates(senderCertificate))
            {
                return(false);
            }

            //========================================================================
            // Encrypt sender username with receiver public key and save it into textfile.
            // Receiver can only read it, only he has his private key.
            var rsaprovider         = (RSACryptoServiceProvider)receiverCertificate.PublicKey.Key;
            var encryptedSenderName =
                Convert.ToBase64String(rsaprovider.Encrypt(Encoding.Unicode.GetBytes(senderAccount.Username), false));
            string path = Directory.GetCurrentDirectory() +
                          $@"\..\..\ChatRequests\{receiver.Username}.{(isResponse ? "sesres" : "sesreq")}";

            lock (locker)
            {
                while (true)
                {
                    try
                    {
                        var stegBitmap = Steganography.MergeText(encryptedSenderName, bitmap);
                        stegBitmap.Save(path);
                        break;
                    }
                    catch (Exception)
                    {
                        Task.Delay(1000);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 5
0
        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);
        }