public int StoreMessage(SelfDestructingMessage selfDestructingMessage, string passphrase, string attachmentName = null, byte[] attachmentData = null) { if (selfDestructingMessage == null) { throw new ArgumentOutOfRangeException("selfDestructingMessage required"); } selfDestructingMessage.Message = GzipCompression.Compress(selfDestructingMessage.Message); var crypto = new SymmetricCryptoProvider(); selfDestructingMessage.Message = crypto.EncryptWithKey(selfDestructingMessage.Message, passphrase); var db = new CryptAByteContext(); SelfDestructingMessageAttachment attachment = null; // save attachment, if it exists if (attachmentData != null && attachmentData.Length > 0) { MemoryStream streamOfOriginalFile = new MemoryStream(1024); using (ZipFile zip = new ZipFile()) { zip.AddEntry(attachmentName, attachmentData); // zip.AddEntry(self, fileData); zip.Save(streamOfOriginalFile); } byte[] zippedFile = RequestRepository.ReadFully(streamOfOriginalFile); string fileAsString = Convert.ToBase64String(zippedFile); attachment = new SelfDestructingMessageAttachment { Attachment = fileAsString }; attachment.Attachment = crypto.EncryptWithKey(fileAsString, passphrase); attachment.SentDate = DateTime.Now; //db.SelfDestructingMessageAttachments.Add(attachment); } db.SelfDestructingMessages.Add(selfDestructingMessage); db.SaveChanges(); if (attachment != null) { attachment.MessageId = selfDestructingMessage.MessageId; db.SelfDestructingMessageAttachments.Add(attachment); db.ChangeTracker.DetectChanges(); db.SaveChanges(); } return(selfDestructingMessage.MessageId); }
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); }
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); }
private void btnEncrypt_Click(object sender, EventArgs e) { SymmetricCryptoProvider symmetricCrypt = new SymmetricCryptoProvider(); string password = txtPassphrase.Text; if (string.IsNullOrWhiteSpace(password)) { password = PronounceablePasswordGenerator.Generate(10); MessageBox.Show( string.Format("Your random password is {0}. \r Please copy it now then click OK.", password), "Password generated", MessageBoxButtons.OK, MessageBoxIcon.Information); } string cypertext = symmetricCrypt.EncryptWithKey(txtClearText.Text, password); txtCypertext.Text = cypertext; tabControl1.SelectTab(1); }