コード例 #1
0
ファイル: AESUtilityTest.cs プロジェクト: devoctomy/cachy
        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);
        }
コード例 #2
0
        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 AESEncryptNode.");

                Boolean randomIV = false;
                if (IV.Value == null)
                {
                    Byte[] iv = SimpleRandomGenerator.QuickGetRandomBytes(16);
                    IV.SetValue(iv);
                    randomIV = true;
                }

                Byte[] inBlock = UnicodeEncoding.Unicode.GetBytes((String)PlainText.Value);
                DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Creating AES encryptor.");
                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.EncryptBytes(inBlock, (Byte[])Key.Value, (Byte[])IV.Value);

                if (randomIV)
                {
                    using (MemoryStream dataWithIV = new MemoryStream())
                    {
                        dataWithIV.Write(IV.Value, 0, IV.Value.Length);
                        dataWithIV.Write(outBlock, 0, outBlock.Length);
                        dataWithIV.Flush();
                        _encryptedData.SetValue(dataWithIV.ToArray());
                    }
                }
                else
                {
                    _encryptedData.SetValue(outBlock);
                }

                retVal = true;
            }
            finally
            {
                _state = Common.NodeState.Processed;
                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);
            }
        }