Exemple #1
0
        public void SecureFile_Stream_Validate()
        {
            string privateKey              = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
            string publicKey               = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);
            EnhancedMemoryStream original  = new EnhancedMemoryStream();
            EnhancedMemoryStream encrypted = new EnhancedMemoryStream();
            SecureFile           secure    = null;
            byte b;

            for (int i = 0; i < 100; i++)
            {
                original.WriteByte((byte)i);
            }

            secure            = new SecureFile(original, SecureFileMode.Encrypt, publicKey);
            original.Position = 0;
            secure.EncryptTo(encrypted, CryptoAlgorithm.AES, 256);
            secure.Close();
            secure = null;

            encrypted.Position = 0;
            Assert.IsTrue(SecureFile.Validate(encrypted, privateKey));

            encrypted.Position = encrypted.Length - 1;
            b = (byte)encrypted.ReadByte();
            encrypted.Position = encrypted.Length - 1;
            encrypted.WriteByte((byte)(~b));

            encrypted.Position = 0;
            Assert.IsFalse(SecureFile.Validate(encrypted, privateKey));
        }
Exemple #2
0
        public void SecureFile_File_Validate()
        {
            string         originalName = Path.GetTempFileName();
            string         encryptName  = Path.GetTempFileName();
            string         privateKey   = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
            string         publicKey    = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);
            EnhancedStream original     = null;
            EnhancedStream encrypted    = null;
            SecureFile     secure       = null;
            byte           b;

            try
            {
                original = new EnhancedFileStream(originalName, FileMode.Create, FileAccess.ReadWrite);

                for (int i = 0; i < 100; i++)
                {
                    original.WriteByte((byte)i);
                }

                original.Close();
                original = null;

                secure = new SecureFile(originalName, SecureFileMode.Encrypt, publicKey);
                secure.EncryptTo(encryptName, CryptoAlgorithm.AES, 256);
                secure.Close();
                secure = null;

                Assert.IsTrue(SecureFile.Validate(encryptName, privateKey));

                encrypted          = new EnhancedFileStream(encryptName, FileMode.Open, FileAccess.ReadWrite);
                encrypted.Position = encrypted.Length - 1;
                b = (byte)encrypted.ReadByte();
                encrypted.Position = encrypted.Length - 1;
                encrypted.WriteByte((byte)(~b));
                encrypted.Close();

                Assert.IsFalse(SecureFile.Validate(encryptName, privateKey));
            }
            finally
            {
                if (original != null)
                {
                    original.Close();
                }

                if (encrypted != null)
                {
                    encrypted.Close();
                }

                System.IO.File.Delete(originalName);
                System.IO.File.Delete(encryptName);
            }
        }