Пример #1
0
        public void WriteReadCustomClassTest()
        {
            using (TestUtils.TempFile tmp = new TestUtils.TempFile())
            {
                //create some data to store
                string   expected = "expected";
                TestData testData = new TestData()
                {
                    someData = expected
                };

                //configure encryption
                String password = "******";
                var    alg      = StandardAlgorithms.SymmetricAlgorithmFromPassword(password);

                //perform the encryption
                CryptoUtils.SerializeToFile(testData, tmp.FilePath, alg);

                //decrypt the data
                TestData decrypted = CryptoUtils.DeserializeFromFile <TestData>(tmp.FilePath, alg);

                //check that it's ok
                Assert.AreEqual(expected, decrypted.someData);
            }
        }
Пример #2
0
        public static void Serialize(UserAccount account, string password, string FileName)
        {
            SymmetricAlgorithm alg = StandardAlgorithms.SymmetricAlgorithmFromPassword(password);

            // Encrypt text to a file using the file name, key, and IV.
            CryptoUtils.SerializeToFile(account, FileName, alg);
        }
Пример #3
0
        public void StreamSerializationTest()
        {
            using (TestUtils.TempFile tmp = new TestUtils.TempFile())
            {
                //Test data
                String expected  = "expected";
                String decrypted = null;
                //configure encryption
                String password = "******";
                var    alg      = StandardAlgorithms.SymmetricAlgorithmFromPassword(password);

                //We create a simple output stream against a temp file, but this could be sockets or anything.
                using (FileStream stream = File.Open(tmp.FilePath, FileMode.CreateNew))
                {
                    //Perform encryption
                    CryptoUtils.SerializeToStream(stream, expected, alg);
                }

                //We create a simple input stream against the temp file, but this could be sockets or anything.
                using (FileStream stream = File.Open(tmp.FilePath, FileMode.Open))
                {
                    //Perform decryption
                    decrypted = CryptoUtils.DeserializeFromStream <String>(stream, alg);
                    stream.Close();
                }
                //perform decryption

                //Check that it's ok.
                Assert.AreEqual(expected, decrypted);
            }
        }
Пример #4
0
        public static UserAccount Deserialize(string password, string FileName)
        {
            SymmetricAlgorithm alg = StandardAlgorithms.SymmetricAlgorithmFromPassword(password);

            // Decrypt the text from a file using the file name, key, and IV.
            return(CryptoUtils.DeserializeFromFile <UserAccount>(FileName, alg));
        }
Пример #5
0
        public void WriteReadSomeStringTest()
        {
            using (TestUtils.TempFile tmp = new TestUtils.TempFile())
            {
                //Test data
                String expected = "expected";

                //configure encryption
                String password = "******";
                var    alg      = StandardAlgorithms.SymmetricAlgorithmFromPassword(password);

                //Perform encryption
                CryptoUtils.SerializeToFile(expected, tmp.FilePath, alg);

                //perform decryption
                String decrypted = CryptoUtils.DeserializeFromFile <String>(tmp.FilePath, alg);

                //Check that it's ok.
                Assert.AreEqual(expected, decrypted);
            }
        }