コード例 #1
0
        public void AES256BitKeyTest()
        {
            string plainText = "hello world";
            string key       = "!@#$%^&*()123456!@#$%^&*()123456";
            string encrypted = Encryption.AesEncrypt(plainText, key);
            string decrypted = Encryption.AesDecrypt(encrypted, key);

            Assert.NotEqual(plainText, encrypted);
            Assert.Equal(plainText, decrypted);
        }
コード例 #2
0
        public void AesEncryptStringTests()
        {
            const string msg      = "all good men must act";
            const string password = "******";

            var encrypt = Encryption.AesEncrypt(msg, password);
            var decrypt = Encryption.AesDecrypt(encrypt, password);

            Assert.Equal(msg, decrypt);
        }
コード例 #3
0
        /// <summary>
        /// Encrypts the model properties of ISqlDataModel object defined by SQLData instance type dictionary.
        /// </summary>
        /// <param name="dict">Dict.</param>
        /// <param name="obj">Object.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static void EncryptedDataModelProperties <T>(this Dictionary <Type, PropertyInfo[]> dict, T obj) where T : new()
        {
            var props = dict[typeof(T)];

            if (props.Count() > 0)
            {
                foreach (var prop in props)
                {
                    prop.SetValue(obj, Encryption.AesEncrypt(prop.GetString(obj), CoreSettings.Config.AESEncryptionKey), null);
                }
            }
        }
コード例 #4
0
        public void AesEncryptStringTests_BadPassword()
        {
            const string msg      = "all good men must act";
            const string password = "******";

            var encrypt = Encryption.AesEncrypt(msg, password);

            Assert.Throws <CryptographicException>(() => Encryption.AesDecrypt(encrypt, "Bad password"));
            var decrypt = Encryption.AesDecrypt(encrypt, password);

            Assert.Equal(msg, decrypt);
        }
コード例 #5
0
        public void EncrypTest()
        {
            //
            // TODO:  在此处添加测试逻辑
            //
            string QQ             = "2426837192";
            string expectedResult = QQ;

            string cipherText = Encryption.AesEncrypt(QQ);

            Assert.AreEqual(expectedResult, Encryption.AesDecrypt(cipherText));
        }
コード例 #6
0
        public void AesEncryptDecryptTests()
        {
            var msg      = "all good men must act";
            var data1    = Encoding.UTF8.GetBytes(msg);
            var password = "******";

            var key = Encryption.KeyFromPassword(password);

            var edata1 = Encryption.AesEncrypt(data1, key);
            var ddata1 = Encryption.AesDecrypt(edata1, key);

            Assert.Equal(data1, ddata1);
            Assert.Equal(msg, Encoding.UTF8.GetString(ddata1));
        }
コード例 #7
0
        public void AesEncryptDecryptTest_With_InitializationVector()
        {
            var msg      = "all good men must act";
            var data1    = Encoding.UTF8.GetBytes(msg);
            var password = "******";

            var key = Encryption.KeyFromPassword(password);

            var iv     = Encryption.InitializationVector(key, data1);
            var edata1 = Encryption.AesEncrypt(data1, key, iv, true);
            var ddata1 = Encryption.AesDecrypt(edata1, key, iv);

            Assert.Equal(data1, ddata1);
            Assert.Equal(msg, Encoding.UTF8.GetString(ddata1));
        }
コード例 #8
0
        /// <summary>
        /// Encrypts the model properties of ObservableObject lists.
        /// </summary>
        /// <param name="list">List.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static void EncryptedModelProperties <T>(IEnumerable <T> list) where T : BindableObject
        {
            var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        .Where(p => p.GetCustomAttributes(typeof(EncryptedPropertyAttribute)).Count() > 0).ToArray();

            if (props.Count() > 0)
            {
                foreach (var prop in props)
                {
                    foreach (var obj in list)
                    {
                        prop.SetValue(obj, Encryption.AesEncrypt(prop.GetString(obj), CoreSettings.Config.AESEncryptionKey), null);
                    }
                }
            }
        }
コード例 #9
0
        public void AESThrowsInvalidKeyException()
        {
            string plainText = "hello world";

            for (int i = 0; i < 100; i++)
            {
                string key       = RandomString(i);
                var    exception = Record.Exception(() => Encryption.AesEncrypt(plainText, key));
                if (i == 16 || i == 24 || i == 32)
                {
                    Assert.Null(exception);
                }
                else
                {
                    Assert.NotNull(exception);
                    Assert.IsType <System.Security.Cryptography.CryptographicException>(exception);
                }
            }
        }
コード例 #10
0
        public void AesEncryptTests()
        {
            var msg      = "all good men must act";
            var data1    = msg.Utf8ToBytes();
            var password = "******";

            var key = Encryption.KeyFromPassword(password);

            {
                var edata1 = Encryption.AesEncrypt(data1, key);
                var ddata1 = Encryption.AesDecrypt(edata1, key);
                Assert.Equal(data1, ddata1);
                Assert.Equal(msg, Encoders.Utf8.Encode(ddata1));
            }

            {
                var iv     = Encryption.InitializationVector(key, data1);
                var edata1 = Encryption.AesEncrypt(data1, key, iv, true);
                var ddata1 = Encryption.AesDecrypt(edata1, key, iv);
                Assert.Equal(data1, ddata1);
                Assert.Equal(msg, Encoders.Utf8.Encode(ddata1));
            }
        }