Exemple #1
0
        public void AsymmetricCrypto_EncryptedCredentials()
        {
            string      privateKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
            string      publicKey  = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);
            Credentials credentials;

            byte[] encrypted;

            credentials = new Credentials("realm", "user", "password");
            encrypted   = AsymmetricCrypto.EncryptCredentials(credentials, "RSA", publicKey);
            credentials = AsymmetricCrypto.DecryptCredentials(encrypted, "RSA", privateKey);

            Assert.AreEqual("realm", credentials.Realm);
            Assert.AreEqual("user", credentials.Account);
            Assert.AreEqual("password", credentials.Password);

            // Force a security failure by decrypting with the wrong key

            ExtendedAssert.Throws <SecurityException>(
                () =>
            {
                privateKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
                AsymmetricCrypto.DecryptCredentials(encrypted, "RSA", privateKey);
            });

            // Force a security failure by tampering with the encrypted credentials

            ExtendedAssert.Throws <SecurityException>(
                () =>
            {
                encrypted[4] = (byte)~encrypted[4];
                AsymmetricCrypto.DecryptCredentials(encrypted, "RSA", privateKey);
            });
        }