/// <summary> /// Formats a message so its easy to work with. Decrypts the message if the cipher is not null. /// </summary> /// <param name="messageIn">The message that will be converted to an instance of NetworkMessage</param> /// <param name="cipher">The SymmetricCipher that will be used for decrypting the message</param> /// <returns></returns> private NetworkMessage GetNetworkMessage(string messageIn) { List <string> arguments = NetworkUtil.RemoveEscape(messageIn); Console.WriteLine(messageIn); if (cipher != null) { for (int i = 0; i < arguments.Count(); i++) { arguments[i] = cipher.Decrypt(arguments[i]); } } if (arguments.Count() >= 2 && Enum.TryParse(arguments[0], out MessageProtocols protocol)) { string message = arguments[1]; NetworkMessage networkMessage = new NetworkMessage() { Protocol = protocol, Message = message }; return(networkMessage); } return(null); }
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 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)}"); }
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); }
protected virtual void OnEncryptedMessageReceived(byte[] data) { data = SymmetricCipher.Decrypt(data); OnDataReceived.Invoke(data); }
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); }
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 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); }