Пример #1
0
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            SymmetricCryptoProvider symmetricCrypt = new SymmetricCryptoProvider();

            if (string.IsNullOrWhiteSpace(txtDecryptPassphrase.Text))
            {
                MessageBox.Show("Cannot decrypt without a passphrase", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            string plaintext = "";

            try
            {
                plaintext = symmetricCrypt.DecryptWithKey(txtCypertext.Text, txtDecryptPassphrase.Text);
            }
            catch (CryptographicException)
            {
                MessageBox.Show("Unable to decrypt with provided passphrase", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            txtCypertext.Text = plaintext;

            tabControl1.SelectTab(0);
        }
Пример #2
0
        public void Encrypt_Decrypt_ReturnsOriginalValue()
        {
            var crypto = new SymmetricCryptoProvider();

            var cryptoText = crypto.EncryptWithKey(secret, password);
            var original   = crypto.DecryptWithKey(cryptoText, password);

            Assert.AreEqual(secret, original);
        }
Пример #3
0
        public void Encrypt_Decrypt_Hash_ReturnsOriginalValue()
        {
            var crypto = new SymmetricCryptoProvider();

            string hashedPassword = SymmetricCryptoProvider.GetSecureHashForString(password);

            var cryptoText = crypto.EncryptWithKey(secret, hashedPassword);
            var original   = crypto.DecryptWithKey(cryptoText, hashedPassword);

            Assert.AreEqual(secret, original);
        }
Пример #4
0
        public SelfDestructingMessage GetMessage(int messageId, string passphrase)
        {
            var db = new CryptAByteContext();

            SelfDestructingMessage message = db.SelfDestructingMessages.SingleOrDefault(m => m.MessageId == messageId);

            //.Include("SelfDestructingMessageAttachment")

            if (message == null)
            {
                throw new ArgumentOutOfRangeException("messageId", "Message not found.  Was it already read?");
            }

            var crypto = new SymmetricCryptoProvider();


            try
            {
                message.Message = crypto.DecryptWithKey(message.Message, passphrase);

                var attachment = db.SelfDestructingMessageAttachments.FirstOrDefault(a => a.MessageId == messageId);

                if (attachment != null)
                {
                    message.HasAttachment = true;
                    // todo: get filename here
                }
            }
            catch (Exception)
            {
                throw new ArgumentOutOfRangeException("passphrase", "server error decrypting message");
            }

            message.Message = GzipCompression.Decompress(message.Message);

            db.SelfDestructingMessages.Remove(message);
            db.SaveChanges();

            message.SelfDestructingMessageAttachment = new SelfDestructingMessageAttachment
            {
                //   AttachmentName = attachmentName
            };

            return(message);
        }
Пример #5
0
        public SelfDestructingMessageAttachment GetAttachment(int messageId, string passphrase)
        {
            var db     = new CryptAByteContext();
            var crypto = new SymmetricCryptoProvider();

            var attachment = db.SelfDestructingMessageAttachments.SingleOrDefault(m => m.MessageId == messageId);

            if (attachment != null)
            {
                attachment.Attachment = crypto.DecryptWithKey(attachment.Attachment, passphrase);

                db.SelfDestructingMessageAttachments.Remove(attachment);

                // todo: move decompression to this class
            }
            db.SaveChanges();

            return(attachment);
        }
Пример #6
0
        public void DeleteKeyWithPassphrase(string token, string passphrase)
        {
            var db  = new CryptAByteContext();
            var key = db.Keys.Include("Messages").SingleOrDefault(k => k.KeyToken == token);

            if (key == null)
            {
                throw new ArgumentOutOfRangeException("Key for this token not found.  Was it already deleted?");
            }

            var crypto = new SymmetricCryptoProvider();

            try
            {
                var plaintext = crypto.DecryptWithKey(key.PrivateKey, passphrase);
            }
            catch (ArgumentException)
            {
                throw new ArgumentException("Failed to verify passphrase.  A correct passphrase is required to verify the delete request.");
            }

            db.Keys.Remove(key);
            db.SaveChanges();
        }