public void EncryptMultipleKeys() { #region Arrange var ecKeyPairInfos = Encryption.NitroKey.EllipticCurveCryptographer.GetEcKeyPairInfos(); var encryptionParameter = new HybridEncryption.EncryptionParameter { PublicKeys = ecKeyPairInfos.Select(info => info.PublicKey.ExportPublicKey()), }; #endregion #region Act using (var input = File.OpenRead(this.InputFile)) using (var output = File.Create(this.OutputFile)) { HybridEncryption.Encrypt(input, output, encryptionParameter); } #endregion #region Assert Assert.That(FileOperation.HasFileSignature(this.OutputFile), "HasFileSignature"); Assert.Pass("No Exception occur"); #endregion }
public void HybridEncryption_FilePath() { var myPrivateKeyJson = @" { ""PrivateKey"":""NquxhQX9cmWkyDulDIm6OEzty5roXQHApdJInL4gPQbmkXqrVBHhkA=="", ""PublicKey"":{ ""Qx"":""tS37fNpfdy5diNusCoalxUn0HOdjqJk2OybUA9jy8HnzCSg0rFi3bA=="", ""Qy"":""sYIRXtP1j3tspMyJOeuCDdG4Ifm3DKC+yIHOFyLUTt8R4z0OxWpLqg=="" }, ""InculdePrivateKey"":true } "; var myPrivateKey = EcKeyPair.FromJson(myPrivateKeyJson); var bobPublicAns1Key = EcKeyPair.CreateFromAnsi(Convert.FromBase64String("BFEECnHOtY1L3He8vwsH0ahiDZZzavjxZzciXYXrzaNP1Zn/x1sL+4lvOpktdaZSjgWH/X2JI1rAqBVl7NO3R0UWJ4WtKnrGa5IhSiW0oC7s2lU=")); File.WriteAllText(this.InputFile, "My Stuff"); var encryptionParameter = new HybridEncryption.EncryptionParameter { PublicKeys = new[] { myPrivateKey.ExportPublicKey(), bobPublicAns1Key.ExportPublicKey() }, }; HybridEncryption.Encrypt(this.InputFile, this.EncryptedFile, encryptionParameter); var decryptionParameter = new HybridEncryption.DecryptionParameter { PrivateKey = myPrivateKey, }; var info = HybridEncryption.Decrypt(this.EncryptedFile, this.PlainFile, decryptionParameter); Console.Out.WriteLine(info.FileName); Console.Out.WriteLine(File.ReadAllText(this.PlainFile)); }
public void EncryptIsCanceled() { #region Arrange var alice = EllipticCurveCryptographer.CreateKeyPair(true); var progressValues = new List <double>(); var multiplier = 10; var paramter = new HybridEncryption.EncryptionParameter() { PublicKeys = new[] { alice }, Progress = d => progressValues.Add(d), IsCanceled = () => true, }; #endregion #region Act using (var input = new MemoryStream(new byte[SymmetricEncryption.BufferSize * multiplier])) using (var output = File.Create(this.OutputFile)) { HybridEncryption.Encrypt(input, output, paramter); } #endregion #region Assert Assert.That(progressValues.Count, Is.EqualTo(0)); #endregion }
private async void EncryptCommandHandling() { this.IsBusy = true; await Task.Run(() => { var targetPath = this.HideFilename ? Path.Combine(Path.GetDirectoryName(this.FilePath), $"{Guid.NewGuid()}.enc") : $"{this.FilePath}.enc"; using (var input = File.OpenRead(this.FilePath)) using (var output = File.Create(targetPath)) { var selectedPublicKeys = this.PublicKeys.Where(model => model.IsSelected).Select(model => model.KeyPairInfos.PublicKey.ExportPublicKey()); HybridEncryption.Encrypt(input, output, new HybridEncryption.EncryptionParameter { Progress = this.ReportProgress, IsCanceled = () => this.IsCanceled, PublicKeys = selectedPublicKeys, Filename = Path.GetFileName(this.FilePath), }); } }); this.IsBusy = false; }
public void EncryptMultipleKeys() { #region Arrange var alice = EllipticCurveCryptographer.CreateKeyPair(true); var bob = EllipticCurveCryptographer.CreateKeyPair(true); var guenther = EllipticCurveCryptographer.CreateKeyPair(true); var paramter = new HybridEncryption.EncryptionParameter() { PublicKeys = new[] { alice, bob, guenther } }; #endregion #region Act using (var input = File.OpenRead(this.InputFile)) using (var output = File.Create(this.OutputFile)) { HybridEncryption.Encrypt(input, output, paramter); } #endregion #region Assert Assert.That(FileOperation.HasFileSignature(this.OutputFile), "HasFileSignature"); Assert.Pass("No Exception occur"); #endregion }
public void Encrypt_Should_Throw_Crypto_Exception_If_RSA_Public_Key_Is_Invalid() { byte[] fileBytes = Random.GetNumbers(2048); asymmetricPublicKey.Modulus = new byte[1]; Assert.That(() => { HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); }, Throws.InstanceOf(typeof(CryptoException))); }
public void Encryption_Generates_Different_Hmac_Each_Time() { byte[] fileBytes = Random.GetNumbers(2048); EncryptedPacket encryptedPacket1 = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); EncryptedPacket encryptedPacket2 = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); CollectionAssert.AreNotEqual(encryptedPacket1.Hmac, encryptedPacket2.Hmac); }
public void Encryption_Generates_New_Aes_Key_Each_Time() { byte[] fileBytes = Random.GetNumbers(2048); EncryptedPacket encryptedPacket1 = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); EncryptedPacket encryptedPacket2 = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); CollectionAssert.AreNotEqual(encryptedPacket1.EncryptedSessionKey, encryptedPacket2.EncryptedSessionKey); }
public void Can_Recover_Encrypted_File() { byte[] fileBytes = Random.GetNumbers(2048); EncryptedPacket encryptedPacket = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); byte[] decryptedBytes = HybridEncryption.Decrypt(encryptedPacket, asymmetricPublicKey); CollectionAssert.AreEqual(fileBytes, decryptedBytes); }
public void Encrypt() { var data = "P@ssw0rd"; var keys = HybridEncryption.GenerateKeys(); var encrypted = HybridEncryption.Encrypt(data, keys.PublicKey); var decrypted = HybridEncryption.Decrypt(encrypted, keys.KeysPair); Console.WriteLine(encrypted); Assert.AreEqual(data, decrypted); }
public void Can_Recover_Encrypted_Message() { string inputString = "This is another test string"; byte[] messageBytes = Encoding.UTF8.GetBytes(inputString); EncryptedPacket encryptedPacket = HybridEncryption.Encrypt(DataType.Message, messageBytes, asymmetricPublicKey); byte[] decryptedBytes = HybridEncryption.Decrypt(encryptedPacket, asymmetricPublicKey); string decryptedMessage = Encoding.UTF8.GetString(decryptedBytes); Assert.AreEqual(inputString, decryptedMessage); }
public void Decryption_Throws_CryptoException_When_Hmac_Differs() { byte[] fileBytes = Random.GetNumbers(2048); EncryptedPacket encryptedPacket = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); encryptedPacket.Hmac[25] = (byte)((encryptedPacket.Hmac[25] + 1) % 255); Assert.Throws(typeof(CryptoException), () => { HybridEncryption.Decrypt(encryptedPacket, asymmetricPublicKey); }); }
/// <summary> /// Send a new message /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void SendButton_Click(object sender, RoutedEventArgs e) { ContactPerson contact = (ContactPerson)ContactListListView.SelectedItem; if (contact == null) { return; } EncryptedPacket packetForReceiver = null, packetForSender = null; string text = MessageTextBox.Text; await Task.Run(() => { try { packetForReceiver = HybridEncryption.Encrypt(DataType.Message, Encoding.UTF8.GetBytes(text), AsymmetricEncryption.PublicKeyFromXml(contact.PublicKey)); packetForSender = HybridEncryption.Encrypt(DataType.Message, Encoding.UTF8.GetBytes(text), AsymmetricEncryption.PublicKey); } catch (CryptoException exception) { ErrorText = exception.Message; } }); // try to send message and clear input if (packetForReceiver != null && packetForSender != null) { try { // send to server await Client.SendNewMessage(packetForReceiver, contact.Id, true); await Client.SendNewMessage(packetForSender, contact.Id, false); // add to chat contact.Messages.Add(new Message { SenderName = Client.UserName, SendTime = DateTime.Now, MessageFromSender = MessageTextBox.Text, DataType = DataType.Message }); MessageTextBox.Clear(); } catch (ClientException exception) { // show message to user ErrorText = exception.Message; } } }
public void Can_Encrypt_Message() { byte[] messageBytes = Encoding.UTF8.GetBytes("This is a test string"); EncryptedPacket encryptedPacket = null; Assert.DoesNotThrow(() => { encryptedPacket = HybridEncryption.Encrypt(DataType.Message, messageBytes, asymmetricPublicKey); }); Assert.NotNull(encryptedPacket); Assert.That(encryptedPacket.DataType, Is.EqualTo(DataType.Message), "EncryptedPacket DataType property not set correctly"); }
public void Can_Encrypt_Steganography_File() { byte[] fileBytes = Random.GetNumbers(2_726_403); EncryptedPacket encryptedPacket = null; Assert.DoesNotThrow(() => { encryptedPacket = HybridEncryption.Encrypt(DataType.Steganography, fileBytes, asymmetricPublicKey); }); Assert.NotNull(encryptedPacket); Assert.That(encryptedPacket.DataType, Is.EqualTo(DataType.Steganography), "EncryptedPacket DataType property not set correctly"); }
public void Can_Encrypt_Big_File() { byte[] fileBytes = Random.GetNumbers(5_889_614); EncryptedPacket encryptedPacket = null; Assert.DoesNotThrow(() => { encryptedPacket = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); }); Assert.NotNull(encryptedPacket); Assert.That(encryptedPacket.DataType, Is.EqualTo(DataType.File), "EncryptedPacket DataType property not set correctly"); }
public void EncryptedData_WithKey_IsSuccessfullyDecrypted() { // Arrange var message = "my super secret message"; var keyPair = AsymmetricEncryption.GenerateKey(32); // Act var encryptedData = HybridEncryption.Encrypt(message, keyPair.PublicKey); var decryptedData = HybridEncryption.DecryptValue(encryptedData.EncryptedData, keyPair.PrivateKey, encryptedData.EncryptedKey, encryptedData.InitializationVector); // Assert Assert.Equal(message, decryptedData); Assert.NotEqual(encryptedData.EncryptedData, System.Text.Encoding.UTF8.GetBytes(message)); }
public void EncryptAndDecrypt() { #region Arrange var alice = EllipticCurveCryptographer.CreateKeyPair(true); var encryptionParameter = new HybridEncryption.EncryptionParameter() { PublicKeys = new[] { alice.ExportPublicKey() }, }; var decryptionParameter = new HybridEncryption.DecryptionParameter() { PrivateKey = alice, }; #endregion #region Act using (var input = File.OpenRead(this.InputFile)) using (var output = File.Create(this.OutputFile)) { HybridEncryption.Encrypt(input, output, encryptionParameter); } using (var input = File.OpenRead(this.OutputFile)) using (var output = File.Create(this.ResultFile)) { HybridEncryption.Decrypt(input, output, decryptionParameter); } #endregion #region Assert var inputData = File.ReadAllBytes(this.InputFile); var outputData = File.ReadAllBytes(this.OutputFile); var resultData = File.ReadAllBytes(this.ResultFile); Assert.That(FileOperation.HasFileSignature(this.OutputFile), "HasFileSignature"); Assert.That(inputData.Length, Is.LessThan(outputData.Length), "Input file is smaller than output file"); Assert.That(outputData, Is.Not.EquivalentTo(resultData), "Encrypted file is not equal to plain file"); Assert.That(inputData.Length, Is.EqualTo(resultData.Length), "size of plain file is equal to encrypted file"); Assert.That(inputData, Is.EquivalentTo(resultData), "plain file is equal to encrypted file"); #endregion }
public void EncryptedData_WithModifiedHMAC_IsNotSuccessfullyDecrypted() { // Arrange var message = "my super secret message"; var keyPair = AsymmetricEncryption.GenerateKey(32); var encryptedData = HybridEncryption.Encrypt(message, keyPair.PublicKey); encryptedData.Hmac[1] = byte.MaxValue; // Act var exception = Assert.ThrowsAny <System.Security.Cryptography.CryptographicException>(() => { HybridEncryption.DecryptValue(encryptedData.EncryptedData, keyPair.PrivateKey, encryptedData.EncryptedKey, encryptedData.InitializationVector, encryptedData.Hmac); }); // Assert Assert.Contains("HMAC for decryption does not match encrypted packet", exception.Message); }
public void EncryptAndDecryptMultipleKeys() { #region Arrange var ecKeyPairInfos = Encryption.NitroKey.EllipticCurveCryptographer.GetEcKeyPairInfos().First(); var encryptionParameter = new HybridEncryption.EncryptionParameter() { PublicKeys = new[] { ecKeyPairInfos.PublicKey.ExportPublicKey() } }; var decrptionParameter = new HybridEncryption.DecryptionParameter { Password = Constants.TestPin }; #endregion #region Act using (var input = File.OpenRead(this.InputFile)) using (var output = File.Create(this.OutputFile)) { HybridEncryption.Encrypt(input, output, encryptionParameter); } using (var input = File.OpenRead(this.OutputFile)) using (var output = File.Create(this.ResultFile)) { HybridEncryption.Decrypt(input, output, decrptionParameter); } #endregion #region Assert var inputData = File.ReadAllBytes(this.InputFile); var outputData = File.ReadAllBytes(this.OutputFile); var resultData = File.ReadAllBytes(this.ResultFile); Assert.That(inputData.Length, Is.LessThan(outputData.Length), "Input file is smaller than output file"); Assert.That(outputData, Is.Not.EquivalentTo(resultData), "Encrypted file is not equal to plain file"); Assert.That(FileOperation.HasFileSignature(this.OutputFile), "HasFileSignature"); Assert.That(inputData.Length, Is.EqualTo(resultData.Length), "size of plain file is equal to encrypted file"); Assert.That(inputData, Is.EquivalentTo(resultData), "plain file is equal to encrypted file"); #endregion }
public void Encryption_Populates_All_Packet_Fields() { byte[] fileBytes = Random.GetNumbers(2048); EncryptedPacket encryptedPacket = HybridEncryption.Encrypt(DataType.File, fileBytes, asymmetricPublicKey); Assert.NotNull(encryptedPacket.EncryptedSessionKey, "AES key not set"); CollectionAssert.IsNotEmpty(encryptedPacket.EncryptedSessionKey, "AES key not set"); Assert.NotNull(encryptedPacket.Iv, "AES IV not set"); CollectionAssert.IsNotEmpty(encryptedPacket.Iv, "AES IV not set"); Assert.NotNull(encryptedPacket.Hmac, "HMAC not set"); CollectionAssert.IsNotEmpty(encryptedPacket.Hmac, "HMAC not set"); Assert.NotNull(encryptedPacket.EncryptedData, "Encrypted data not set"); CollectionAssert.IsNotEmpty(encryptedPacket.EncryptedData, "Encrypted data not set"); }
static void Encrypt_Stream(int dataSize) { var data = CryptoHelper.GenerateBytes(dataSize); var keys = HybridEncryption.GenerateKeys(); var encryptedStream = new MemoryStream(); var decryptedStream = new MemoryStream(); HybridEncryption.Encrypt(new MemoryStream(data), encryptedStream, keys.PublicKey); var encrypted = encryptedStream.ToArray(); HybridEncryption.Decrypt(new MemoryStream(encrypted), decryptedStream, keys.KeysPair); var decrypted = decryptedStream.ToArray(); if (dataSize <= 1024) { Console.WriteLine(Convert.ToBase64String(encrypted)); } CollectionAssert.AreEqual(data, decrypted); }
public void EncryptAndDecryptWithFilename() { #region Arrange var alice = EllipticCurveCryptographer.CreateKeyPair(true); var encryptionParameter = new HybridEncryption.EncryptionParameter() { PublicKeys = new[] { alice.ExportPublicKey() }, Filename = Guid.NewGuid().ToString(), }; var decryptionParameter = new HybridEncryption.DecryptionParameter() { PrivateKey = alice, }; #endregion #region Act using (var input = File.OpenRead(this.InputFile)) using (var output = File.Create(this.OutputFile)) { HybridEncryption.Encrypt(input, output, encryptionParameter); } DecryptInfo info; using (var input = File.OpenRead(this.OutputFile)) using (var output = File.Create(this.ResultFile)) { info = HybridEncryption.Decrypt(input, output, decryptionParameter); } #endregion #region Assert Assert.That(encryptionParameter.Filename, Is.EqualTo(info.FileName), "is decrypted filename equal to input filename"); #endregion }
public void Encrypt_Should_Throw_Crypto_Exception_If_Data_Is_Null() { Assert.That(() => { HybridEncryption.Encrypt(DataType.File, null, asymmetricPublicKey); }, Throws.InstanceOf(typeof(CryptoException))); }