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, "CreateKey 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); } 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); }
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); }
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); }
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); }
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); }
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(); }
static int Main(string[] args) { // Create a unique external ID with the 32 digits separated by hyphens. String externalId = Guid.NewGuid().ToString("D").ToUpper(); // 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, "CreateKeyWithExternalId Sample"); agent.Initialize(persistor); } catch (SdkException sdkExp) { Console.WriteLine("Agent initialization error: " + sdkExp.Message); WaitForInput(); Environment.Exit(1); } // Define ionic-external-id fixed attribute. AttributesDictionary fixedKeyAttrs = new AttributesDictionary(); fixedKeyAttrs.Add("ionic-external-id", new List <string> { externalId }); // Define mutable attributes. AttributesDictionary mutableKeyAttrs = new AttributesDictionary(); // empty map CreateKeysResponse.Key key = null; // Create single key with fixed attributes 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); }
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(); }
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); }