protected override void EstablishConnection(NetworkMessage message) { //Generate symmetric key + IV, save it and encrypt it with the given public assymetric key and send it to the client. try { if (message.TryGetObject <string>(out string publicKey)) { AsymmetricCipher asymmetric = new AsymmetricCipher(); asymmetric.LoadPublicKey(publicKey); cipher = new SymmetricCipher(); SymmetricKey key = new SymmetricKey { Key = asymmetric.Encrypt(cipher.Key), IV = asymmetric.Encrypt(cipher.IV) }; Send(MessageProtocols.Setup, key, false); Send(MessageProtocols.SetUsername, Username); server.Broadcast(MessageProtocols.Connect, this, Username); // Tell users that it connected Send(MessageProtocols.Users, server.ConnectedUsers().ToArray()); } else { throw new Exception("Failed to load the public key"); } } catch (Exception) { Terminate(); } }
internal virtual SymmetricCipher CreateSymmetricCipher() { // TODO: Instantiate an appropriate concrete class. SymmetricCipher target = null; return(target); }
public void DeriveKey_CreateCommonKeyAndEncryptString_Pass() { ECDiffieHellmanAgreement agreement; byte[] commonKey; byte[] iv; string messageFromBob = "Hello my friend, how are you?"; string encrypted; string decrypted; using(var alice = new ECDiffieHellmanCipher<ECDiffieHellmanCng>()) { agreement = alice.Agreement; using(var bob = new ECDiffieHellmanCipher<ECDiffieHellmanCng>(agreement)) { commonKey = alice.DeriveKey(bob.PublicKey); using (var cipher = new SymmetricCipher<AesManaged>(commonKey, out iv)) encrypted = cipher.EncryptToString(messageFromBob); } using (var cipher = new SymmetricCipher<AesManaged>(commonKey, iv)) decrypted = cipher.DecryptToString(encrypted); } Assert.AreEqual(messageFromBob, decrypted); }
private void GenerateSymmetricCipher(SymmetricCipherGenerator scg, int lenIndex) { int cipherSize = scg.CipherSize[lenIndex]; SymmetricCipher cipher = scg.Generate(cipherSize); SymmetricResult symmetricResult = (SymmetricResult)this.resultContentControl.Content; symmetricResult.Cipher = cipher; }
public void ConstructorWithRandomSalt_InstantiateNewObjectWithDefaultSaltLength_Pass() { int saltLength = 32; int actualLength; using (var cipher = new SymmetricCipher<AesManaged>("passwd")) { actualLength = cipher.Salt.Length; } Assert.AreEqual(saltLength, actualLength); }
public void HashAndEncrypt(uint hashDigestSize, uint encryptStartOffset, SymmetricCipher theCipher) { var digestStart = GetBytePosition(); SetBytePosition(digestStart); var hash = new SHA256Managed().ComputeHash(Data, 0, (int)digestStart); Write(hashDigestSize, hash); theCipher.Encrypt(GetBuffer(), encryptStartOffset, GetBuffer(), encryptStartOffset, GetBytePosition() - encryptStartOffset); }
public void EncryptToString_AesEncryptToStringAndDecryptToString_Pass() { string plainText = "Encrypt me but don' forget me."; string encryptedText; string decryptedText; using(var cipher = new SymmetricCipher<AesManaged>("passwd", "mysalt1337")) { encryptedText = cipher.EncryptToString(plainText); decryptedText = cipher.DecryptToString(encryptedText); } Assert.AreEqual(plainText, decryptedText); }
/// <inheritdoc /> public override int GetHashCode() { unchecked { int hashCode = (KeyConfirmation != null ? KeyConfirmation.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (KeyConfirmationVerifiedOutput != null ? KeyConfirmationVerifiedOutput.GetHashCodeExt() : 0); hashCode = (hashCode * 397) ^ KeyDerivation.GetHashCode(); hashCode = (hashCode * 397) ^ SymmetricCipher.GetHashCode(); hashCode = (hashCode * 397) ^ Authentication.GetHashCode(); hashCode = (hashCode * 397) ^ AuthenticationVerifiedOutput.GetHashCodeExt(); return(hashCode); } }
public void AesBasicCipher1_SameInstance_ComparesOutput() { string plainText = "Encrypt me but don' forget me."; byte[] plaindata = Encoding.UTF8.GetBytes(plainText); string encryptedText; string decryptedText; using (var cipher = new SymmetricCipher<AesManaged>("passwd", "mysalt1337")) { encryptedText = cipher.EncryptToString(plaindata); decryptedText = cipher.DecryptToString(encryptedText); } Assert.AreEqual(plainText, decryptedText); }
public static void TestSymmetricEncrypt1() { Console.WriteLine("TestSymmetric1"); var cipher = new SymmetricCipher(SymmetricAlgorithm.Des); Console.WriteLine($"secretKey:{cipher.Base64SecretKey}"); var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes("对称加密测试1")); Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}"); var decrypted = cipher.Decrypt(encrypted); Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}"); }
public void EncryptBlockTest() { SymmetricCipher target = CreateSymmetricCipher(); // TODO: Initialize to an appropriate value byte[] inputBuffer = null; // TODO: Initialize to an appropriate value int inputOffset = 0; // TODO: Initialize to an appropriate value int inputCount = 0; // TODO: Initialize to an appropriate value byte[] outputBuffer = null; // TODO: Initialize to an appropriate value int outputOffset = 0; // TODO: Initialize to an appropriate value int expected = 0; // TODO: Initialize to an appropriate value int actual; actual = target.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
public static void TestSymmetricEncrypt2() { Console.WriteLine("TestSymmetric2"); var secretKey = Convert.FromBase64String("6V2SlGS25bw="); var cipher = new SymmetricCipher(SymmetricAlgorithm.Des, secretKey); var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes("对称加密测试2")); Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}"); // encrypted = Convert.FromBase64String("uMi0zTne1HMv5sU1HB2WI/CsNvBUn5y3"); var decrypted = cipher.Decrypt(encrypted); Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}"); }
/// <inheritdoc /> public PayloadItem CloneSafely() { return(new PayloadItem { Type = Type, Path = String.Copy(Path), ExternalLength = ExternalLength, InternalLength = InternalLength, FormatName = String.Copy(FormatName), FormatData = FormatData.DeepCopy(), SymmetricCipher = SymmetricCipher.CloneSafely(), SymmetricCipherKey = null, Authentication = Authentication.CloneSafely(), AuthenticationKey = null, AuthenticationVerifiedOutput = null, KeyConfirmation = KeyConfirmation.CloneSafely(), KeyConfirmationVerifiedOutput = null, KeyDerivation = KeyDerivation.CloneSafely() }); }
/// <inheritdoc /> public bool Equals(SymmetricManifestCryptographyConfiguration other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return ((KeyConfirmation == null ? other.KeyConfirmation == null : KeyConfirmation.Equals(other.KeyConfirmation)) && (KeyConfirmationVerifiedOutput == null ? other.KeyConfirmation == null : KeyConfirmationVerifiedOutput.SequenceEqualShortCircuiting(other.KeyConfirmationVerifiedOutput)) && KeyDerivation.Equals(other.KeyDerivation) && SymmetricCipher.Equals(other.SymmetricCipher) && Authentication.Equals(other.Authentication) && AuthenticationVerifiedOutput.SequenceEqualShortCircuiting(other.AuthenticationVerifiedOutput)); }
public void ConstructorWithOutParams_InstantiateNewWithRandomSaltAndIV_Pass() { string passwd = "PassIWantt0youS3"; string message = "This is my cool message, you fokkin w0t m8"; string cipherMessage; string decrypted; byte[] salt; byte[] IV; using (var cipher = new SymmetricCipher<AesManaged>(passwd, out salt, out IV)) { cipherMessage = cipher.EncryptToString(message); } using(var cipher = new SymmetricCipher<AesManaged>(passwd, salt, IV)) { decrypted = cipher.DecryptToString(cipherMessage); } Assert.AreEqual(message, decrypted); }
public void Constructor_PassReference_Pass() { string passwd = "superpassword"; byte[] salt = { 1,2,3,4,5,6,7,8,9 }; Cipher originCipher; Cipher handlerCipher; using(SymmetricCipher<AesManaged> cipher = new SymmetricCipher<AesManaged>(passwd, salt)) { using(var handler = new CipherStream<AesManaged>(cipher)) { handlerCipher = handler.Cipher; } originCipher = cipher; Assert.ReferenceEquals(originCipher, handlerCipher); } }
protected override void EstablishConnection(NetworkMessage message) { try { if (message.TryGetObject <SymmetricKey>(out SymmetricKey key)) { cipher = new SymmetricCipher(); string Key = asymCipher.Decrypt(key.Key); string IV = asymCipher.Decrypt(key.IV); cipher.Key = Key; cipher.IV = IV; } else { throw new Exception(); } } catch (Exception) { Terminate(); } }
/// <inheritdoc /> public bool Equals(PayloadItem other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return (Type.Equals(other.Type) && String.Equals(Path, other.Path, Type != PayloadItemType.KeyAction ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) && InternalLength == other.InternalLength && ExternalLength == other.ExternalLength && (FormatName == null ? other.FormatName == null : String.Equals(FormatName, other.FormatName, StringComparison.Ordinal)) && (FormatData == null ? other.FormatData == null : FormatData.SequenceEqualShortCircuiting(other.FormatData)) && SymmetricCipher.Equals(other.SymmetricCipher) && Authentication.Equals(other.Authentication) && (AuthenticationKey == null ? other.AuthenticationKey == null : AuthenticationKey.SequenceEqualShortCircuiting(other.AuthenticationKey)) && AuthenticationVerifiedOutput.SequenceEqualShortCircuiting(other.AuthenticationVerifiedOutput) && (KeyConfirmation == null ? other.KeyConfirmation == null : KeyConfirmation.Equals(other.KeyConfirmation)) && (KeyConfirmationVerifiedOutput == null ? other.KeyConfirmationVerifiedOutput == null : KeyConfirmationVerifiedOutput.SequenceEqualShortCircuiting(other.KeyConfirmationVerifiedOutput)) && KeyDerivation == null ? other.KeyDerivation == null : KeyDerivation.Equals((other.KeyDerivation))); }
public void AesBasicCipher4_SameInstance_ComparesOutput() { string plainText = "Encryption is pretty fun"; byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText); string encryptStr; byte[] decryptArr; string decryptStr; using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb")) { encryptStr = cipher.EncryptToString(plainText); decryptArr = cipher.Decrypt(encryptStr); } decryptStr = Encoding.UTF8.GetString(decryptArr); Assert.AreEqual(plainText, decryptStr); }
public bool DecryptAndCheckHash(uint hashDigestSize, uint decryptStartOffset, SymmetricCipher theCipher) { var bufferSize = GetBufferSize(); var buffer = GetBuffer(); if (bufferSize < decryptStartOffset + hashDigestSize) { return(false); } theCipher.Decrypt(buffer, decryptStartOffset, buffer, decryptStartOffset, bufferSize - decryptStartOffset); var hash = new SHA256Managed().ComputeHash(buffer, 0, (int)(bufferSize - hashDigestSize)); var ret = Memcmp(buffer, bufferSize - hashDigestSize, hash, 0U, hashDigestSize); if (ret) { Resize(bufferSize - hashDigestSize); } return(ret); }
public void DecryptToString_EncryptAndDecryptToString_Pass() { string plainText = "Encrypt me but don' forget me."; byte[] plaindata = Encoding.UTF8.GetBytes(plainText); byte[] encryptedText; string decryptedText; using (var cipher = new SymmetricCipher<AesManaged>("passwd", "mysalt1337")) { encryptedText = cipher.Encrypt(plaindata); decryptedText = cipher.DecryptToString(encryptedText); } Assert.AreEqual(plainText, decryptedText); }
public void BasicCipher_TrippleDESSimpleCryption_ShouldPass() { string plainText = "Encryption is pretty fun"; byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText); byte[] encryptArr; byte[] decryptArr; byte[] IV; using (var cipher = new SymmetricCipher<TripleDESCryptoServiceProvider>("mypasswd2", "mysalt1337bb44")) { IV = cipher.IV; encryptArr = cipher.Encrypt(plainText); } using (var cipher = new SymmetricCipher<TripleDESCryptoServiceProvider>("mypasswd2", "mysalt1337bb44", IV)) { decryptArr = cipher.Decrypt(encryptArr); } CollectionAssert.AreEqual(plainTextArr, decryptArr); }
public void BasicCipher_RijndaelSimpleCryption_ShouldPass() { string plainText = "Encryption is pretty fun"; string passwd = "mypasswd2"; string salt = "mysalt1337bb44"; byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText); byte[] encryptArr; byte[] decryptArr; byte[] IV; using (var cipher = new SymmetricCipher<RijndaelManaged>(passwd, salt)) { IV = cipher.IV; encryptArr = cipher.Encrypt(plainText); } using (var cipher = new SymmetricCipher<RijndaelManaged>(passwd, salt, IV)) { decryptArr = cipher.Decrypt(encryptArr); } CollectionAssert.AreEqual(plainTextArr, decryptArr); }
protected virtual void OnEncryptedMessageReceived(byte[] data) { data = SymmetricCipher.Decrypt(data); OnDataReceived.Invoke(data); }
/// <summary> /// Sends a byte array to a remote peer after encrypting it /// </summary> public void SendData(byte[] data) { data = SymmetricCipher.Encrypt(data); Connection.SendData(data); }
public void AesBasicCipher_MultiInstance_WrongSaltFail() { string plainText = "Encryption is pretty fun"; byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText); byte[] encryptArr; byte[] decryptArr; using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb44")) { encryptArr = cipher.Encrypt(plainText); } using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb")) { decryptArr = cipher.Decrypt(encryptArr); } }
public SymmetricAlgorithm GetSymmetricCipher(SymmetricCipher cipher) { return SymmetricCipherBuilders[cipher].Build(); }
public void Constructor_ChangeKeySize_FailDueToInvalidKeySize() { string plainText = "Encryption is pretty fun"; byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText); int keySize = 234; int newKeySize; using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb44", keysize: keySize)) { newKeySize = cipher.Algorithm.KeySize; } Assert.AreEqual(keySize, newKeySize); }