Пример #1
0
        public void Encrypt_ValidParams_Success([Values(" ", "unencrypted")]
                                                string unencryptedAsString,

                                                [Values(0, 1, 10)]
                                                int workFactor,

                                                [Values("", " ", "salt")]
                                                string saltAsString)
        {
            byte[] unencrypted = Encoding.UTF8.GetBytes(unencryptedAsString);
            byte[] salt        = Encoding.UTF8.GetBytes(saltAsString);

            byte[] key1 = Encoding.UTF8.GetBytes("key1");
            byte[] key2 = Encoding.UTF8.GetBytes("key2");
            var    es1  = new AesEncryptionService(key1);
            var    es2  = new AesEncryptionService(key2);

            byte[] encrypted1 = es1.Encrypt(unencrypted, workFactor, salt);
            byte[] encrypted2 = es2.Encrypt(unencrypted, workFactor, salt);

            Assert.IsNotNull(encrypted1);
            Assert.IsNotNull(encrypted2);
            Assert.IsNotEmpty(encrypted1);
            Assert.IsNotEmpty(encrypted2);
            Assert.AreNotEqual(encrypted1, unencrypted);
            Assert.AreNotEqual(encrypted2, unencrypted);
            Assert.AreNotEqual(encrypted1, encrypted2);
        }
        public void Decrypt_ValidParams_Success([Values(" ", "unencrypted")]
                                                string unencryptedAsString,

                                                [Values(0, 1, 10)]
                                                int workFactor,

                                                [Values("", " ", "salt")]
                                                string saltAsString)
        {
            byte[] unencrypted = Encoding.UTF8.GetBytes(unencryptedAsString);
            byte[] salt        = Encoding.UTF8.GetBytes(saltAsString);

            byte[] key1       = Encoding.UTF8.GetBytes("key1");
            byte[] key2       = Encoding.UTF8.GetBytes("key2");
            var    es1        = new AesEncryptionService(key1);
            var    es2        = new AesEncryptionService(key2);
            byte[] encrypted1 = es1.Encrypt(unencrypted, workFactor, salt);
            byte[] encrypted2 = es2.Encrypt(unencrypted, workFactor, salt);
            byte[] decrypted1 = es1.Decrypt(encrypted1,  workFactor, salt);
            byte[] decrypted2 = es2.Decrypt(encrypted2,  workFactor, salt);

            Assert.IsNotNull(encrypted1);
            Assert.IsNotNull(encrypted2);
            Assert.IsNotEmpty(encrypted1);
            Assert.IsNotEmpty(encrypted2);
            Assert.AreNotEqual(encrypted1, encrypted2);
            Assert.AreEqual(decrypted1, decrypted2);
            Assert.AreEqual(unencrypted, decrypted1);
            Assert.AreEqual(unencrypted, decrypted2);
        }
Пример #3
0
    public void TestKeyInfoCreation()
    {
        var keyInfo = new KeyInfo();

        AesEncryptionService = new AesEncryptionService(keyInfo);
        Assert.IsNotNull(AesEncryptionService.KeyInfo);
        Assert.IsNotNull(AesEncryptionService.KeyInfo.KeyString);
        Assert.IsNotNull(AesEncryptionService.KeyInfo.IVString);
    }
        public void Encrypt_using_missing_key_identifier_must_throw()
        {
            var encryptionKey1 = Encoding.ASCII.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");
            var service1       = new AesEncryptionService(null, new Dictionary <string, byte[]>
            {
                { "some-key", encryptionKey1 }
            }, new List <byte[]>());

            Assert.Catch <InvalidOperationException>(() => service1.Encrypt("string to encrypt", null), "It is required to set the rijndael key identifier.");
        }
        public void CannotDecryptWithDifferentKey()
        {
            var expected = "beautiful";

            var target = new AesEncryptionService(_options, _serializer);

            var encryptedValue = target.Encrypt(expected, _key);

            Assert.ThrowsException <CryptographicException>(() => target.Decrypt(encryptedValue, _key2));
        }
Пример #6
0
        public void TextFileWriter_WritesFileToDisk()
        {
            var path = "file.txt";
            var encryptionService = new AesEncryptionService();
            var textFileWriter    = new TextFileWriter(path, true, encryptionService, Encoding.Unicode);

            textFileWriter.Write("Hello world!");
            var fileExists = File.Exists(path);

            Assert.True(fileExists);
        }
        public void CanDecryptWithSameKey()
        {
            var expected = "beautiful";

            var target = new AesEncryptionService(_options, _serializer);

            var encryptedValue = target.Encrypt(expected, _key);
            var actual         = target.Decrypt(encryptedValue, _key);

            Assert.AreEqual(expected, actual);
        }
        public void Encrypt_InvalidParams_ArgumentNullExceptionThrown(string unencryptedAsString,
                                                                      int    workFactor,
                                                                      string saltAsString)
        {
            byte[] unencrypted = unencryptedAsString == null ? null : Encoding.UTF8.GetBytes(unencryptedAsString);
            byte[] salt        = saltAsString        == null ? null : Encoding.UTF8.GetBytes(saltAsString);

            byte[] key = Encoding.UTF8.GetBytes("key");
            var    es  = new AesEncryptionService(key);

            Assert.Throws<ArgumentNullException>(() => es.Encrypt(unencrypted, workFactor, salt));
        }
Пример #9
0
    public void TestDecryption()
    {
        var keyInfo = new KeyInfo(TestKey, TestIv);

        AesEncryptionService = new AesEncryptionService(keyInfo);

        var enc = "3QFJhcT2MB+/FH7T5JgWfQ==";
        var str = AesEncryptionService.Decrypt(enc);

        Assert.IsNotEmpty(str);
        Assert.AreEqual("Microsoft Rocks", str);
    }
Пример #10
0
    public void TestEncryption()
    {
        var keyInfo = new KeyInfo(TestKey, TestIv);

        AesEncryptionService = new AesEncryptionService(keyInfo);

        var str             = "Microsoft Rocks";
        var encryptedString = AesEncryptionService.Encrypt(str);

        Assert.IsNotEmpty(encryptedString);
        Assert.AreEqual("3QFJhcT2MB+/FH7T5JgWfQ==", encryptedString);
    }
Пример #11
0
        public void Encrypt_InvalidParams_ArgumentOutOfRangeExceptionThrown(string unencryptedAsString,
                                                                            int workFactor,
                                                                            string saltAsString)
        {
            byte[] unencrypted = unencryptedAsString == null ? null : Encoding.UTF8.GetBytes(unencryptedAsString);
            byte[] salt        = saltAsString == null ? null : Encoding.UTF8.GetBytes(saltAsString);

            byte[] key = Encoding.UTF8.GetBytes("key");
            var    es  = new AesEncryptionService(key);

            Assert.Throws <ArgumentOutOfRangeException>(() => es.Encrypt(unencrypted, workFactor, salt));
        }
Пример #12
0
 public BlogConfigurationService(MoongladeDbContext context,
                                 ILogger <BlogConfigurationService> logger,
                                 AesEncryptionService encryptionService)
 {
     _encryptionService = encryptionService;
     if (null != context)
     {
         Context = context;
     }
     if (null != logger)
     {
         Logger = logger;
     }
 }
Пример #13
0
        public void TextFileWriter_WritesFileToDisk()
        {
            var path = "filebuilder.txt";
            var aesEncryptionService = new AesEncryptionService();
            var textFileWriter       = TextFileWriterBuilder.Create(path)
                                       .OverwriteAllowed()
                                       .WithEncryption(aesEncryptionService)
                                       .EncodedUsing(Encoding.Unicode)
                                       .Build();

            textFileWriter.Write("Hello world!");
            var fileExists = File.Exists(path);

            Assert.True(fileExists);
        }
        public void EncryptDecryptTest()
        {
            var mockConfig = new Mock <IConfiguration>();

            mockConfig.Setup(x => x["Key"]).Returns("5678\\#t7N3TVNs12");
            mockConfig.Setup(x => x["Iv"]).Returns("1234\\Eup3==dZM34");

            var encryptionService = new AesEncryptionService(mockConfig.Object);

            var cardNumber = "4111111111111111";
            var encrypted  = encryptionService.Encrypt(cardNumber);
            var decrypted  = encryptionService.Decrypt(encrypted);

            Assert.Equal(cardNumber, decrypted);
        }
Пример #15
0
        public static string GenerateKey()
        {
            string retValue = null;

            using (System.Security.Cryptography.TripleDES des3 = System.Security.Cryptography.TripleDES.Create())
            {
                des3.GenerateKey();
                des3.GenerateIV();

                byte[] bIV  = des3.IV;
                byte[] bKey = des3.Key;

                retValue = "IV: " + AesEncryptionService.ByteArrayToHexString(bIV)
                           + System.Environment.NewLine
                           + "Key: " + AesEncryptionService.ByteArrayToHexString(bKey);
            } // End Using des3

            return(retValue);
        } // End Function GenerateKey
 public void Initialize()
 {
     this.target = new AesEncryptionService();
 }
 public void Constructor_ValidParams_Success(string keyAsString)
 {
     byte[] key = Encoding.UTF8.GetBytes(keyAsString);
     var    es  = new AesEncryptionService(key);
 }
Пример #18
0
 public void Constructor_ValidParams_Success(string keyAsString)
 {
     byte[] key = Encoding.UTF8.GetBytes(keyAsString);
     var    es  = new AesEncryptionService(key);
 }