コード例 #1
0
        public void TestEncryptWithPasswordWithSalt()
        {
            byte[] salt = CreateRandomData(128);
            byte[] data = CreateRandomData(1000);


            byte[] encryptedData;
            string base64EncryptedData;

            using (ICrypto crypto = new AesCrypto("password!", salt))
            {
                encryptedData       = crypto.Encrypt(data);
                base64EncryptedData = crypto.EncryptToBase64(data);
            }

            byte[] decryptedData;
            byte[] decryptedDataFromBase64;
            using (ICrypto crypto = new AesCrypto("password!", salt))
            {
                decryptedData           = crypto.Decrypt(encryptedData);
                decryptedDataFromBase64 = crypto.Decrypt(base64EncryptedData);
            }

            TestHelper.AreEqual(data, decryptedData);
            TestHelper.AreEqual(data, decryptedDataFromBase64);
        }
コード例 #2
0
        public void TestHashAndEncryptFromString()
        {
            string data = "Some test data";

            byte[] encryptedData;
            string base64EncryptedData;

            using (ICrypto crypto = new AesCrypto("password!"))
            {
                encryptedData       = crypto.HashAndEncrypt(data);
                base64EncryptedData = crypto.HashAndEncryptToBase64(data);
            }

            string decryptedData;
            string decryptedDataFromBase64;

            using (ICrypto crypto = new AesCrypto("password!"))
            {
                decryptedData           = crypto.DecryptToStringAndCheckHash(encryptedData);
                decryptedDataFromBase64 = crypto.DecryptToStringAndCheckHash(base64EncryptedData);
            }

            Assert.AreEqual(data, decryptedData);
            Assert.AreEqual(data, decryptedDataFromBase64);
        }
コード例 #3
0
        public void TestEncryptWithSevenCharPassword()
        {
            byte[] data = CreateRandomData(1000);

            using (ICrypto crypto = new AesCrypto("1234567"))
            {
                crypto.Encrypt(data);
            }
        }
コード例 #4
0
        public void TestHashAndEncryptWithNoData()
        {
            byte[] data = new byte[0];

            byte[] encryptedData;
            using (ICrypto crypto = new AesCrypto("password!"))
            {
                encryptedData = crypto.HashAndEncrypt(data);
            }

            byte[] decryptedData;
            using (ICrypto crypto = new AesCrypto("password!"))
            {
                decryptedData = crypto.DecryptAndCheckHash(encryptedData);
            }

            Assert.AreEqual(0, decryptedData.Length);
        }
コード例 #5
0
        public void TestHashAndEncryptTamperingWithData()
        {
            byte[] data = CreateRandomData(1000);

            byte[] encryptedData;
            using (ICrypto crypto = new AesCrypto("password!"))
            {
                encryptedData = crypto.HashAndEncrypt(data);
            }

            //Tamper
            encryptedData[20] = (byte)~(uint)encryptedData[20];

            using (ICrypto crypto = new AesCrypto("password!"))
            {
                crypto.DecryptAndCheckHash(encryptedData);
            }
        }
コード例 #6
0
        public void TestHashAndEncrypt()
        {
            byte[] data = CreateRandomData(1000);

            byte[] encryptedData;
            string base64EncryptedData;

            using (ICrypto crypto = new AesCrypto("password!"))
            {
                encryptedData       = crypto.HashAndEncrypt(data);
                base64EncryptedData = crypto.HashAndEncryptToBase64(data);
            }

            byte[] decryptedData;
            byte[] decryptedDataFromBase64;
            using (ICrypto crypto = new AesCrypto("password!"))
            {
                decryptedData           = crypto.DecryptAndCheckHash(encryptedData);
                decryptedDataFromBase64 = crypto.DecryptAndCheckHash(base64EncryptedData);
            }

            TestHelper.AreEqual(data, decryptedData);
            TestHelper.AreEqual(data, decryptedDataFromBase64);
        }