public EncryptedPacket EncryptSoapBody(string original, RsaWithRsaParameterKey keys) { var hybrid = new HybridEncryption(); var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), keys); return(encryptedBlock); }
public void GivenEncryptingData_WhenDecryptingData_FromImportedKey_ThenDataIsDecrypted() { var currentUser = WindowsIdentity.GetCurrent() .Name; var target = new RSAEncryption("target", currentUser); var signatureContainer = new RSAEncryption("signatureContainer", currentUser); var signaturePublicKey = signatureContainer.ExportKey(false); var targetPublicKey = target.ExportKey(false); HybridEncryption hybridEncryption = HybridEncryption.CreateEncryption(targetPublicKey, "signatureContainer"); HybridDecryption hybridDecryption = HybridDecryption.CreateDecryption("target", signaturePublicKey); RandomNumberGenerator random = new RNGCryptoServiceProvider(); var data = new byte[512]; var sessionKey = new byte[32]; var iv = new byte[16]; random.GetBytes(sessionKey); random.GetBytes(iv); random.GetBytes(data); (EncryptionSettings key, byte[] encryptedData)encryptedResult = hybridEncryption.EncryptData(sessionKey, data, iv); var keyBlob = encryptedResult.key.ExportToBlob(); var keyFromBlob = EncryptionSettings.FromBlob(keyBlob); var decryptedData = hybridDecryption.DecryptData(keyFromBlob, encryptedResult.encryptedData); Assert.That(decryptedData, Is.EqualTo(data)); }
public void GivenEncryptionKeyBlob_WhenImportingKey_ThenImportsSessionKeyCorrectly() { var currentUser = WindowsIdentity.GetCurrent() .Name; var target = new RSAEncryption("target", currentUser); new RSAEncryption("signatureContainer", currentUser); var targetPublicKey = target.ExportKey(false); HybridEncryption hybridEncryption = HybridEncryption.CreateEncryption(targetPublicKey, "signatureContainer"); RandomNumberGenerator random = new RNGCryptoServiceProvider(); var data = File.ReadAllBytes("appsettings.json"); var sessionKey = new byte[32]; var iv = new byte[16]; random.GetBytes(sessionKey); random.GetBytes(iv); (EncryptionSettings key, byte[] encryptedData)encryptedResult = hybridEncryption.EncryptData(sessionKey, data, iv); var key = encryptedResult.key; var keyBlob = key.ExportToBlob(); var keyFromBlob = EncryptionSettings.FromBlob(keyBlob); Assert.That(keyFromBlob.SessionKey, Is.EqualTo(key.SessionKey)); }
public void GivenRandomData_WhenEncryptingData_ThenDataIsEncrypted() { RandomNumberGenerator random = new RNGCryptoServiceProvider(); var data = new byte[512]; var sessionKey = new byte[32]; var iv = new byte[16]; random.GetBytes(sessionKey); random.GetBytes(iv); random.GetBytes(data); var encryptedResult = _hybridEncryption.EncryptData(sessionKey, data, iv); Assert.That(encryptedResult.encryptedData, Is.Not.EqualTo(data)); }
public void GivenRandomData_WhenEncryptingData_ThenSessionKeyIsReturned() { var currentUser = WindowsIdentity.GetCurrent() .Name; var target = new RSAEncryption("target", currentUser); new RSAEncryption("signatureContainer", currentUser); var targetPublicKey = target.ExportKey(false); HybridEncryption hybridEncryption = HybridEncryption.CreateEncryption(targetPublicKey, "signatureContainer"); RandomNumberGenerator random = new RNGCryptoServiceProvider(); var data = new byte[512]; var sessionKey = new byte[32]; var iv = new byte[16]; random.GetBytes(sessionKey); random.GetBytes(iv); random.GetBytes(data); var encryptedResult = hybridEncryption.EncryptData(sessionKey, data, iv); Assert.That(encryptedResult.key.SessionKey, Is.Not.Empty); }
private (SessionKeyContainer key, byte[] encryptedData) EncryptFile(string signatureContainer, string secretsettingsJson, string encryptionKey) { HybridEncryption encryption = HybridEncryption.Create(encryptionKey, signatureContainer); var symmetricKey = CreateSymmetricKey(); (SessionKeyContainer key, byte[] encryptedData)kvp = encryption.EncryptData(symmetricKey.SessionKey, File.ReadAllBytes(secretsettingsJson), symmetricKey.Iv); return(kvp); }
public void GivenEncryptionKeyBlob_WhenImportingKey_ThenImportsIVCorrectly() { RandomNumberGenerator random = new RNGCryptoServiceProvider(); var data = File.ReadAllBytes("appsettings.json"); var sessionKey = new byte[32]; var iv = new byte[16]; random.GetBytes(sessionKey); random.GetBytes(iv); (SessionKeyContainer key, byte[] encryptedData)encryptedResult = _hybridEncryption.EncryptData(sessionKey, data, iv); var key = encryptedResult.key; var keyBlob = key.ExportToBlob(); var keyFromBlob = SessionKeyContainer.FromBlob(keyBlob); Assert.That(keyFromBlob.IV, Is.EqualTo(key.IV)); }
// Тест метод за успешна RSA енкрипција со јавен клуч public EncryptedPacket Encrypt() { var ascii = ASCIIEncoding.Default.GetBytes(_startText); var rsa = (RSACryptoServiceProvider)_certForUJP.PrivateKey; var publicKey = rsa.ExportParameters(false); var rsaParams = new RsaWithRsaParameterKey { PublicKey = publicKey }; var encryptedBlock = _hybrid.EncryptData(ascii, rsaParams); return(encryptedBlock); }
private static void TestHybrid() { const string original = "Very secret and important information that must not fall in the hands of the enemy."; var rsaParams = new RSAWithRSAParameterKey(); rsaParams.AssignNewKey(); var hybrid = new HybridEncryption(); var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams); var decryptedBlock = hybrid.DecryptData(encryptedBlock, rsaParams); Console.WriteLine($"Original Message: {original}"); Console.WriteLine($"Encrypted Block Data: {Convert.ToBase64String(encryptedBlock.EncryptedData)}"); Console.WriteLine($"Decrypted Block: {Convert.ToBase64String(decryptedBlock)}"); Console.WriteLine($"Decrypted Message: {Encoding.UTF8.GetString(decryptedBlock)}"); }
public void HybridEncryption_Test_Pass() { const string expected = "Very secret and important information that can not fall into the wrong hands."; using (var encryption = new HybridEncryption()) { (RSAParameters, RSAParameters)encryptionKeys = new Helpers().GenerateRSAKeys(); (RSAParameters, RSAParameters)signingnKeys = new Helpers().GenerateRSAKeys(); var encryptedBlock = encryption.EncryptData(Encoding.UTF8.GetBytes(expected), encryptionKeys.Item1, signingnKeys.Item2); var decrypted = encryption.DecryptData(encryptedBlock, encryptionKeys.Item2, signingnKeys.Item1); string actual = Encoding.UTF8.GetString(decrypted); string encryptedAsString = Encoding.UTF8.GetString(encryptedBlock.EncryptedData); Assert.AreEqual(expected, actual); Assert.AreNotEqual(expected, encryptedAsString); } }
static void Main(string[] args) { const string original = "Very secret and important information that can not fall into the wrong hands."; var rsaParams = new RsaWithRsaParameterKey(); rsaParams.AssignNewKey(); var hybrid = new HybridEncryption(); var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams); var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams); Console.WriteLine("Hybrid Encryption Demonstration in .NET"); Console.WriteLine("---------------------------------------"); Console.WriteLine(); Console.WriteLine("Original Message = " + original); Console.WriteLine(); Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted)); Console.ReadLine(); }
static void Main() { //const string original = "Very secret and important information that can not fall into the wrong hands."; //string original = new String('0', 127); string original = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrs"; original = GenerateRandomText(); //string original = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890"; //string original = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non dictum diam. Donec feugiat libero sed arcu interdum consectetur vitae amet."; //string original = @"?=??Y@쳘?{?? &긳 ? v ? "; var hybrid = new HybridEncryption(); var rsaParams = new RSAWithRSAParameterKey(); rsaParams.ImportKeys(); var digitalSignature = new DigitalSignature(); digitalSignature.AssignNewKey(); Console.WriteLine("Hybrid Encryption with Integrity Check Demonstration in .NET"); Console.WriteLine("------------------------------------------------------------"); Console.WriteLine(); try { var originalData = Encoding.UTF8.GetBytes(original); byte[] compressedBytes = Compress(originalData); byte[] decompressedBytes = Decompress(compressedBytes); var encryptedBlock = hybrid.EncryptData( originalData, rsaParams, digitalSignature); var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams, digitalSignature); //byte[] gzippedBytes = GetGZippedBytes(encryptedBlock.EncryptedData); //byte[] ungzippedBytes = GetUnGZippedBytes(gzippedBytes); byte[] gzippedBytes = Compress(encryptedBlock.EncryptedData); byte[] ungzippedBytes = Decompress(gzippedBytes); Console.WriteLine("Original Message = " + original); Console.WriteLine("Original Message Length: {0}", original.Length); Console.WriteLine("Compressed Original Message = " + Convert.ToBase64String(compressedBytes)); Console.WriteLine("Compressed Original Message Length: {0}", compressedBytes.Length); Console.WriteLine("DeCompressed Original Message = " + Convert.ToBase64String(decompressedBytes)); Console.WriteLine("DeCompressed Original Message Length: {0}", decompressedBytes.Length); Console.WriteLine("Encrypted Data: {0}", Convert.ToBase64String(encryptedBlock.EncryptedData)); Console.WriteLine("Encrypted Data Size: {0}", encryptedBlock.EncryptedData.Length); Console.WriteLine("GZipped Encrypted Data: {0}", Convert.ToBase64String(gzippedBytes)); Console.WriteLine("GZipped Encrypted Data Size: {0}", gzippedBytes.Length); Console.WriteLine("UnGZipped Encrypted Data: {0}", Convert.ToBase64String(ungzippedBytes)); Console.WriteLine("UnGZipped Encrypted Data Size: {0}", ungzippedBytes.Length); Console.WriteLine(); Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted)); } catch (CryptographicException ex) { Console.WriteLine("Error : " + ex.Message); } Console.ReadLine(); }