public static AttributesDictionary CreateTestAttributes()
        {
            AttributesDictionary keyAttributes = new AttributesDictionary();

            keyAttributes.Add("classifications", new List <string>());
            keyAttributes["classifications"].Add("c1");
            keyAttributes["classifications"].Add("c2");

            return(keyAttributes);
        }
Пример #2
0
        static void Main(string[] args)
        {
            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

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

            // Setup the Chunk Crypto object.
            ChunkCipherAuto chunkCrypto = new ChunkCipherAuto(agent);

            string clearText     = "Hello, World!";
            string encryptedText = null;

            // Define data markings
            AttributesDictionary attributes = new AttributesDictionary();

            attributes.Add("clearance-level", new List <string> {
                "secret"
            });
            ChunkCryptoEncryptAttributes dataMarkings = new ChunkCryptoEncryptAttributes(attributes);

            // Encrypt the string using an Ionic-managed key.
            chunkCrypto.Encrypt(clearText, ref encryptedText, ref dataMarkings);

            string decryptedText = null;

            // Note: Decryption only works if the policy allows it.
            chunkCrypto.Decrypt(encryptedText, ref decryptedText);

            Console.WriteLine("Plain Text: {0}", clearText);
            Console.WriteLine("Ionic Chunk Encrypted Text: {0}", encryptedText);
            Console.WriteLine("Decrypted text: {0}", decryptedText);

            WaitForInput();
        }
Пример #3
0
        static int Main(string[] args)
        {
            // The files to encrypt from and decrypt to.
            string fileOriginal   = "../../../../../../sample-data/files/Message.docx";
            string fileCipherText = "./Message-Protected.docx";
            string filePlainText  = "./Message.docx";

            // 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, "CryptoFileCipherOpenxml Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

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

            // Define mutable attributes and empty fixed attributes.
            AttributesDictionary mutableKeyAttrs = new AttributesDictionary();
            AttributesDictionary fixedKeyAttrs   = new AttributesDictionary();

            mutableKeyAttrs.Add("classification", new List <string> {
                "Restricted"
            });
            FileCryptoEncryptAttributes fileCryptoEncryptAttrs =
                new FileCryptoEncryptAttributes(fixedKeyAttrs, mutableKeyAttrs);

            // Initialize OpenXML file cipher object.
            OpenXmlFileCipher cipher = new OpenXmlFileCipher(agent);

            // Encrypt
            try
            {
                Console.WriteLine("Encrypting file {0} and saving in cipher file {1}", fileOriginal, fileCipherText);
                cipher.Encrypt(fileOriginal, fileCipherText, ref fileCryptoEncryptAttrs);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Generic file cipher encrypt error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Decrypt
            try
            {
                Console.WriteLine("Decrypting file {0} and saving in plaintext file {1}", fileCipherText, filePlainText);
                cipher.Decrypt(fileCipherText, filePlainText);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Generic file cipher decrypt error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Read the files for comparison.
            string message   = File.ReadAllText(fileOriginal);
            string plainText = File.ReadAllText(filePlainText);

            // Verify encrypt and decrypt worked.
            if (message != plainText)
            {
                Console.WriteLine("Encryption/Decrption does not match!");
                Console.WriteLine("Message: {0} - PlainText: {1}", message, plainText);
                WaitForInput();
                Environment.Exit(1);
            }

            WaitForInput();
            return(0);
        }
Пример #4
0
        static void 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, "IonicHelloWorld Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Setup the Chunk Crypto object.
            ChunkCipherAuto chunkCrypto = new ChunkCipherAuto(agent);

            string clearText     = "Hello, World!";
            string encryptedText = null;

            // Define data markings
            AttributesDictionary dataMarkings = new AttributesDictionary();

            dataMarkings.Add("clearance-level", new List <string> {
                "secret"
            });

            // Encrypt the string using an Ionic-managed key.
            chunkCrypto.Encrypt(clearText, ref encryptedText, dataMarkings);

            string decryptedText = null;

            // Note: Decryption only works if the policy allows it.
            chunkCrypto.Decrypt(encryptedText, decryptedText);

            Console.WriteLine("Plain Text: {0}", clearText);
            Console.WriteLine("Ionic Chunk Encrypted Text: {0}", encryptedText);
            Console.WriteLine("Decrypted text: {0}", decryptedText);

            WaitForInput();
        }
Пример #5
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, "CreateKeyWithFixedAttributes Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + 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(); // empty map

            // Create single key with fixed attributes.
            CreateKeysResponse.Key key = null;
            try
            {
                key = agent.CreateKey(fixedKeyAttrs, mutableKeyAttrs).Keys[0];
            }
            catch (SdkException sdkExp
                   )
            {
                Console.WriteLine("Key creation error: {0}", 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);
        }
Пример #6
0
        static int Main(string[] args)
        {
            // Initialize the Ionic agent
            agent = new Agent();
            agent.Initialize();

            // Request keys
            // Forming the key request object
            CreateKeysRequest request = new CreateKeysRequest();
            // Here update request with the list of what it should create.
            AttributesDictionary dirAttributes   = new AttributesDictionary();
            List <string>        listClassValues = new List <string>(1);

            listClassValues.Add("restricted");
            dirAttributes.Add("classification", listClassValues);
            CreateKeysRequest.Key requestKey = new CreateKeysRequest.Key("reference_key", 2, dirAttributes);
            request.Keys.Add(requestKey);
            // Now ask the server to make those keys:
            CreateKeysResponse response;

            try
            {
                response = agent.CreateKeys(request);
            }
            catch (SdkException e)
            {
                System.Console.WriteLine("Error creating keys: {0}", e.Message);
                return(-1);
            }

            // Show us what keys we got (you can always get a key right when you create it):
            List <CreateKeysResponse.Key> responseKeys = response.Keys;
            GetKeysRequest fetchRequest = new GetKeysRequest(); //we will use this to track the keys we want to fetch later

            foreach (CreateKeysResponse.Key responseKey in responseKeys)
            {
                System.Console.WriteLine("We created a key with the Key Tag: {0}", responseKey.Id);
                fetchRequest.KeyIds.Add(responseKey.Id);
            }

            // The rest of this program would typically happen at a different time,
            //  not right after creating the keys, but when you were going to access
            //  the data protected by those keys.

            // Now, using the Key Tags, ask the server for those keys again:
            // NOTE: We populated fetchRequest's list of keytags in the above loop.
            GetKeysResponse fetchResponse;

            try
            {
                fetchResponse = agent.GetKeys(fetchRequest);
            }
            catch (SdkException e)
            {
                System.Console.WriteLine("Error fetching keys: {0}", e.Message);
                return(-1);
            }
            // Show what we got access to after a request for keys:
            foreach (GetKeysResponse.Key responseKey in fetchResponse.Keys)
            {
                System.Console.WriteLine("We fetched a key with the Key Tag: {0}", responseKey.Id);
            }

            // Tell us if we got less keys when we fetched than we created.
            //  This would happen if policy didn't give us access to all the keys.
            if (fetchResponse.Keys.Count < fetchRequest.KeyIds.Count)
            {
                System.Console.Write("We didn't get given all of the requested keys.");
                return(-2);
            }

            System.Console.Read();
            return(0);
        }
Пример #7
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);
        }
Пример #8
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);
        }