public void CryptoBlob_BasicUsage()
        {
            try
            {
                byte[] bytes = new ByteGenerator().GenerateBytes(10);

                // Create Credentials
                CryptoCredentials credentials = new CryptoCredentials
                {
                    Passphrase = new CryptoString("My Passphrase"),
                    Pin        = 2222
                };

                // Create the Blob object and assign Encrypt some Bytes
                CryptoBlob blob = new CryptoBlob(credentials, bytes);

                // Retrieve the Decrypted Bytes
                byte[] decryptedBytes = blob.Decrypt();

                // Get the Encrypted Data - Perhaps you want to store it in a Database for example
                byte[] encryptedBytes = blob.GetEncryptedBytes();

                // Set the Encrypted Bytes - Perhaps you pulled them from a Database or in another file
                blob.SetEncryptedBytes(encryptedBytes);

                // Validates the Checksum of the blob and throws an exception if the Blob fails the integrity check
                blob.ValidateChecksum();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw;
            }
        }
 public SymmetricEncryptor(CryptoCredentials credentials,
                           SymmetricAlgorithm algorithm      = null,
                           SymmetricEncryptorOptions options = null,
                           HashAlgorithm hashAlgorithm       = null) : this(algorithm, options, hashAlgorithm)
 {
     Credentials = credentials;
 }
        public void CryptoDataFile_EncryptedEntireFileInvalidCredentialTest()
        {
            CryptoCredentials credentials = new CryptoCredentials
            {
                Passphrase = new CryptoString("My Passphrase"),
                Pin        = 2222
            };
            CryptoCredentials credentialsWrongPassphrase = new CryptoCredentials
            {
                Passphrase = new CryptoString("My Wrong Passphrase"),
                Pin        = 2222
            };
            CryptoCredentials credentialsWrongPin = new CryptoCredentials
            {
                Passphrase = new CryptoString("My Passphrase"),
                Pin        = 1234
            };

            string dataFileName = "EncryptedFile.bin";
            string fileContent  = "My Test Content";

            byte[] fileContentBytes = Encoding.UTF8.GetBytes(fileContent);


            using (AutoDeleteFiles autoDeleteFile = new AutoDeleteFiles(dataFileName))
            {
                // Save some data
                CryptoDataFile dataFile = new CryptoDataFile(dataFileName);
                dataFile.Credentials = credentials;
                dataFile.EncryptFile = true;
                dataFile.Content     = fileContentBytes;
                dataFile.Save();

                try
                {
                    // TEST WRONG PASSPHRASE
                    CryptoDataFile dataFile2 = new CryptoDataFile(dataFileName);
                    dataFile2.Credentials = credentialsWrongPassphrase;
                    dataFile2.EncryptFile = true;
                    dataFile2.Load();
                }
                catch (CryptoDecryptionException exception)
                {
                    Assert.IsTrue(exception is CryptoDecryptionException);
                }
                catch (CryptoFileInvalidFormatException)
                {
                    Assert.Fail("Test was not expecting an Exception");
                }
                catch (Exception)
                {
                    Assert.Fail("Test was not expecting an Exception");
                }



                //Assert.IsTrue(dataFile.Content.SequenceEqual(dataFile2.Content));
            }
        }
        public void FileArchiver_SimpleAddExtractEntry()
        {
            // Arrange
            CryptoCredentials credentials = new CryptoCredentials
            {
                Passphrase = new CryptoString("My Passphrase"),
                Pin        = 2222
            };

            FileManager fileMan  = new FileManager();
            Hasher      hasher   = new Hasher();
            int         fileSize = 2000;

            // Define Some Directories & Files
            string directoryName1     = "Test1";
            string extractDirectory   = "TestExtract";
            string zipArchiveFileName = "ZipArchiveTest.zip";



            List <DirectoryInfo> directories = new List <DirectoryInfo>
            {
                new DirectoryInfo(directoryName1),
                new DirectoryInfo(extractDirectory)
            };
            List <FileInfo> files = new List <FileInfo>
            {
                new FileInfo(Path.Combine(directoryName1, "file1.bin")),
                new FileInfo(zipArchiveFileName),
            };

            // Make sure any existing are deleted
            fileMan.DeleteAllFilesAndDirectories(files, directories, true);

            // Save & Extract Archive To/From a File
            using (CreateAutoDeleteDirectory d = new CreateAutoDeleteDirectory(directories))
            {
                using (CreateAutoDeleteFiles f = new CreateAutoDeleteFiles(files, true, fileSize))
                {
                    // Remove the Archive File since you will create it below
                    fileMan.DeleteFile(zipArchiveFileName);

                    // Create the Archiver, Add Directory and Save (Zip/Archive)
                    CryptoArchiver archiver = new CryptoArchiver(zipArchiveFileName, new ZipArchiver(zipArchiveFileName));
                    archiver.Credentials = credentials;
                    archiver.CreateFromDirectory(directoryName1);

                    // Extract all files
                    archiver.ExtractToDirectory(extractDirectory);

                    // Compare the two directories htey should be the same
                    string dir1 = directoryName1;
                    string dir2 = Path.Combine(extractDirectory, directoryName1);
                    Assert.IsTrue(hasher.CompareDirectoryHashSignatures(dir1, dir2));
                }
            }
        }
        public void CryptoCredential_BasicUsage()
        {
            CryptoCredentials credentials = new CryptoCredentials
            {
                Passphrase = new CryptoString("My Passphrase"),
                Pin        = 2222
            };


            // Check what  credentials are used
            bool passphraseUsed = credentials.UsePassphrase;
            bool pinUsed        = credentials.UsePin;

            // Generate Key to be used for your encryption
            string key = credentials.Key;
        }
        /// <summary>
        /// Loads the File from the FileSystem based on the FileName property
        /// </summary>
        public virtual void Load()
        {
            byte[] bytes;

            if (EncryptFile)
            {
                CryptoCredentials credentials    = GetCredentials();
                byte[]            encryptedBytes = File.ReadAllBytes(FullFileName);
                CryptoBlob        blob           = new CryptoBlob(credentials);
                blob.SetEncryptedBytes(encryptedBytes);
                bytes = blob.Decrypt(true);
            }
            else
            {
                bytes = File.ReadAllBytes(FullFileName);
            }
            LoadFromBytes(bytes);
        }
        public void CryptoBlob_StressTest()
        {
            int iterations = 200;
            int blocksize  = 100;

            byte[] bytes = new ByteGenerator().GenerateBytes(blocksize);


            for (int i = 0; i < iterations; i++)
            {
                try
                {
                    // Create Credentials
                    CryptoCredentials credentials = new CryptoCredentials
                    {
                        Passphrase = new CryptoString("My Passphrase"),
                        Pin        = 2222
                    };

                    // Create the Blob object and assign Encrypt some Bytes
                    CryptoBlob blob = new CryptoBlob(credentials, bytes);

                    // Retrieve the Decrypted Bytes
                    byte[] decryptedBytes = blob.Decrypt();

                    // Get the Encrypted Data - Perhaps you want to store it in a Database for example
                    byte[] encryptedBytes = blob.GetEncryptedBytes();

                    // Set the Encrypted Bytes - Perhaps you pulled them from a Database or in another file
                    blob.SetEncryptedBytes(encryptedBytes);

                    // Validates the Checksum of the blob and throws an exception if the Blob fails the integrity check
                    blob.ValidateChecksum();

                    Assert.IsTrue(decryptedBytes.SequenceEqual(bytes));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                    throw;
                }
            }
        }
        public void CryptoCredential_ConfirmPropertiesSet()
        {
            // Create Credentials
            CryptoString      passphrase  = new CryptoString("My Passphrase");
            int               pin         = 1234;
            CryptoCredentials credentials = new CryptoCredentials();

            // Assign Credentials and ensure the 'Use' properties get updated
            Assert.IsFalse(credentials.UsePassphrase);
            credentials.Passphrase = passphrase;
            Assert.IsTrue(credentials.UsePassphrase);

            Assert.IsFalse(credentials.UsePin);
            credentials.Pin = pin;
            Assert.IsTrue(credentials.UsePin);

            string key = credentials.Key;

            Assert.IsTrue(Hasher.IsHashValid(key));
        }
        public void CryptoCredential_AdvancedUsage()
        {
            CryptoCredentials credentials = new CryptoCredentials
            {
                Passphrase = new CryptoString("My Passphrase"),
                Pin        = 2222
            }.MakeReadOnly();


            try { credentials.Passphrase = new CryptoString("test"); } catch (Exception e) { Assert.IsTrue(e is CryptoCredentialsException); }
            try { credentials.Pin = 5555; } catch (Exception e) { Assert.IsTrue(e is CryptoCredentialsException); }


            // Check what  credentials are used
            bool passphraseUsed = credentials.UsePassphrase;
            bool pinUsed        = credentials.UsePin;

            // Generate Key to be used for your encryption
            string key = credentials.Key;
        }
        public void CryptoBlob_AdvancedUsage()
        {
            try
            {
                //byte[] bytes = new ByteGenerator().GenerateBytes(10);
                byte[] bytes = new byte[] { 0x00, 0x00 };

                // Create Credentials
                CryptoCredentials credentials = new CryptoCredentials
                {
                    Passphrase = new CryptoString("My Passphrase"),
                    Pin        = 2222
                };

                CryptoBlob blob           = new CryptoBlob(credentials, bytes);
                byte[]     encryptedBytes = blob.GetEncryptedBytes();
                blob.SetEncryptedBytes(encryptedBytes);
                byte[] decryptedBytes = blob.Decrypt(true);


                CryptoBlob blob2           = new CryptoBlob(credentials, decryptedBytes);
                byte[]     encryptedBytes2 = blob2.GetEncryptedBytes();
                byte[]     decryptedBytes2 = blob.Decrypt(true);

                bool diff = encryptedBytes.SequenceEqual(encryptedBytes2);


                //byte[] decryptedBytes = blob.Decrypt();
                //byte[] encryptedBytes = blob.GetEncryptedBytes();
                //blob.SetEncryptedBytes(encryptedBytes);

                // Validates the Checksum of the blob and throws an exception if the Blob fails the integrity check
                blob.ValidateChecksum();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw;
            }
        }
        public void CryptoDataFile_EncryptedEntireFileTest()
        {
            string       passphraseString = "My Passphrase";
            CryptoString passphrase       = new CryptoString(CryptoString.StringToSecureString(passphraseString));

            CryptoCredentials credentials = new CryptoCredentials
            {
                Passphrase = passphrase,
            };

            string dataFileName = "EncryptedFile.bin";
            string fileContent  = "My Test Content";


            using (AutoDeleteFiles autoDeleteFile = new AutoDeleteFiles(dataFileName))
            {
                // Save some data
                CryptoDataFile dataFile = new CryptoDataFile(dataFileName);
                byte[]         content1 = Encoding.UTF8.GetBytes(fileContent);
                dataFile.Credentials = credentials;
                dataFile.EncryptFile = true;
                dataFile.Content     = content1;
                dataFile.Save();
                byte[] fileBytes1 = dataFile.SaveToBytes();

                // Now load the data and compare
                CryptoDataFile dataLoader = new CryptoDataFile(dataFileName);
                dataLoader.Credentials = credentials;
                dataLoader.EncryptFile = true;
                dataLoader.Load();
                dataLoader.LoadFromBytes(fileBytes1);
                byte[] content2 = dataLoader.Content;


                Assert.IsTrue(content1.SequenceEqual(content2));
                Assert.IsTrue(dataFile.Content.SequenceEqual(dataLoader.Content));
            }
        }
        public void CryptoDataFile_VersionNumbersTest()
        {
            FileManager fileOps = new FileManager();
            Hasher      hasher  = new Hasher();

            // Create Credentials
            CryptoCredentials credentials = new CryptoCredentials {
                Passphrase = new CryptoString("My Passphrase")
            };

            CryptoDataFile file1         = null;
            CryptoDataFile file2         = null;
            string         dataFileName1 = "VersionDataFileTest.dat";

            byte[] testBytes = new byte[] { 0x11, 0x22 };

            fileOps.DeleteFile(dataFileName1);

            using (AutoDeleteFiles deleteFiles = new AutoDeleteFiles(dataFileName1))
            {
                ////////////////////////////////////////////////////////////
                // Write
                ////////////////////////////////////////////////////////////
                try
                {
                    // Write Content to data file
                    file1 = new CryptoDataFile(dataFileName1);

                    file1.EncryptFile = true;
                    file1.Credentials = credentials;
                    file1.Content     = testBytes;
                    file1.Save();
                }
                catch (Exception ex)
                {
                    Assert.Fail(ex.Message);
                }

                ////////////////////////////////////////////////////////////
                // Read / Load
                ////////////////////////////////////////////////////////////
                try
                {
                    // Read & Load File
                    file2             = new CryptoDataFile(dataFileName1);
                    file2.Credentials = credentials;
                    file2.EncryptFile = true;
                    file2.Load();

                    // Compare the files they should be exactly the same
                    Assert.IsTrue(file1.Content.SequenceEqual(file2.Content));
                }
                catch (Exception ex)
                {
                    // Delete Files
                    Assert.Fail(ex.Message);
                }
                finally
                {
                }
            }
        }
        public void CryptoDataFile_BasicTest_EncryptedFile()
        {
            FileManager fileOps = new FileManager();
            Hasher      hasher  = new Hasher();

            // Create Credentials
            CryptoCredentials credentials = new CryptoCredentials {
                Passphrase = new CryptoString("My Passphrase")
            };
            CryptoCredentials wrongCredentials = new CryptoCredentials {
                Passphrase = new CryptoString("My WRONG Passphrase")
            };

            CryptoDataFile file1         = null;
            CryptoDataFile file2         = null;
            string         dataFileName1 = "EncryptedTestDataFile1.dat";
            string         dataFileName2 = "EncryptedTestDataFile2.dat";

            //byte[] testBytes = new ByteGenerator().GenerateBytes(1000);
            byte[]          testBytes = new byte[] { 0x11, 0x22 };
            List <FileInfo> fileList  = new List <FileInfo> {
                new FileInfo(dataFileName1), new FileInfo(dataFileName2)
            };

            fileOps.DeleteFile(dataFileName1);

            using (AutoDeleteFiles deleteFiles = new AutoDeleteFiles(fileList))
            {
                ////////////////////////////////////////////////////////////
                // Write
                ////////////////////////////////////////////////////////////
                try
                {
                    // Write Content to data file
                    file1             = new CryptoDataFile(dataFileName1);
                    file1.EncryptFile = true;                     // This is the default
                    file1.Credentials = credentials;
                    file1.Content     = testBytes;
                    file1.Save();
                }
                catch (Exception ex)
                {
                    Assert.Fail(ex.Message);
                }

                ////////////////////////////////////////////////////////////
                // Read / Load
                ////////////////////////////////////////////////////////////
                try
                {
                    // Read & Load File
                    file2             = new CryptoDataFile(dataFileName1);
                    file2.Credentials = credentials;
                    file2.EncryptFile = true;
                    file2.Load();

                    // Compare the files they should be exactly the same
                    Assert.IsTrue(file1.Content.SequenceEqual(file2.Content));
                }
                catch (Exception ex)
                {
                    // Delete Files
                    Assert.Fail(ex.Message);
                }
                finally
                {
                }

                // THIS NEEDS REWORKING!
                ////////////////////////////////////////////////////////////
                // Read / Load Wrong Password
                ////////////////////////////////////////////////////////////
                try
                {
                    // Read & Load File
                    file1             = new CryptoDataFile(dataFileName1);
                    file1.Credentials = wrongCredentials;
                    file1.EncryptFile = true;
                    file1.Load();

                    Assert.Fail("Load should fail with wrong password");
                }
                catch (Exception ex)
                {
                    Assert.IsTrue(true, ex.Message);
                }
            }
        }