예제 #1
0
        public void AESEncryptEncodeDecodeDecryptNodeTest()
        {
            Byte[] salt      = SimpleRandomGenerator.QuickGetRandomBytes(16);
            String password  = "******";
            String plainText = "Testing testing, 123.";

            GraphBuilder           encryptGraph         = new GraphBuilder();
            GraphIO <String>       passwordInput        = new GraphIO <String>(plainText);
            GraphIO <Int32>        keyLengthInput       = new GraphIO <Int32>(32);
            GraphIO <Byte[]>       saltInput            = new GraphIO <Byte[]>(salt);
            Rfc2898DeriveBytesNode deriveEncryptKeyNode = new Rfc2898DeriveBytesNode(passwordInput, keyLengthInput, saltInput, new GraphIO <Int32>(10000));
            AESEncryptNode         encryptNode          = new AESEncryptNode(new GraphIO <String>(plainText), new GraphIO <Byte[]>(null), deriveEncryptKeyNode.DervivedKey);
            Base64EncodeNode       encodeNode           = new Base64EncodeNode(encryptNode.EncryptedData);

            encryptGraph.AddNode("deriveNode", deriveEncryptKeyNode);
            encryptGraph.AddNode("encryptNode", encryptNode);
            encryptGraph.AddNode("encodeNode", encodeNode);
            encryptGraph.CreateRoute("encrypt",
                                     deriveEncryptKeyNode.Password,
                                     encodeNode.EncodedData,
                                     "deriveNode",
                                     new String[] { "deriveNode", "encryptNode", "encodeNode" });

            Object output = null;

            if (encryptGraph.Process(true, "encrypt", password, out output))
            {
                String encryptedEncoded = (String)output;

                GraphBuilder           decryptGraph          = new GraphBuilder();
                GraphIO <String>       encryptedEncodedInput = new GraphIO <String>(encryptedEncoded);
                Rfc2898DeriveBytesNode deriveDecryptKeyNode  = new Rfc2898DeriveBytesNode(new GraphIO <String>(String.Empty), new GraphIO <Int32>(32), new GraphIO <Byte[]>(salt), new GraphIO <Int32>(10000));
                Base64DecodeNode       decodeNode            = new Base64DecodeNode(encryptedEncodedInput);
                AESDecryptNode         decryptNode           = new AESDecryptNode(decodeNode.UnencodedData, new GraphIO <Byte[]>(null), deriveDecryptKeyNode.DervivedKey);

                decryptGraph.AddNode("deriveNode", deriveDecryptKeyNode);
                decryptGraph.AddNode("decodeNode", decodeNode);
                decryptGraph.AddNode("decryptNode", decryptNode);
                decryptGraph.CreateRoute("decrypt",
                                         deriveDecryptKeyNode.Password,
                                         decryptNode.UnencryptedData,
                                         "deriveNode",
                                         new String[] { "deriveNode", "decodeNode", "decryptNode" });

                if (decryptGraph.Process(true, "decrypt", password, out output))
                {
                    String decodedDecrypted = (String)output;
                    Assert.IsTrue(decodedDecrypted == plainText);
                }
            }
        }
예제 #2
0
        public object Read(
            object data, 
            string masterPassphrase,
            params KeyValuePair<string, string>[] parameters)
        {
            if (data.GetType() != typeof(Byte[])) throw new ArgumentException("Data must be of type 'Byte[]'.", "data");

            DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information, "Reading Vault from AES encrypted byte array.");

            Dictionary<string, string> parametersDict = parameters.ToDictionary(x => x.Key, x => x.Value);

            byte[] dataBytesWithSalt = (Byte[])data;
            byte[] dataBytes;
            byte[] salt;
            dataBytesWithSalt.RemoveFromEnd(16, out dataBytes, out salt);

            GraphBuilder decryptGraph = new GraphBuilder("AESEncryptedVaultSerialiser.Read");
            GraphIO<Byte[]> encryptedInput = new GraphIO<Byte[]>(dataBytes);
            GraphIO<Byte[]> saltInput = new GraphIO<Byte[]>(salt);

            IGraphNode keyDerivationNode = null;

            if(parametersDict.ContainsKey("KeyDerivationFunction"))
            {
                string keyDerivationFunction = parametersDict["KeyDerivationFunction"];
                switch (keyDerivationFunction)
                {
                    case "PBKDF2":
                        {
                            int iterationCount = int.Parse(parametersDict["IterationCount"]);
                            keyDerivationNode = new Rfc2898DeriveBytesNode(new GraphIO<String>(String.Empty), new GraphIO<Int32>(32), saltInput, new GraphIO<int>(iterationCount));
                            decryptGraph.AddNode("deriveNode", keyDerivationNode);
                            break;
                        }
                    case "SCRYPT":
                        {
                            int iterationCount = int.Parse(parametersDict["IterationCount"]);
                            int blockSize = int.Parse(parametersDict["BlockSize"]);
                            int threadCount = int.Parse(parametersDict["ThreadCount"]);
                            keyDerivationNode = new SCryptDeriveBytesNode(new GraphIO<String>(String.Empty), new GraphIO<Int32>(32), saltInput, new GraphIO<int>(iterationCount), new GraphIO<int>(blockSize), new GraphIO<int>(threadCount));
                            decryptGraph.AddNode("deriveNode", keyDerivationNode);
                            break;
                        }
                    default:
                        {
                            throw new InvalidOperationException(String.Format("Unknown key derivation function '{0}'.", keyDerivationFunction));
                        }
                }
            }
            else
            {
                keyDerivationNode = new Rfc2898DeriveBytesNode(new GraphIO<String>(String.Empty), new GraphIO<Int32>(32), saltInput, new GraphIO<Int32>(10000));
                decryptGraph.AddNode("deriveNode", keyDerivationNode);
            }

            AESDecryptNode decryptNode = new AESDecryptNode(encryptedInput, new GraphIO<Byte[]>(null), keyDerivationNode.GetBytesIO("DerivedKey"));

            decryptGraph.AddNode("decryptNode", decryptNode);
            decryptGraph.CreateRoute("decrypt",
                keyDerivationNode.GetStringIO("Password"),
                decryptNode.UnencryptedData,
                "deriveNode",
                new String[] { "deriveNode", "decryptNode" });

            Object output = null;
            if (decryptGraph.Process(true, "decrypt", masterPassphrase, out output))
            {
                String decryptedJSON = (String)output;
                JObject json = JObject.Parse(decryptedJSON);

                JSONVaultSerialiser jsonSerialiser = new JSONVaultSerialiser();
                Vault vault = (Vault)jsonSerialiser.Read(json, String.Empty);
                return (vault);
            }
            else
            {
                throw new Exception("Failed to decrypt Vault.");
            }
        }