public void GetRandomBytes()
        {
            int expected = 24;

            byte[] random = CryptographyUtility.GetRandomBytes(expected);
            Assert.AreEqual(expected, random.Length);
        }
Beispiel #2
0
        public void Import2BytePasswordProtected()
        {
            byte[] shortKey = new byte[2];
            CryptographyUtility.GetRandomBytes(shortKey);

            ImportExportAssert(shortKey, "12345");
        }
Beispiel #3
0
        public void Import1ByteNotPasswordProtected()
        {
            byte[] shortKey = new byte[1];
            CryptographyUtility.GetRandomBytes(shortKey);

            ImportExportAssert(shortKey, string.Empty);
        }
Beispiel #4
0
        public void Import1024BytePasswordProtected()
        {
            byte[] shortKey = new byte[1024];
            CryptographyUtility.GetRandomBytes(shortKey);

            ImportExportAssert(shortKey, "54321");
        }
        public void GenerateRandomBytes()
        {
            int rndSize = 16;

            byte[] rnd1 = CryptographyUtility.GetRandomBytes(rndSize);
            byte[] rnd2 = CryptographyUtility.GetRandomBytes(rndSize);

            Assert.IsFalse(CryptographyUtility.CompareBytes(rnd1, rnd2));
        }
        public void EncryptAndDecryptTest(byte[] plainText)
        {
            plainText = CryptographyUtility.GetRandomBytes(plainText.Length);

            byte[] cipherText = DefaultSymmProvider.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText), "encrypted");

            byte[] decryptedText = DefaultSymmProvider.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText), "decrypted");
        }
Beispiel #7
0
        public void EncryptAndDecryptOneMegabyte()
        {
            byte[] megabyte = new byte[1024 * 1024];
            CryptographyUtility.GetRandomBytes(megabyte);

            byte[] encrypted = Cryptographer.EncryptSymmetric(symmInstance, megabyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(megabyte, encrypted));

            byte[] decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted);

            Assert.IsTrue(CryptographyUtility.CompareBytes(megabyte, decrypted));
        }
        public void EncryptAndDecryptOneByte()
        {
            byte[] onebyte = new byte[1];
            CryptographyUtility.GetRandomBytes(onebyte);

            byte[] encrypted = cryptographyManager.EncryptSymmetric(symmInstance, onebyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(onebyte, encrypted));

            byte[] decrypted = cryptographyManager.DecryptSymmetric(symmInstance, encrypted);

            Assert.IsTrue(CryptographyUtility.CompareBytes(onebyte, decrypted));
        }
        public void FixtureSetup()
        {
            byte[] key = new byte[32];
            CryptographyUtility.GetRandomBytes(key);
            CryptographySettings settings = (CryptographySettings)context.GetConfiguration(CryptographySettings.SectionName);

            ((SymmetricAlgorithmProviderData)settings.SymmetricCryptoProviders[symmetricInstanceName]).Key = key;

            SymmetricCryptoProviderFactory factory = new SymmetricCryptoProviderFactory(context);

            symmProvider = factory.CreateSymmetricCryptoProvider(symmetricInstanceName);
        }
Beispiel #10
0
        public void DecryptWithDifferentEntropyThanEncrypt()
        {
            byte[] plainBytes = new byte[] { 0, 1, 2, 3, 4 };
            byte[] entropy1   = new byte[16];
            byte[] entropy2   = new byte[16];
            CryptographyUtility.GetRandomBytes(entropy1);
            CryptographyUtility.GetRandomBytes(entropy2);

            DpapiCryptographer dpapi = new DpapiCryptographer(DpapiStorageMode.Machine);

            byte[] encrypted = dpapi.Encrypt(plainBytes, entropy1);
            dpapi.Decrypt(encrypted, entropy2);
        }
Beispiel #11
0
        public void DecryptWithDifferentEntropyThanEncryptThrows()
        {
            byte[] plainBytes = new byte[] { 0, 1, 2, 3, 4 };
            byte[] entropy1   = new byte[16];
            byte[] entropy2   = new byte[16];
            CryptographyUtility.GetRandomBytes(entropy1);
            CryptographyUtility.GetRandomBytes(entropy2);

            DpapiCryptographer dpapi = new DpapiCryptographer(DataProtectionScope.LocalMachine);

            byte[] encrypted = dpapi.Encrypt(plainBytes, entropy1);
            dpapi.Decrypt(encrypted, entropy2);
        }
        public void CanCreatePoliciesTo_EncryptAndDecryptStringWithASymmetricAlgorithm()
        {
            Assert.IsInstanceOfType(container.Resolve <ISymmetricCryptoProvider>(symmetricAlgorithm1),
                                    typeof(SymmetricAlgorithmProvider));

            byte[] megabyte = new byte[1024 * 1024];
            CryptographyUtility.GetRandomBytes(megabyte);

            byte[] encrypted = container.Resolve <ISymmetricCryptoProvider>(symmetricAlgorithm1).Encrypt(megabyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(megabyte, encrypted));

            byte[] decrypted = container.Resolve <ISymmetricCryptoProvider>(symmetricAlgorithm1).Decrypt(encrypted);
            Assert.IsTrue(CryptographyUtility.CompareBytes(megabyte, decrypted));
        }
Beispiel #13
0
        public void EncryptAndDecryptWithTypeUsingProtectedKey()
        {
            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);
            ProtectedKey protectedKey = ProtectedKey.CreateFromPlaintextKey(key, DataProtectionScope.LocalMachine);

            SymmetricCryptographer symm = new SymmetricCryptographer(typeof(RijndaelManaged), protectedKey);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
Beispiel #14
0
        public void EncryptAndDecryptWithAlgorithm()
        {
            SymmetricAlgorithm alg = RijndaelManaged.Create();

            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);

            SymmetricCryptographer symm = new SymmetricCryptographer(alg, key);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
Beispiel #15
0
        public void EncryptAndDecryptWithType()
        {
            string alg = typeof(RijndaelManaged).AssemblyQualifiedName;

            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);

            SymmetricCryptographer symm = new SymmetricCryptographer(alg, key);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
Beispiel #16
0
        public void Properties()
        {
            string           name = "name";
            DpapiStorageMode mode = DpapiStorageMode.User;

            byte[] machineEntropy = new byte[16];
            CryptographyUtility.GetRandomBytes(machineEntropy);

            DpapiSymmetricCryptoProviderData data = new DpapiSymmetricCryptoProviderData();

            data.Name = name;

            Assert.AreEqual(name, data.Name, "name");
            Assert.AreEqual(typeof(DpapiSymmetricCryptoProvider).AssemblyQualifiedName, data.TypeName, "type");

            DpapiSettingsData dpapiData = new DpapiSettingsData();

            dpapiData.Mode          = mode;
            dpapiData.Entropy       = machineEntropy;
            data.DataProtectionMode = dpapiData;
            Assert.AreEqual(mode, data.DataProtectionMode.Mode, "Mode");
            Assert.IsTrue(CryptographyUtility.CompareBytes(machineEntropy, dpapiData.Entropy), "Entropy");
        }
Beispiel #17
0
 private byte[] CreateEntropy()
 {
     byte[] entropy = new byte[16];
     CryptographyUtility.GetRandomBytes(entropy);
     return(entropy);
 }
Beispiel #18
0
 public void Setup()
 {
     this.utility = new ImportExportUtility();
     CryptographyUtility.GetRandomBytes(key);
 }
Beispiel #19
0
 public TestCryptographyConfigurationView() : base(new ConfigurationContext(new ConfigurationDictionary()))
 {
     entropy = new byte[16];
     CryptographyUtility.GetRandomBytes(entropy);
 }
Beispiel #20
0
 private byte[] GenerateSalt()
 {
     return(CryptographyUtility.GetRandomBytes(16));
 }