Exemple #1
0
        static int Main(string[] args)
        {
            // Please set keyId to a key you have already created.
            String keyId = "HVzG34L2MVI";

            // Get the user's home path and password persistor from the environment.
            String homePath = Environment.GetEnvironmentVariable("USERPROFILE");

            String persistorPassword = Environment.GetEnvironmentVariable("IONIC_PERSISTOR_PASSWORD");

            if (persistorPassword == null || persistorPassword.Length == 0)
            {
                Console.WriteLine("Please provide the persistor password as env variable: IONIC_PERSISTOR_PASSWORD");
                WaitForInput();
                Environment.Exit(1);
            }

            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

            // Create a password persistor for agent initialization.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = homePath + "\\.ionicsecurity\\profiles.pw";
                persistor.Password = persistorPassword;

                agent.SetMetadata(Agent.MetaApplicationName, "GetKey Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Fetch a single key from the agent.
            GetKeysResponse.Key key = null;
            try
            {
                key = agent.GetKey(keyId).Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching key {0}: {1}", keyId, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            Console.WriteLine("Key ID             : " + key.Id);
            Console.WriteLine("Key Bytes          : " + BitConverter.ToString(key.KeyBytes).Replace("-", String.Empty));
            Console.WriteLine("Fixed Attributes   : " + JsonDump(key.Attributes));
            Console.WriteLine("Mutable Attributes : " + JsonDump(key.MutableAttributes));

            WaitForInput();
            return(0);
        }
Exemple #2
0
        static int Main(string[] args)
        {
            // Get the user's home path and password persistor from the environment.
            String homePath = Environment.GetEnvironmentVariable("USERPROFILE");

            String persistorPassword = Environment.GetEnvironmentVariable("IONIC_PERSISTOR_PASSWORD");

            if (persistorPassword == null || persistorPassword.Length == 0)
            {
                Console.WriteLine("Please provide the persistor password as env variable: IONIC_PERSISTOR_PASSWORD");
                WaitForInput();
                Environment.Exit(1);
            }

            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

            // Create a password persistor for agent initialization.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = homePath + "\\.ionicsecurity\\profiles.pw";
                persistor.Password = persistorPassword;

                agent.SetMetadata(Agent.MetaApplicationName, "Ionic Encryption Tutorial");
                agent.SetMetadata(Agent.MetaApplicationVersion, "1.0.0");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            /*****************************************************************
            * SENDER
            *****************************************************************/

            //The message to encrypt.
            String message = "this is a secret message!";

            // Create single key without attributes.
            CreateKeysResponse.Key createdKey = null;
            try
            {
                createdKey = agent.CreateKey().Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Key creation error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Initialize sender AES CTR cipher object.
            AesCtrCipher senderAes = new AesCtrCipher();

            byte[] senderKeyBytes = createdKey.KeyBytes;
            senderAes.KeyBytes = senderKeyBytes;

            // Encrypt
            byte[] cipherText = new byte[256];

            try
            {
                senderAes.Encrypt(message, ref cipherText);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Encryption error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Create a string payload to send to the receiver.
            String b64CipherText = Convert.ToBase64String(cipherText);
            Dictionary <String, String> payload = new Dictionary <String, String>
            {
                ["key_id"]         = createdKey.Id,
                ["b64_ciphertext"] = b64CipherText
            };

            Console.WriteLine("CREATED KEYID : " + createdKey.Id);
            Console.WriteLine("CIPHERTEXT    : {0}", BitConverter.ToString(cipherText).Replace("-", String.Empty));

            Console.WriteLine("\nPAYLOAD       : " + JsonDump(payload));

            /*****************************************************************
            * RECEIVER
            *****************************************************************/

            // Imagine that this reciever recieved a 'payload'.
            String payloadKeyId = payload["key_id"];

            // Fetch the key from the payload.
            GetKeysResponse.Key fetchedKey = null;
            try
            {
                fetchedKey = agent.GetKey(payloadKeyId).Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching payload key {0}: {1}", payloadKeyId, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Initialize receiver AES CTR cipher object.
            AesCtrCipher receiverAes = new AesCtrCipher();

            byte[] keyBytes = fetchedKey.KeyBytes;
            receiverAes.KeyBytes = keyBytes;

            // Decrypt
            string plainText = null;

            try
            {
                receiverAes.Decrypt(cipherText, ref plainText);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Decryption error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            Console.WriteLine("\nFETCHED KEYID : " + fetchedKey.Id);
            Console.WriteLine("PLAINTEXT     : {0}", plainText);

            WaitForInput();
            return(0);
        }
Exemple #3
0
        static int Main(string[] args)
        {
            // Get the user's home path and password persistor from the environment.
            String homePath = Environment.GetEnvironmentVariable("USERPROFILE");

            String persistorPassword = Environment.GetEnvironmentVariable("IONIC_PERSISTOR_PASSWORD");

            if (persistorPassword == null || persistorPassword.Length == 0)
            {
                Console.WriteLine("Please provide the persistor password as env variable: IONIC_PERSISTOR_PASSWORD");
                WaitForInput();
                Environment.Exit(1);
            }

            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

            // Create a password persistor for agent initialization.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = homePath + "\\.ionicsecurity\\profiles.pw";
                persistor.Password = persistorPassword;

                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Set the application metadata.
            try
            {
                agent.SetMetadata(Agent.MetaApplicationName, "Ionic Keys Tutorial");
                agent.SetMetadata(Agent.MetaApplicationVersion, "1.0.0");
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error setting the application metadata: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Define fixed attributes.
            AttributesDictionary fixedKeyAttrs = new AttributesDictionary();

            fixedKeyAttrs.Add("data-type", new List <string> {
                "Finance"
            });
            fixedKeyAttrs.Add("region", new List <string> {
                "North America"
            });

            // Define mutable keys.
            AttributesDictionary mutableKeyAttrs = new AttributesDictionary();

            mutableKeyAttrs.Add("classification", new List <string> {
                "Restricted"
            });
            mutableKeyAttrs.Add("designated_owner", new List <string> {
                "*****@*****.**"
            });

            // Create single key with fixed attributes.
            CreateKeysResponse.Key createdKey = null;
            try
            {
                createdKey = agent.CreateKey(fixedKeyAttrs, mutableKeyAttrs).Keys[0];
            }
            catch (SdkException sdkExp
                   )
            {
                Console.WriteLine("Key creation error: {0}", sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Display the created key information.
            Console.WriteLine("\nNEW KEY:");
            Console.WriteLine("Key ID             : " + createdKey.Id);
            Console.WriteLine("Key Bytes          : " + BitConverter.ToString(createdKey.KeyBytes).Replace("-", String.Empty));
            Console.WriteLine("Fixed Attributes   : " + JsonDump(createdKey.Attributes));
            Console.WriteLine("Mutable Attributes : " + JsonDump(createdKey.MutableAttributes));

            // Fetch a single key from the agent.
            GetKeysResponse.Key fetchedKey = null;
            try
            {
                fetchedKey = agent.GetKey(createdKey.Id).Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching key {0}: {1}", createdKey.Id, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Display the fetched key information.
            Console.WriteLine("\nFETCHED KEY:");
            Console.WriteLine("Key ID             : " + fetchedKey.Id);
            Console.WriteLine("Key Bytes          : " + BitConverter.ToString(fetchedKey.KeyBytes).Replace("-", String.Empty));
            Console.WriteLine("Fixed Attributes   : " + JsonDump(fetchedKey.Attributes));
            Console.WriteLine("Mutable Attributes : " + JsonDump(fetchedKey.MutableAttributes));

            // Merge new and existing mutable attributes.
            AttributesDictionary updatedMutableKeyAttrs = fetchedKey.MutableAttributes;

            updatedMutableKeyAttrs["classification"] = new List <string> {
                "Highly Restricted"
            };

            // Create the update key request.
            bool forceUpdate = false;
            UpdateKeysRequest updateKeysRequest = new UpdateKeysRequest();

            UpdateKeysRequest.Key updateKey = new UpdateKeysRequest.Key(fetchedKey, forceUpdate);
            updateKey.MutableAttributes = updatedMutableKeyAttrs;
            updateKeysRequest.addKey(updateKey);

            // Update the key attributes on the agent.
            UpdateKeysResponse.Key updatedKey = null;
            try
            {
                updatedKey = agent.UpdateKeys(updateKeysRequest).Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error updating key {0}: {1}", fetchedKey.Id, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Display the updated key information.
            Console.WriteLine("\nUPDATED KEY:");
            Console.WriteLine("Key ID             : " + updatedKey.Id);
            Console.WriteLine("Key Bytes          : " + BitConverter.ToString(updatedKey.KeyBytes).Replace("-", String.Empty));
            Console.WriteLine("Fixed Attributes   : " + JsonDump(updatedKey.Attributes));
            Console.WriteLine("Mutable Attributes : " + JsonDump(updatedKey.MutableAttributes));

            WaitForInput();
            return(0);
        }
Exemple #4
0
        static int Main(string[] args)
        {
            // Please set keyId to a key you have already created.
            String keyId = null;

            if (keyId == null)
            {
                Console.WriteLine("Please set the keyId to a key you have already created.");
                WaitForInput();
                Environment.Exit(1);
            }

            // Get the user's home path and password persistor from the environment.
            String homePath = Environment.GetEnvironmentVariable("USERPROFILE");

            String persistorPassword = Environment.GetEnvironmentVariable("IONIC_PERSISTOR_PASSWORD");

            if (persistorPassword == null || persistorPassword.Length == 0)
            {
                Console.WriteLine("Please provide the persistor password as env variable: IONIC_PERSISTOR_PASSWORD");
                WaitForInput();
                Environment.Exit(1);
            }

            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

            // Create a password persistor for agent initialization.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = homePath + "\\.ionicsecurity\\profiles.pw";
                persistor.Password = persistorPassword;

                agent.SetMetadata(Agent.MetaApplicationName, "UpdateKey Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Fetch the key from the agent.
            GetKeysResponse fetchedResponse = null;

            try
            {
                fetchedResponse = agent.GetKey(keyId);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching key {0}: {1}", keyId, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Pull the key out of the response.
            GetKeysResponse.Key fetchedKey = fetchedResponse.Keys[0];

            // Define mutable key attributes
            AttributesDictionary newMutableKeyAttrs = new AttributesDictionary();

            newMutableKeyAttrs.Add("classification", new List <string> {
                "Highly Restricted"
            });

            // Create the update key request.
            bool forceUpdate = false;
            UpdateKeysRequest updateKeysRequest = new UpdateKeysRequest();

            UpdateKeysRequest.Key updateKey = new UpdateKeysRequest.Key(fetchedKey, forceUpdate);
            updateKey.MutableAttributes = newMutableKeyAttrs;
            updateKeysRequest.addKey(updateKey);

            // Update the key attributes on the agent.
            UpdateKeysResponse.Key key = null;
            try
            {
                key = agent.UpdateKeys(updateKeysRequest).Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error updating key {0}: {1}", keyId, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }


            Console.WriteLine("Key ID             : " + key.Id);
            Console.WriteLine("Key Bytes          : " + BitConverter.ToString(key.KeyBytes).Replace("-", String.Empty));
            Console.WriteLine("Fixed Attributes   : " + JsonDump(key.Attributes));
            Console.WriteLine("Mutable Attributes : " + JsonDump(key.MutableAttributes));

            WaitForInput();
            return(0);
        }