Exemplo n.º 1
0
        public void Aes_EncryptFile_Test()
        {
            //Arrange
            var tempPath = Path.GetTempPath();

            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
            var path         = $"{tempPath}Security.txt";
            var originalData = Resources.ResourceManager.GetString("Security");

            File.WriteAllText(path, originalData);

            var encryptedPath = $"{tempPath}Security.encrypt";
            var decryptPath   = $"{tempPath}security2.txt";
            var aes           = new AES256();

            //Act
            aes.EncryptFile(EncryptionKey, path, encryptedPath);
            aes.DecryptFile(EncryptionKey, encryptedPath, decryptPath);

            var inputFile  = File.ReadAllText(path);
            var outputFile = File.ReadAllText(decryptPath);

            //Assert
            Assert.AreEqual(inputFile, outputFile);

            File.Delete(path);
            File.Delete(encryptedPath);
            File.Delete(decryptPath);
        }
Exemplo n.º 2
0
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            AES256 aes = new AES256(txtPass.Text);

            new Thread(() =>
            {
                try
                {
                    foreach (var path in Pathes)
                    {
                        string newPath = path.Substring(0, path.Length - MenuService.ENCRYPTED_FILE_EXTANTION.Length - 1);
                        aes.DecryptFile(path, newPath);
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }

                    this.Close();
                }
                catch (Exception exp)
                {
                    string msg;
                    if (exp.Message == "Padding is invalid and cannot be removed.")
                    {
                        msg = "Wrong Password";
                    }
                    else if (exp.Message == "Length of the data to decrypt is invalid.")
                    {
                        msg = "File is not encrypted";
                    }
                    else
                    {
                        msg = exp.Message;
                    }
                    MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }).Start();
        }