Пример #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);
        }
Пример #2
0
        static int Main(string[] args)
        {
            LogBase logger = LogFactory.GetInstance.CreateSimple("sdk.log", false, LogSeverity.SEV_DEBUG);

            Log.SetSingleton(logger);

            // Set constants to use for the ISAgentDeviceProfilePersistorPassword
            string passwordSepPath     = "profiles.pw"; //NOTE: On Linux, this file should typically be placed in ~/.ionicsecurity/profiles.pw
            string passwordSepPassword = "******";

            // First create an agent that will try to read in the SEP stored with a password persistor.
            DeviceProfilePersistorPassword passwordPersistor = new DeviceProfilePersistorPassword();

            passwordPersistor.FilePath = passwordSepPath;
            passwordPersistor.Password = passwordSepPassword;

            // Try reading with this persistor, to see if we have a password persisted SEP already:
            string sActiveDeviceId = null;
            List <IonicSecurity.SDK.DeviceProfile> lstProfiles = new List <IonicSecurity.SDK.DeviceProfile>();

            try
            {
                passwordPersistor.LoadAllProfiles(ref lstProfiles, ref sActiveDeviceId);
            }
            catch (SdkException e)
            {
                Console.WriteLine("Error loading from password persistor: {0}", e.Message);
                if (e.ErrorCodeEnum == ErrorCode.AGENT_RESOURCE_NOT_FOUND)
                {
                    Console.WriteLine("The file for the password persistor was not found at {0}.", passwordSepPath);
                }
                Console.WriteLine("We will now try to convert a default persistor SEP to the password persisted SEP for you.");
                if (0 != convertSepDefaultToPassword(passwordSepPath, passwordSepPassword))
                {
                    Console.ReadKey();
                    return(-2);
                }
                Console.WriteLine("A password protected SEP should now exist.");
            }

            // Now that we have a password protected SEP, we can load it as we normally would when intitializing an agent:
            Agent agent = new Agent();

            try
            {
                agent.Initialize(passwordPersistor);
                Console.WriteLine("A password protected SEP was loaded from {0}", passwordSepPath);
                Console.WriteLine("{0} were loaded.", agent.AllProfiles.Count);
            }
            catch (SdkException e)
            {
                Console.WriteLine("Error initalizing agent: {0}", e.Message);
            }

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

            // Application variables to set.
            string appName    = "app-sample-csharp";
            string appVersion = "1.0.0";

            // Set the application metadata.
            try
            {
                agent.SetMetadata("ionic-application-name", appName);
                agent.SetMetadata("ionic-application-version", appVersion);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error setting the application metadata: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            Console.WriteLine("Application metadata is set");
            WaitForInput();
            return(0);
        }
Пример #4
0
        static void Main(string[] args)
        {
            // Set the persistors's path and password.
            // Information can be found at ../../../../../../sample-data/perisstors/README.md.
            String persistorPath     = "../../../../../../sample-data/persistors/sample-persistor.pw";
            String persistorPassword = "******";

            // Create a blank agent.
            Agent agent = new Agent();

            // Create a password persistor and intialize agent.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = persistorPath;
                persistor.Password = persistorPassword;

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

            // Get the profiles and check if the are any.
            List <DeviceProfile> profiles = agent.AllProfiles;

            if (profiles.Count == 0)
            {
                Console.WriteLine("No profiles for password persistor.");
                WaitForInput();
                return;
            }

            // Display profile information.
            foreach (DeviceProfile profile in profiles)
            {
                Console.WriteLine("-----");
                Console.WriteLine("ID       : " + profile.DeviceId);
                Console.WriteLine("Name     : " + profile.Name);
                Console.WriteLine("Keyspace : " + profile.KeySpace);
                Console.WriteLine("API URL  : " + profile.Server);
            }

            WaitForInput();
        }
Пример #5
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;

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

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

            WaitForInput();
        }
Пример #6
0
        static int convertSepDefaultToPassword(string passwordSepPath, string passwordSepPassword)
        {
            // Temporary variables to hold profiles during conversion.
            string sActiveDeviceId = null;
            List <IonicSecurity.SDK.DeviceProfile> lstProfiles = new List <IonicSecurity.SDK.DeviceProfile>();

            // First create an default persistor that will read in a SEP that you have with your platform's default persistor,
            // for example, one that you created using the Ionic Manager enrollment tool.
            DeviceProfilePersistorDefault defaultPersistor = new DeviceProfilePersistorDefault();

            try
            {
                defaultPersistor.LoadAllProfiles(ref lstProfiles, ref sActiveDeviceId);
            }
            catch (SdkException e)
            {
                Console.WriteLine("Error loading profiles from default SEP persistor: {0}", e.Message);
                return(-1);
            }

            Console.WriteLine("{0} profiles loaded by the default persistor.", lstProfiles.Count);

            // Now we create another persistor that outputs the profiles we loaded in a password protected SEP
            DeviceProfilePersistorPassword passwordPersistor = new DeviceProfilePersistorPassword();

            passwordPersistor.FilePath = passwordSepPath;
            passwordPersistor.Password = passwordSepPassword;

            // Save the profiles using the password persistor.
            try
            {
                passwordPersistor.SaveAllProfiles(lstProfiles, sActiveDeviceId);
            }
            catch (SdkException e)
            {
                Console.WriteLine("Error saving profiles with the password-protected SEP: {0}", e.Message);
                return(-2);
            }

            Console.WriteLine("Profiles saved to password persistor, in encrypted file {0}", passwordSepPath);
            return(0);
        }
Пример #7
0
        static int Main(string[] args)
        {
            // The message to encrypt and authentication data GCM requires.
            String message = "secret message";

            byte[] authData = Encoding.ASCII.GetBytes("data to authenticate");

            // 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, "CryptoAesGcm 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);
            }

            byte[] keyBytes = key.KeyBytes;

            // Initialize AES GCM (Galois Counter Mode) cipher object.
            AesGcmCipher aes = new AesGcmCipher();

            aes.KeyBytes      = keyBytes;
            aes.AuthDataBytes = authData;

            // Encrypt
            byte[] cipherText = new byte[256];
            aes.Encrypt(message, ref cipherText);

            // Decrypt
            string plainText = null;

            aes.Decrypt(cipherText, ref plainText);

            // 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);
            }

            Console.WriteLine("CipherText : {0}", BitConverter.ToString(cipherText).Replace("-", String.Empty));
            Console.WriteLine("PlainText  : {0}", plainText);

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

            String keyRef   = "sample";
            int    keyCount = 5;

            // Create multiple keys request.
            CreateKeysRequest.Key keysToCreate      = new CreateKeysRequest.Key(keyRef, keyCount);
            CreateKeysRequest     createKeysRequest = new CreateKeysRequest();

            createKeysRequest.Keys.Add(keysToCreate);

            // Invoke the agent to create the keys.
            CreateKeysResponse createKeysResponse = null;

            try
            {
                createKeysResponse = agent.CreateKeys(createKeysRequest);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Create multiple keys error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Pull the keys out of the response.
            List <CreateKeysResponse.Key> keys = createKeysResponse.Keys;

            foreach (CreateKeysResponse.Key key in keys)
            {
                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);
        }
Пример #11
0
        static int Main(string[] args)
        {
            // The message to encrypt.
            String message = "secret message";

            // 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, "CryptoChunkCipher 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);
            }

            // Initialize chunk cipher object.
            ChunkCipherAuto cipher = new ChunkCipherAuto(agent);

            // Encrypt
            string cipherText = null;

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

            // Decrypt
            string plainText = null;

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

            // 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);
            }

            Console.WriteLine("CipherText : {0}", cipherText);
            Console.WriteLine("PlainText  : {0}", plainText);

            WaitForInput();
            return(0);
        }
Пример #12
0
        static void Main(string[] args)
        {
            // Set the persistors's path and password.
            // Information can be found at ../../../../../../sample-data/perisstors/README.md.
            String persistorPath     = "../../../../../../../sample-data/persistors/sample-persistor.pw";
            String persistorPassword = "******";

            // Create a blank agent.
            Agent agent = new Agent();

            // Create a password persistor and intialize agent.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = persistorPath;
                persistor.Password = persistorPassword;

                agent.SetMetadata(Agent.MetaApplicationName, "Ionic Profiles 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);
            }

            // Get the profiles and check if the are any.
            List <DeviceProfile> profiles = agent.AllProfiles;

            if (!agent.HasAnyProfiles)
            {
                Console.WriteLine("No profiles for password persistor.");
                WaitForInput();
                return;
            }

            // Display profile information.
            Console.WriteLine("ALL PROFILES:");
            foreach (DeviceProfile profile in profiles)
            {
                Console.WriteLine("-----");
                Console.WriteLine("ID       : " + profile.DeviceId);
                Console.WriteLine("Name     : " + profile.Name);
                Console.WriteLine("Keyspace : " + profile.KeySpace);
                Console.WriteLine("API URL  : " + profile.Server);
            }

            // Verify an active profile exists.
            if (!agent.HasActiveProfile)
            {
                Console.WriteLine("No profile is set as active.");
                WaitForInput();
                Environment.Exit(1);
            }

            // Display the active profile device ID.
            DeviceProfile activeProfilesile = agent.ActiveProfile;

            Console.WriteLine("\nACTIVE PROFILE: " + activeProfilesile.DeviceId);

            // Change the active profile.
            String newProfilesileId = "EfGh.1.54sdf8-sdfj-5802-sd80-248vwqucv9s73";

            Console.WriteLine("\nSETTING NEW ACTIVE PROFILE: " + newProfilesileId);
            bool success = agent.SetActiveProfileById(newProfilesileId);

            if (!success)
            {
                Console.WriteLine("Failed to set active profile to: " + newProfilesileId);
                WaitForInput();
                Environment.Exit(1);
            }

            // Display the active device ID.
            DeviceProfile newActiveProfilesile = agent.ActiveProfile;

            Console.WriteLine("\nNEW ACTIVE PROFILE: " + newActiveProfilesile.DeviceId);

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

            // Create a resource request for "classification" marking values
            // configured in the dashboard.
            GetResourcesRequest.Resource resourceRequest = new GetResourcesRequest.Resource();
            resourceRequest.ResourceId = "marking-values";
            resourceRequest.Args       = "classification";
            GetResourcesResponse resourcesResponse = null;

            // Fetch the resource "classification" marking values.
            try
            {
                resourcesResponse = agent.GetResource(resourceRequest);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error requesting resource: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Extract the resources from the get resources response.
            List <GetResourcesResponse.Resource> resources = resourcesResponse.Resources;

            if (resources.Count == 0)
            {
                Console.WriteLine("There are no resources available.");
                WaitForInput();
                Environment.Exit(1);
            }

            // Display resources.
            Console.WriteLine("Classification values:");
            foreach (GetResourcesResponse.Resource resource in resources)
            {
                Console.WriteLine("Data   : " + resource.Data);
            }

            WaitForInput();
        }
Пример #14
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);
        }
Пример #15
0
        static int Main(string[] args)
        {
            // Please set keyIds to keys you have already created.
            String keyId1 = null;
            String keyId2 = null;
            String keyId3 = null;

            if (keyId1 == null || keyId2 == null || keyId3 == null)
            {
                Console.WriteLine("Please set the keyIds to keys 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, "GetMultipleKeys Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Create a key request for multiple keys.
            GetKeysRequest keyRequest = new GetKeysRequest();

            keyRequest.KeyIds.Add(keyId1);
            keyRequest.KeyIds.Add(keyId2);
            keyRequest.KeyIds.Add(keyId3);

            // Fetch multiple keys from the agent.
            GetKeysResponse getKeysResponse = null;

            try
            {
                getKeysResponse = agent.GetKeys(keyRequest);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching keys: {1}", sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Pull the keys out of the response.
            List <GetKeysResponse.Key> keys = getKeysResponse.Keys;

            if (keys.Count == 0)
            {
                Console.WriteLine("No keys returned for external IDs. (Key does not exist or access was denied.)");
                WaitForInput();
                Environment.Exit(1);
            }

            foreach (GetKeysResponse.Key key in keys)
            {
                Console.WriteLine("-----");
                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);
        }
Пример #16
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);
        }
Пример #17
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, "IonicCiphers Tutorial");
                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!";

            // Initialize chunk sender cipher object.
            ChunkCipherAuto SenderCipher = new ChunkCipherAuto(agent);

            // Encrypt data.
            string cipherText = null;

            try
            {
                SenderCipher.Encrypt(message, ref cipherText);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Chunk sender cipher encrypt error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            Console.WriteLine("CIPHERTEXT : {0}", cipherText);

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

            // Initialize chunk receiver cipher object.
            ChunkCipherAuto recieverCipher = new ChunkCipherAuto(agent);

            // Decrypt data.
            string plainText = null;

            try
            {
                recieverCipher.Decrypt(cipherText, ref plainText);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Chunk receiver cipher decrypt error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            Console.WriteLine("\nPLAINTEXT  : {0}", plainText);

            WaitForInput();
            return(0);
        }
Пример #18
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);
        }
Пример #19
0
        static void Main(string[] args)
        {
            string logFilePath = "../../../../../../sample-data/logs/sample.log";
            bool   appendFile  = false;

            // Log SDK to a file with serverity Debug and above.
            LogBase logger = LogFactory.Instance.CreateSimple(logFilePath, appendFile, LogSeverity.SEV_DEBUG);

            Log.SetSingleton(logger);

            // The message to encrypt.
            String message = "top secret message";

            // 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, "LogSdkCalls 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);
            }

            // Initialize chunk cipher object.
            ChunkCipherAuto cipher = new ChunkCipherAuto(agent);

            // Encrypt
            string cipherText = null;

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

            // Decrypt
            string plainText = null;

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

            // 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);
            }

            Console.WriteLine("CipherText : {0}", cipherText);
            Console.WriteLine("PlainText  : {0}", plainText);

            WaitForInput();
        }
Пример #20
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 a blank agent.
            Agent agent = new Agent();

            // Create a password persistor and intialize agent.
            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 Agents Tutorial");
                agent.SetMetadata(Agent.MetaApplicationVersion, "1.0.0");
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error setting the application metadata: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

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

            // Display the newly created key ID.
            Console.WriteLine("CREATED NEW KEY  : " + key.Id);

            WaitForInput();
        }
Пример #21
0
        static void Main(string[] args)
        {
            // Set the persistor's path.
            String passwordPersistorPath = "../../../../../../sample-data/persistors/convert-persistor.pw";
            String persistorPassword     = "******";

            // Initialize the agent.
            Agent pwAgent = new Agent();

            // Create a password persistor and initialize agent initialization.
            DeviceProfilePersistorPassword passwordPersistor = new DeviceProfilePersistorPassword();

            try
            {
                //DeviceProfilePersistorPassword passwordPersistor = new DeviceProfilePersistorPassword();
                passwordPersistor.FilePath = passwordPersistorPath;
                passwordPersistor.Password = persistorPassword;

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

            if (pwAgent.HasAnyProfiles)
            {
                Console.WriteLine("Password persistor already has profiles.");
                Console.WriteLine("Please delete the file " + passwordPersistorPath + " and rerun.");
                WaitForInput();
                Environment.Exit(1);
            }

            List <DeviceProfile> plaintextProfiles = GetDeviceProfilesFromPlaintextPersistor();
            int numPlaintextProfiles = plaintextProfiles.Count;

            Console.WriteLine("There are {0} plaintext profiles to convert.", numPlaintextProfiles);

            // Get each plaintext profile and add it to the password persistor.
            try
            {
                foreach (DeviceProfile plaintextProfile in plaintextProfiles)
                {
                    pwAgent.AddProfile(plaintextProfile);
                }
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Add Profile error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Save the profiles added to the password persistor.
            try
            {
                pwAgent.SaveProfiles(passwordPersistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Save Profile error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Get the profiles and check if the are any.
            List <DeviceProfile> profiles = pwAgent.AllProfiles;

            if (profiles.Count == 0)
            {
                Console.WriteLine("No profiles for password persistor.");
                WaitForInput();
                Environment.Exit(1);
                return;
            }

            // Display profile information.
            Console.WriteLine("Password Profiles:");
            foreach (DeviceProfile profile in profiles)
            {
                Console.WriteLine("-----");
                Console.WriteLine("ID       : " + profile.DeviceId);
                Console.WriteLine("Name     : " + profile.Name);
                Console.WriteLine("Keyspace : " + profile.KeySpace);
                Console.WriteLine("API URL  : " + profile.Server);
            }
            Console.WriteLine("-----");

            WaitForInput();
        }