Beispiel #1
0
 public void InitalizationVectorTest()
 {
     var target = new AesCryptoProvider() { Key = Key, InitalizationVector = InitializationVector };
     var initializiationVector = InitializationVector;
     target.InitalizationVector = initializiationVector;
     Assert.IsNotNull(target);
 }
        protected override ClientConfiguration GetConnectionConfig()
        {
            //for encryption we need a key
            const string publicKey     = "!mysecretkey#9^5usdk39d&dlf)03sL";
            const string publicKeyName = "publickey";

            //for authentication we need a password
            const string signingKey     = "myauthpassword";
            const string signingKeyName = "mysecret";

            //An in-memory insecure key store - for real world applications use FileSystemKeyStore
            //which uses DAPI to protect the keys that are stotred within it.
            var keystore = new InsecureKeyStore(
                new KeyValuePair <string, string>(publicKeyName, publicKey),
                new KeyValuePair <string, string>(signingKeyName, signingKey));

            //This example is using the symmetric key AES algorithm
            var cryptoProvider = new AesCryptoProvider(keystore)
            {
                PublicKeyName  = publicKeyName,
                SigningKeyName = signingKeyName
            };

            //Get the config and enable field encryption
            var config = base.GetConnectionConfig();

            config.EnableFieldEncryption(cryptoProvider);
            return(config);
        }
        public void Test_Authenticate()
        {
            var aesCryptoProvider = new AesCryptoProvider
            {
                KeyStore       = _keystore,
                PublicKeyName  = "mypublickey",
                SigningKeyName = "myauthsecret"
            };

            var someText      = "The old grey goose jumped over the wrickety gate.";
            var textBytes     = Encoding.UTF8.GetBytes(someText);
            var encryptedTest = aesCryptoProvider.Encrypt(textBytes, out _iv);

            Assert.NotEqual(encryptedTest, textBytes);

            var hash1 = aesCryptoProvider.GetSignature(encryptedTest);

            var decryptedText = aesCryptoProvider.Decrypt(encryptedTest, _iv, "mypublickey");

            Assert.Equal(decryptedText, textBytes);

            var hash2 = aesCryptoProvider.GetSignature(encryptedTest);

            Assert.Equal(hash1, hash2);
        }
        public void Test_Decrypt2()
        {
            var aesCryptoProvider = new AesCryptoProvider
            {
                KeyStore       = _keystore,
                PublicKeyName  = "mypublickey",
                SigningKeyName = "myauthsecret"
            };

            var message     = "The old grey goose jumped over the wrickety gate.";
            var utf8Message = System.Text.Encoding.UTF8.GetBytes(message);

            byte[] iv;
            var    encryptedBytes = aesCryptoProvider.Encrypt(utf8Message, out iv);
            var    base64message  = Convert.ToBase64String(encryptedBytes);

            _output.WriteLine(base64message);
            _output.WriteLine("iv: " + Convert.ToBase64String(iv));

            var base64MessageBytes = Convert.FromBase64String(base64message);
            var decrypted          = aesCryptoProvider.Decrypt(base64MessageBytes, iv);

            Assert.Equal(message, System.Text.Encoding.UTF8.GetString(decrypted));

            _output.WriteLine("authkey: " + Convert.ToBase64String(Encoding.UTF8.GetBytes(_keystore.GetKey("myauthsecret"))));
            _output.WriteLine("sig: " + Convert.ToBase64String(aesCryptoProvider.GetSignature(encryptedBytes)));
        }
Beispiel #5
0
 public void GetInitializationVectorTest()
 {
     var target = new AesCryptoProvider() { Key = Key, InitalizationVector = InitializationVector };
     var expected = typeof(byte[]);
     var actual = target.GetInitializationVector();
     Assert.IsInstanceOfType(actual, expected);
 }
Beispiel #6
0
        public void GetRandomBytes_256KeySize_KeyGenerated()
        {
            const ushort expectedKeySize = 256;

            byte[] key = AesCryptoProvider.GetRandomBytes(expectedKeySize);
            key.Length.Should().Be((int)expectedKeySize);
        }
Beispiel #7
0
 public void EncryptTest()
 {
     var target = new AesCryptoProvider() { Key = Key, InitalizationVector = InitializationVector };
     var clearText = "Test";
     var expected = new byte[] { 56, 148, 231, 102, 26, 238, 54, 45, 113, 205, 129, 41, 57, 221, 255, 130 };
     var actual = target.Encrypt(clearText);
     Assert.AreEqual(System.Convert.ToBase64String(expected), System.Convert.ToBase64String(actual));
 }
Beispiel #8
0
 public void DecryptTest()
 {
     var target = new AesCryptoProvider() { Key = Key, InitalizationVector = InitializationVector};
     var cryptoText = new byte[] { 56, 148, 231, 102, 26, 238, 54, 45, 113, 205, 129, 41, 57, 221, 255, 130 };
     var expected = "Test";
     var actual = target.Decrypt(cryptoText);
     Assert.AreEqual(expected, actual);
 }
Beispiel #9
0
        private void btn_aes_encrypt_Click(object sender, EventArgs e)
        {
            byte[]            keyBytes     = getAesKeyBytes();
            string            hexKeyString = keyBytes.Byte2HexString();
            AesCryptoProvider aes          = new AesCryptoProvider(hexKeyString, check_aes_random.Checked);

            aes.CipherMode  = GetAesCipherMode();
            aes.PaddingMode = GetAesPaddingMode();
            var    buff   = aes.Encrypt(GetAesPlainEncoding().GetBytes(txt_aes_plain.Text.Trim()));
            string cihper = GetAesCipherEncode().Encode(buff);

            txt_aes_cipher.Text = cihper;
        }
        public void Test_Encrypt()
        {
            var aesCryptoProvider = new AesCryptoProvider
            {
                KeyStore      = _keystore,
                PublicKeyName = "mypublickey"
            };

            var someText      = "The old grey goose jumped over the wrickety vase.";
            var textBytes     = Encoding.UTF8.GetBytes(someText);
            var encryptedTest = aesCryptoProvider.Encrypt(textBytes, out _iv);

            Assert.NotEqual(encryptedTest, textBytes);
        }
        public void Test_Encrypt2()
        {
            var aesCryptoProvider = new AesCryptoProvider
            {
                KeyStore      = _keystore,
                PublicKeyName = "mypublickey"
            };

            var message     = "The old grey goose jumped over the wrickety gate.";
            var utf8Message = System.Text.Encoding.UTF8.GetBytes(message);

            byte[] iv;
            var    encryptedBytes = aesCryptoProvider.Encrypt(utf8Message, out iv);
            var    base64message  = Convert.ToBase64String(encryptedBytes);
        }
Beispiel #12
0
 public void KeyTest()
 {
     var target = new AesCryptoProvider() { Key = Key, InitalizationVector = InitializationVector };
     var key = Key;
     target.Key = key;
     Assert.IsNotNull(target);
 }
Beispiel #13
0
 public static void ClassInitialize(TestContext context)
 {
     _key     = AesCryptoProvider.GetRandomBytes(16);
     _hashKey = "Secret".ToBytes(Encoding.UTF8);
 }