Skip to content

Instantly share code, notes, and snippets.

@samngms
Last active September 12, 2018 04:45
Show Gist options
  • Save samngms/19fa8dae6b25544e6a317cb9ceda1d61 to your computer and use it in GitHub Desktop.
Save samngms/19fa8dae6b25544e6a317cb9ceda1d61 to your computer and use it in GitHub Desktop.
Code sample for Azure KeyVault
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.models.KeyOperationResult;
import com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm;
public class MyKeyVaultTest {
public static String CLIENT_ID = "<<your application id>>";
public static String DOMAIN_ID = "<<your orgnization id>>";
public static String SECRET = "<<your secret>>";
public static String BASE_URL = "https://vault-01.vault.azure.net/";
public static String KEY_NAME = "key-01";
public static String KEY_VERSION = "4fec6c8eef9d4ee986f238249046016f";
public static void main(String[] args) throws Exception {
ApplicationTokenCredentials token =
new ApplicationTokenCredentials(CLIENT_ID, DOMAIN_ID, SECRET, AzureEnvironment.AZURE);
KeyVaultClient client = new KeyVaultClient(token);
String message = "HelloWorld, Good Morning";
KeyOperationResult result1 = client.encrypt(BASE_URL, KEY_NAME, KEY_VERSION,
JsonWebKeyEncryptionAlgorithm.RSA1_5, message.getBytes("UTF-8"));
KeyOperationResult result2 = client.decrypt(BASE_URL, KEY_NAME, KEY_VERSION,
JsonWebKeyEncryptionAlgorithm.RSA1_5, result1.result());
System.out.println("Decrypted data: " + new String(result2.result(), "UTF-8"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment