Ejemplo n.º 1
0
        public void Test_DecryptString_with_encryption_data_appended_with_associated_data()
        {
            string associatedData       = "0f8fad5b-d9cb-469f-a165-70867728950e";
            var    appendEncryptionData = true;
            var    aesDecryptionResult  = new AesDecryptionResult();
            string errorMessage         = "";

            var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);

                if (!aesDecryptionResult.Success)
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
        }
Ejemplo n.º 2
0
        public void Test_DecryptString_without_encryption_data_appended_with_associated_data()
        {
            string associatedData       = "0f8fad5b-d9cb-469f-a165-70867728950e";
            var    appendEncryptionData = false;
            var    aesDecryptionResult  = new AesDecryptionResult();
            string errorMessage         = "";

            var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), System.Text.Encoding.UTF8.GetBytes(associatedData), hasEncryptionDataAppendedInInput: appendEncryptionData,
                                                               aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);

                if (!aesDecryptionResult.Success)
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
        }
        public void Test_DecryptFile_without_append_encryption_data()
        {
            var    testFilePath              = Path.GetTempFileName();
            var    appendEncryptionData      = false;
            var    aesDecryptionResult       = new AesDecryptionResult();
            var    testFileStringContentRead = "";
            string errorMessage              = "";

            File.WriteAllText(testFilePath, _testString);

            var aesEncryptionResult = _aes192cbcHmacSha384.EncryptFile(testFilePath, testFilePath, _password, false, appendEncryptionDataToOutputFile: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes192cbcHmacSha384.DecryptFile(testFilePath, testFilePath, Encoding.UTF8.GetBytes(_password), false, hasEncryptionDataAppendedInInputFile: appendEncryptionData,
                                                                       aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.IV);

                if (aesDecryptionResult.Success)
                {
                    testFileStringContentRead = File.ReadAllText(testFilePath);
                }
                else
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && testFileStringContentRead.Equals(_testString)), errorMessage);
        }
        public void Test_DecryptString_without_encryption_data_appended()
        {
            var    appendEncryptionData = false;
            var    aesDecryptionResult  = new AesDecryptionResult();
            string errorMessage         = "";

            var aesEncryptionResult = _aes192cbcHmacSha384.EncryptString(_testString, _password, appendEncryptionDataToOutputString: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes192cbcHmacSha384.DecryptString(aesEncryptionResult.EncryptedDataBytes, Encoding.UTF8.GetBytes(_password),
                                                                         hasEncryptionDataAppendedInInputString: appendEncryptionData, aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.IV);

                if (!aesDecryptionResult.Success)
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
        }
        public void Test_DecryptFile_with_append_encryption_data()
        {
            var testFilePath              = Path.GetTempFileName();
            var appendEncryptionData      = true;
            var aesDecryptionResult       = new AesDecryptionResult();
            var testFileStringContentRead = "";
            var errorMessage              = "";

            File.WriteAllText(testFilePath, _testString);

            var aesEncryptionResult = _aes128cbcHmacSha256.EncryptFile(testFilePath, testFilePath, _password, false, appendEncryptionDataToOutputFile: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes128cbcHmacSha256.DecryptFile(testFilePath, testFilePath, _password, false, hasEncryptionDataAppendedInInputFile: appendEncryptionData);

                if (aesDecryptionResult.Success)
                {
                    testFileStringContentRead = File.ReadAllText(testFilePath);
                }
                else
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && testFileStringContentRead.Equals(_testString)), errorMessage);
        }
Ejemplo n.º 6
0
        public void Test_DecryptString_with_encryption_data_appended_without_associated_data()
        {
            string associatedData       = null;
            var    appendEncryptionData = true;
            var    aesDecryptionResult  = new AesDecryptionResult();
            var    errorMessage         = "";

            var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes128gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);

                if (!aesDecryptionResult.Success)
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
        }
Ejemplo n.º 7
0
        public void Test_DecryptString_with_encryption_data_appended()
        {
            var    appendEncryptionData = true;
            var    aesDecryptionResult  = new AesDecryptionResult();
            string errorMessage         = "";

            var aesEncryptionResult = _aes256cbcHmacSha384.EncryptString(_testString, _password, appendEncryptionDataToOutput: appendEncryptionData);

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes256cbcHmacSha384.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, hasEncryptionDataAppendedInInput: appendEncryptionData);

                if (!aesDecryptionResult.Success)
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
        }
Ejemplo n.º 8
0
        public void Test_Encrypt_Decrypt_String_without_password()
        {
            var    aesDecryptionResult = new AesDecryptionResult();
            string errorMessage        = "";

            var aesEncryptionResult = _aes256cbcHmacSha384.EncryptString(System.Text.Encoding.UTF8.GetBytes(_testString));

            if (aesEncryptionResult.Success)
            {
                aesDecryptionResult = _aes256cbcHmacSha384.DecryptString(aesEncryptionResult.EncryptedDataBytes, aesEncryptionResult.Key, aesEncryptionResult.IV,
                                                                         aesEncryptionResult.AuthenticationKey, aesEncryptionResult.Tag);

                if (!aesDecryptionResult.Success)
                {
                    errorMessage = aesDecryptionResult.Message;
                }
            }
            else
            {
                errorMessage = aesEncryptionResult.Message;
            }

            Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
        }
Ejemplo n.º 9
0
        private static ExitCode RunDecryptOptionsAndReturnExitCode(DecryptOptions decryptOptions)
        {
            AesDecryptionResult aesDecryptionResult = null;

            switch (decryptOptions.InputType.ToLower())
            {
            case "string":
            {
                aesDecryptionResult = (decryptOptions.Algorithm.ToLower()) switch
                {
                    "aes128cbc" => new AE_AES_128_CBC_HMAC_SHA_256().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password),
                    "aes192cbc" => new AE_AES_192_CBC_HMAC_SHA_384().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password),
                    "aes256cbc" => new AE_AES_256_CBC_HMAC_SHA_512().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password),
                    "aes128gcm" => new AEAD_AES_128_GCM().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password, decryptOptions.AssociatedData),
                    "aes192gcm" => new AEAD_AES_192_GCM().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password, decryptOptions.AssociatedData),
                    "aes256gcm" => new AEAD_AES_256_GCM().DecryptString(decryptOptions.InputToBeDecrypted, decryptOptions.Password, decryptOptions.AssociatedData),
                    _ => new AesDecryptionResult()
                    {
                        Success = false, Message = $"Unknown algorithm \"{decryptOptions.Algorithm}\"."
                    },
                };
            }
            break;

            case "file":
            {
                switch (decryptOptions.Algorithm.ToLower())
                {
                case "aes128cbc":
                {
                    using (var progressBar = new ProgressBar())
                    {
                        var aes128 = new AE_AES_128_CBC_HMAC_SHA_256();
                        aes128.OnDecryptionProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); };
                        aes128.OnDecryptionMessage  += (msg) => { progressBar.WriteLine(msg); };
                        aesDecryptionResult          = aes128.DecryptFile(decryptOptions.InputToBeDecrypted, decryptOptions.OutputFilePath, decryptOptions.Password, decryptOptions.DeleteEncryptedFile);
                    }
                }
                break;

                case "aes192cbc":
                {
                    using (var progressBar = new ProgressBar())
                    {
                        var aes192 = new AE_AES_192_CBC_HMAC_SHA_384();
                        aes192.OnDecryptionProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); };
                        aes192.OnDecryptionMessage  += (msg) => { progressBar.WriteLine(msg); };
                        aesDecryptionResult          = aes192.DecryptFile(decryptOptions.InputToBeDecrypted, decryptOptions.OutputFilePath, decryptOptions.Password, decryptOptions.DeleteEncryptedFile);
                    }
                }
                break;

                case "aes256cbc":
                {
                    using (var progressBar = new ProgressBar())
                    {
                        var aes256 = new AE_AES_256_CBC_HMAC_SHA_512();
                        aes256.OnDecryptionProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); };
                        aes256.OnDecryptionMessage  += (msg) => { progressBar.WriteLine(msg); };
                        aesDecryptionResult          = aes256.DecryptFile(decryptOptions.InputToBeDecrypted, decryptOptions.OutputFilePath, decryptOptions.Password, decryptOptions.DeleteEncryptedFile);
                    }
                }
                break;

                case "aes128gcm":
                case "aes192gcm":
                case "aes256gcm":
                    aesDecryptionResult = new AesDecryptionResult()
                    {
                        Success = false, Message = $"Algorithm \"{decryptOptions.Algorithm}\" currently not available for file decryption."
                    };
                    break;

                default:
                    aesDecryptionResult = new AesDecryptionResult()
                    {
                        Success = false, Message = $"Unknown algorithm \"{decryptOptions.Algorithm}\"."
                    };
                    break;
                }
            }
            break;

            default:
                aesDecryptionResult = new AesDecryptionResult()
                {
                    Success = false, Message = $"Unknown input type \"{decryptOptions.InputType}\"."
                };
                break;
            }

            if (aesDecryptionResult.Success)
            {
                Console.WriteLine((decryptOptions.InputType.ToLower().Equals("string") ? aesDecryptionResult.DecryptedDataString : aesDecryptionResult.Message));

                return(ExitCode.Sucess);
            }
            else
            {
                Console.WriteLine(aesDecryptionResult.Message);

                return(ExitCode.Error);
            }
        }