public void TestInvalidCertCreateCall()
        {
            var config = new CryptoConfig();

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreateCert(config));
        }
        public void TestValidPbkdf2Sha256Key()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                IsPbeActive     = true,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 256,
                PbeAlgorithm    = PbeAlgorithm.PBKDF2,
                PbeDigest       = PbeDigest.SHA256,
                PbePassword     = "******".ToCharArray(),
                BlockMode       = BlockMode.CBC,
                Padding         = Padding.Pkcs7,
            };

            var pbeBuilder = CryptoFactory.CreatePbe(config);

            config = pbeBuilder.GenerateKeyBytes();

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            config = cipherBuilder.EncryptTextToBytes("Hallo Welt");
            var decodedCipher = Convert.FromBase64String(config.Cipher);

            var result = cipherBuilder.DecryptBytesToText(decodedCipher);

            Assert.Equal("Hallo Welt", result);
        }
Exemplo n.º 3
0
        public void TestGetValidAlgorithms()
        {
            var config = new CryptoConfig();
            var result = config.GetValidAlgorithms();

            Assert.Collection(result,
                              algorithm => Assert.Equal(CipherAlgorithm.AES, algorithm),
                              algorithm => Assert.Equal(CipherAlgorithm.RC4, algorithm));
        }
        public void TestInvalidPbeCreateCall()
        {
            var config = new CryptoConfig {
                PbePassword = "******".ToCharArray()
            };

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreatePbe(config));
        }
Exemplo n.º 5
0
        public void TestGetDigestForAesSScrypt()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.AES,
                PbeAlgorithm    = PbeAlgorithm.SCRYPT
            };
            var result = config.GetDigest();

            Assert.Equal(PbeDigest.GCM, result);
        }
Exemplo n.º 6
0
        public void TestGetDigestForRc4Pbkdf2()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.RC4,
                PbeAlgorithm    = PbeAlgorithm.PBKDF2
            };
            var result = config.GetDigest();

            Assert.Equal(PbeDigest.SHA1, result);
        }
        public void TestInvalidCertCreateConfig()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Digest
            };

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreateCert(config));
        }
Exemplo n.º 8
0
        public void TestGetValidBlockModesForRc4NoPbe()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.RC4
            };
            var result = config.GetValidBlockModes();

            Assert.Collection(result,
                              blockmode => Assert.Equal(BlockMode.None, blockmode));
        }
Exemplo n.º 9
0
        public void TestGetValidBlockModesForAesPbkdf2()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.AES,
                IsPbeActive     = true,
                PbeAlgorithm    = PbeAlgorithm.PBKDF2
            };
            var result = config.GetValidBlockModes();

            Assert.Collection(result,
                              blockmode => Assert.Equal(BlockMode.CBC, blockmode));
        }
Exemplo n.º 10
0
        public void TestGetKeySizesAES()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.AES
            };
            var result = config.GetKeySizes();

            Assert.Collection(result,
                              keySize => Assert.Equal(128, keySize),
                              keySize => Assert.Equal(192, keySize),
                              keySize => Assert.Equal(256, keySize));
        }
Exemplo n.º 11
0
        public void TestGetIntegrityOptionsForDsa()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.AES,
                BlockMode       = BlockMode.CBC,
                Integrity       = Integrity.Dsa
            };
            var result = config.GetIntegrityOptions();

            Assert.Collection(result,
                              option => Assert.Equal(IntegrityOptions.Sha256, option));
        }
Exemplo n.º 12
0
        public void TestGetValidPaddings()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.AES,
                BlockMode       = BlockMode.CBC
            };
            var result = config.GetValidPaddings();

            Assert.Collection(result,
                              p => Assert.Equal(Padding.Pkcs7, p),
                              p => Assert.Equal(Padding.ZeroByte, p));
        }
Exemplo n.º 13
0
        public void TestGetValidBlockModesForRc4Pbkdf2()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.RC4,
                IsPbeActive     = true,
                PbeAlgorithm    = PbeAlgorithm.SCRYPT
            };
            var result = config.GetValidBlockModes();

            Assert.Collection(result,
                              blockmode => Assert.Equal(BlockMode.None, blockmode));
        }
Exemplo n.º 14
0
        public void TestGetValidBlockModesForAesNoPbe()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.AES
            };
            var result = config.GetValidBlockModes();

            Assert.Collection(result,
                              blockmode => Assert.Equal(BlockMode.ECB, blockmode),
                              blockmode => Assert.Equal(BlockMode.CBC, blockmode),
                              blockmode => Assert.Equal(BlockMode.GCM, blockmode),
                              blockmode => Assert.Equal(BlockMode.OFB, blockmode),
                              blockmode => Assert.Equal(BlockMode.CTS, blockmode));
        }
        public void TestEmptyPasswordConfig()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 256,
                PbeDigest       = PbeDigest.GCM,
                BlockMode       = BlockMode.GCM,
                IsPbeActive     = true
            };

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreatePbe(config));
        }
Exemplo n.º 16
0
        public void TestGetKeySizesRC4()
        {
            var config = new CryptoConfig
            {
                CipherAlgorithm = CipherAlgorithm.RC4
            };
            var result = config.GetKeySizes();

            Assert.Collection(result,
                              keySize => Assert.Equal(40, keySize),
                              keySize => Assert.Equal(128, keySize),
                              keySize => Assert.Equal(256, keySize),
                              keySize => Assert.Equal(512, keySize),
                              keySize => Assert.Equal(1024, keySize),
                              keySize => Assert.Equal(2048, keySize));
        }
Exemplo n.º 17
0
        public void TestInvalidCtsInput()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                BlockMode       = BlockMode.CTS,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 128,
                Padding         = Padding.Pkcs7
            };

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            Assert.Throws <Org.BouncyCastle.Crypto.DataLengthException>(
                () => cipherBuilder.EncryptTextToBytes("Hallo Welt"));
        }
        public void TestMissingPrivateKeyDsa256()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Dsa,
                IntegrityOptions  = IntegrityOptions.Sha256
            };

            var certBuilder  = CryptoFactory.CreateCert(config);
            var bytes        = Encoding.UTF8.GetBytes("Hallo Welt");
            var base64String = Convert.ToBase64String(bytes);

            Assert.Throws <ArgumentException>(
                () => certBuilder.SignInput(base64String));
        }
Exemplo n.º 19
0
        public void TestValidRc4Cipher()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                CipherAlgorithm = CipherAlgorithm.RC4,
                KeySize         = 2048
            };

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            config = cipherBuilder.EncryptTextToBytes("Hallo Welt");
            var decodedCipher = Convert.FromBase64String(config.Cipher);

            var result = cipherBuilder.DecryptBytesToText(decodedCipher);

            Assert.Equal("Hallo Welt", result);
        }
        public void TestValidAesCmac()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Digest,
                IntegrityOptions  = IntegrityOptions.AesCmac
            };

            var cipherBuilder = CryptoFactory.CreateDigest(config);
            var bytes         = Encoding.UTF8.GetBytes("Hallo Welt");
            var base64String  = Convert.ToBase64String(bytes);

            config = cipherBuilder.SignInput(base64String);

            var result = cipherBuilder.VerifySign(config.Signature, base64String);

            Assert.True(result);
        }
Exemplo n.º 21
0
        public void TestValidAes192EcbZeroByteCipher()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 192,
                BlockMode       = BlockMode.ECB,
                Padding         = Padding.ZeroByte
            };

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            config = cipherBuilder.EncryptTextToBytes("Hallo Welt");
            var decodedCipher = Convert.FromBase64String(config.Cipher);

            var result = cipherBuilder.DecryptBytesToText(decodedCipher);

            Assert.Equal("Hallo Welt", result);
        }
        public void TestValidDsa256Cert()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Dsa,
                IntegrityOptions  = IntegrityOptions.Sha256
            };

            var certBuilder  = CryptoFactory.CreateCert(config);
            var bytes        = Encoding.UTF8.GetBytes("Hallo Welt");
            var base64String = Convert.ToBase64String(bytes);

            certBuilder.GenerateCerts();
            config = certBuilder.SignInput(base64String);

            var result = certBuilder.VerifySign(config.Signature, base64String);

            Assert.True(result);
        }