/// <summary> /// Encrypts a plain text and then decrypts the encrypted text /// </summary> /// <param name="keyId"> a global key identifier of the key to get </param> private static void EncryptDecrypt(KeyBundle key) { KeyOperationResult operationResult; var algorithm = inputValidator.GetEncryptionAlgorithm(); var plainText = inputValidator.GetPlainText(); string keyVersion = inputValidator.GetKeyVersion(); if (keyVersion != string.Empty) { var vaultAddress = inputValidator.GetVaultAddress(); string keyName = inputValidator.GetKeyName(true); // Encrypt the input data using the specified algorithm operationResult = keyVaultClient.EncryptAsync(vaultAddress, keyName, keyVersion, algorithm, plainText).GetAwaiter().GetResult(); } else { // If the key is not initialized get the key id from args var keyId = (key != null) ? key.Key.Kid : inputValidator.GetKeyId(); // Encrypt the input data using the specified algorithm operationResult = keyVaultClient.EncryptAsync(keyId, algorithm, plainText).GetAwaiter().GetResult(); } Console.Out.WriteLine(string.Format("The text is encrypted using key id {0} and algorithm {1}", operationResult.Kid, algorithm)); // Decrypt the encrypted data var decryptedText = keyVaultClient.DecryptAsync(operationResult.Kid, algorithm, operationResult.Result).GetAwaiter().GetResult(); Console.Out.WriteLine(string.Format("The decrypted text is {0}the same as the original key!", plainText.SequenceEqual(decryptedText.Result) ? "" : "not ")); Console.Out.WriteLine(string.Format("The decrypted text is {0}", Encoding.UTF8.GetString(decryptedText.Result))); }
/// <summary> /// Encrypts a plain text and then decrypts the encrypted text /// </summary> /// <param name="key"> key to use for the encryption & decryption operations </param> private static void EncryptDecrypt(KeyBundle key) { KeyOperationResult operationResult; var algorithm = inputValidator.GetEncryptionAlgorithm(); var plainText = inputValidator.GetPlainText(); string keyVersion = inputValidator.GetKeyVersion(); operationResult = _encrypt(key, keyVersion, algorithm, plainText); Console.Out.WriteLine(string.Format("The text is encrypted using key id {0} and algorithm {1}", operationResult.Kid, algorithm)); // Decrypt the encrypted data var decryptedText = keyVaultClient.DecryptAsync(operationResult.Kid, algorithm, operationResult.Result).GetAwaiter().GetResult(); Console.Out.WriteLine(string.Format("The decrypted text is{0}the same as the original key!", plainText.SequenceEqual(decryptedText.Result) ? " " : " not ")); Console.Out.WriteLine(string.Format("The decrypted text is: {0}", Encoding.UTF8.GetString(decryptedText.Result))); }