Example #1
0
        public void GenerateKeys_ReturnsKeys()
        {
            var key = AsymmetricCryptoProvider.GenerateKeys();

            Assert.IsNotNull(key.PrivateKey);
            Assert.IsNotNull(key.PublicKey);
        }
Example #2
0
        public void Create_Message_Encrypt_Decrypt_Verify()
        {
            // Arrange:
            var          repository = new RequestRepository();
            var          request    = CryptoKey.CreateRequest(DateTime.Now);
            var          keys       = AsymmetricCryptoProvider.GenerateKeys();
            var          crypto     = new AsymmetricCryptoProvider();
            const string message    = "secret message";

            string encryptedPassword;
            string hash;
            string encryptedMessage = crypto.EncryptMessageWithKey(message, keys.PublicKey, out encryptedPassword,
                                                                   out hash);

            request.Messages.Add(new Message
            {
                MessageData = encryptedMessage, EncryptionKey = encryptedPassword, MessageHash = hash
            });

            // Act:
            repository.AddRequest(request);
            var retrieved        = repository.GetRequest(request.KeyToken);
            var retrievedMessage = retrieved.Messages.First();

            string messageDecryptionKey;

            var decryptedMessage = crypto.DecryptMessageWithKey(keys.PrivateKey, retrievedMessage.MessageData,
                                                                retrievedMessage.EncryptionKey,
                                                                retrievedMessage.MessageHash, out messageDecryptionKey);

            // Assert:
            Assert.AreEqual(message, decryptedMessage);
        }
Example #3
0
        public void Encrypt_Decrypt_ReturnsOriginalValue()
        {
            var crypto = new AsymmetricCryptoProvider();
            var key    = AsymmetricCryptoProvider.GenerateKeys();

            var cryptoText = crypto.EncryptWithKey(secret, key.PublicKey);
            var original   = crypto.DecryptWithKey(cryptoText, key.PrivateKey);

            Assert.AreEqual(secret, original);
        }
Example #4
0
        public void Message_Encrypt_Decrypt_ReturnsOriginalValue()
        {
            // Arrange
            var    crypto = new AsymmetricCryptoProvider();
            var    key    = AsymmetricCryptoProvider.GenerateKeys();
            string hash;
            string encryptedPassword;

            // Act
            var encryptedMessage = crypto.EncryptMessageWithKey(secret, key.PublicKey, out encryptedPassword, out hash);

            string messageDecryptionKey;

            var decryptedSecret = crypto.DecryptMessageWithKey(key.PrivateKey, encryptedMessage, encryptedPassword, hash, out messageDecryptionKey);

            // Assert
            Assert.AreEqual(secret, decryptedSecret);
            Assert.AreEqual(SymmetricCryptoProvider.GetSecureHashForString(secret), hash, "hashes do not match");
        }
Example #5
0
        public static CryptoKey CreateRequestWithPassPhrase(string passphrase)
        {
            var key = AsymmetricCryptoProvider.GenerateKeys();

            var request = new CryptoKey
                              {
                                  RequestDate = DateTime.UtcNow,
                                  ReleaseDate = DateTime.Now,
                                  KeyToken = UniqueIdGenerator.GetUniqueId(),
                                  PublicKey = key.PublicKey,
                                  PrivateKey = new SymmetricCryptoProvider().EncryptWithKey(key.PrivateKey, passphrase),
                                  PrivateKeyHash = SymmetricCryptoProvider.GetSecureHashForString(key.PrivateKey),
                                  IsPrivateKeyEncrypted = true,
                                  IsPublicKeyOnly = false,
                                  Notifications = new EntityCollection<Notification>(),
                                  Messages = new EntityCollection<Message>(),
                              };

            return request;
        }
Example #6
0
        public static CryptoKey CreateRequest(DateTime? releaseDate = null)
        {
            var key = AsymmetricCryptoProvider.GenerateKeys();

            var request = new CryptoKey
                              {
                                  RequestDate = DateTime.Now,
                                  ReleaseDate = releaseDate ?? DateTime.Now,
                                  KeyToken = UniqueIdGenerator.GetUniqueId(),
                                  PrivateKey = key.PrivateKey,
                                  PublicKey = key.PublicKey,
                                  IsPrivateKeyEncrypted = false,
                                  IsPublicKeyOnly = false,
                                  Notifications = new EntityCollection<Notification>(),
                                  Messages = new EntityCollection<Message>(),
                              };


            return request;
        }
Example #7
0
        public void Can_CreateRequestWithPublicKey_AndRetrieveKeys()
        {
            var repository = new RequestRepository();

            var keys = AsymmetricCryptoProvider.GenerateKeys();

            keys.PrivateKey = new SymmetricCryptoProvider().EncryptWithKey(keys.PrivateKey, password);

            var request = CryptoKey.CreateRequestWithPublicKey(keys.PublicKey, keys.PrivateKey, true);

            repository.AddRequest(request);

            repository.AttachMessageToRequest(request.KeyToken, message);

            var retrieved = repository.GetRequest(request.KeyToken);

            var msg = repository.GetDecryptedMessagesWithPassphrase(request.KeyToken, password);

            Assert.IsTrue(request.IsPrivateKeyEncrypted);

            Assert.IsTrue(request.KeyToken == retrieved.KeyToken);

            Assert.IsTrue(msg.First().MessageData == message);
        }