public void EncryptDecryptSimpleStringTest() { string plainText = "Hello World!"; byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(plainText); byte[] iv = SimpleRandomGenerator.QuickGetRandomBytes(16); byte[] key = SimpleRandomGenerator.QuickGetRandomBytes(32); byte[] cipherBytes = AESUtility.EncryptBytes(plainBytes, key, iv); byte[] decryptedBytes = AESUtility.DecryptBytes(cipherBytes, key, iv); string decryptedPlainText = System.Text.Encoding.UTF8.GetString(decryptedBytes); Assert.AreEqual(plainText, decryptedPlainText); }
public Boolean Process() { Boolean retVal = false; try { _state = Common.NodeState.Processing; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Processing AESDecryptNode."); if (IV.Value == null) { //Split IV from the data using (MemoryStream dataWithIV = new MemoryStream(EncryptedData.Value)) { Byte[] iv = new Byte[16]; dataWithIV.Read(iv, 0, iv.Length); Byte[] dataBlock = new Byte[dataWithIV.Length - iv.Length]; dataWithIV.Read(dataBlock, 0, dataBlock.Length); IV.SetValue(iv); EncryptedData.SetValue(dataBlock); } } Byte[] inBlock = (Byte[])EncryptedData.Value; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Creating AES decryptor."); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Key = {0}.", ((Byte[])Key.Value).ToPrettyString()); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "IV = {0}.", ((Byte[])IV.Value).ToPrettyString()); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Input = {0}.", inBlock.ToPrettyString()); Byte[] outBlock = AESUtility.DecryptBytes(inBlock, (Byte[])Key.Value, (Byte[])IV.Value); _unencryptedData.SetValue(UnicodeEncoding.Unicode.GetString(outBlock)); retVal = true; } catch (Exception ex) { _state = Common.NodeState.Error; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.Fail | DFramework.Logging.Interfaces.LoggerMessageType.Exception | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Exception occurred in AESDecryptNode :: {0}", ex.ToString()); retVal = false; } finally { _state = Common.NodeState.Processed; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Processed AESDecryptNode."); if (retVal) { Processed?.Invoke(this, EventArgs.Empty); } } if (retVal) { if (_next != null && _next.Length > 0) { return(_next[0].Process()); } else { return(true); } } else { return(false); } }
public static T Decode <T>(string source) { byte[] decrypted = AESUtility.DecryptBytes(source, "HENA").GZipDecompress(); return(JsonConvert.DeserializeObject <T>(ByteExtensions.ToUTF8String(decrypted))); }