Example #1
0
        public void SecureFile_Stream_GetPublicKey()
        {
            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;

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

            // Verify that the public key is saved when requested (the default)

            secure = new SecureFile(original, SecureFileMode.Encrypt, publicKey);
            Assert.IsTrue(secure.SavePublicKey);
            Assert.AreEqual(publicKey, secure.PublicKey);

            original.Position = 0;
            secure.EncryptTo(encrypted, CryptoAlgorithm.AES, 256);
            secure.Close();
            secure = null;

            encrypted.Position = 0;
            Assert.AreEqual(publicKey, SecureFile.GetPublicKey(encrypted));

            // Verify that the public key is not saved if SavePublicKey=false

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

            encrypted.Position = 0;
            Assert.IsNull(SecureFile.GetPublicKey(encrypted));
        }
Example #2
0
        public void SecureFile_File_GetPublicKey()
        {
            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;

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

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

                original.Close();
                original = null;

                // Verify that the public key is saved if requested

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

                Assert.AreEqual(publicKey, SecureFile.GetPublicKey(encryptName));

                // Verify that the public key is not saved, if SavePublicKey=false

                System.IO.File.Delete(encryptName);

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

                Assert.IsNull(SecureFile.GetPublicKey(encryptName));
            }
            finally
            {
                if (original != null)
                {
                    original.Close();
                }

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

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