Azure Key Vault: Create an RSA or EC Key
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.
Set key_name, key_type (RSA or EC), and key_purpose (sign or encrypt). Creates software-protected RSA-2048 or EC P-256 with restricted key_ops; key_ops is not a permission grant. Requires keys/create. An existing name produces another version. Keep the returned versioned kid. These are vault keys, not certificates or Managed HSM keys.
Chilkat Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
config: HCkJsonObject;
stringMatch1: Boolean;
validate1: HCkStringBuilder;
alphabet1: HCkStringBuilder;
charIndex1: Integer;
vaultName: HCkStringBuilder;
letters: HCkStringBuilder;
origin: HCkStringBuilder;
http: HCkHttp;
request: HCkHttpRequest;
response: HCkHttpResponse;
result: HCkJsonObject;
token: HCkJsonObject;
stringMatch2: Boolean;
stringMatch3: Boolean;
expires: HCkDateTime;
now: HCkDateTime;
stringMatch4: Boolean;
validate23: HCkStringBuilder;
alphabet23: HCkStringBuilder;
charIndex23: Integer;
resourceBase: HCkStringBuilder;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
stringMatch8: Boolean;
stringMatch9: Boolean;
stringMatch10: Boolean;
stringMatch11: Boolean;
stringMatch12: Boolean;
body: HCkJsonObject;
stringMatch13: Boolean;
stringMatch14: Boolean;
operationUrl: HCkStringBuilder;
stringMatch15: Boolean;
returnedVersion24Prefix: HCkStringBuilder;
returnedVersion24Id: HCkStringBuilder;
returnedVersion24: PWideChar;
validate25: HCkStringBuilder;
alphabet25: HCkStringBuilder;
charIndex25: Integer;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this configuration private, outside public web content and source control.
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/keyvault.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private Key Vault configuration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'vault_name'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'vault_name') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid vault_name.');
Exit;
end;
validate1 := CkStringBuilder_Create();
alphabet1 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate1,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate vault_name.');
Exit;
end;
success := CkStringBuilder_Append(alphabet1,'abcdefghijklmnopqrstuvwxyz0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate1) < 3) or (CkStringBuilder_getLength(validate1) > 24)) then
begin
Memo1.Lines.Add('Invalid vault_name length.');
Exit;
end;
charIndex1 := 0;
while (charIndex1 < CkStringBuilder_getLength(validate1)) do
begin
if (CkStringBuilder_Contains(alphabet1,CkStringBuilder__getRange(validate1,charIndex1,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in vault_name.');
Exit;
end;
charIndex1 := charIndex1 + 1;
end;
vaultName := CkStringBuilder_Create();
CkStringBuilder_Append(vaultName,CkJsonObject__stringOf(config,'vault_name'));
letters := CkStringBuilder_Create();
CkStringBuilder_Append(letters,'abcdefghijklmnopqrstuvwxyz');
if ((CkStringBuilder_Contains(letters,CkStringBuilder__getRange(vaultName,0,1,False),True) <> True) or (CkStringBuilder_EndsWith(vaultName,'-',True) = True) or (CkStringBuilder_Contains(vaultName,'--',True) = True)) then
begin
Memo1.Lines.Add('Invalid vault_name.');
Exit;
end;
origin := CkStringBuilder_Create();
success := CkStringBuilder_Append(origin,'https://');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(origin,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(origin,'.vault.azure.net');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
request := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(request,'GET');
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
// First run Get a Key Vault access token.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/azureKeyVaultToken.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the private token file.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'access_token'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_token.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(token,'expires_at'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(token,'expires_at') <> 1) or (stringMatch3 = True)) then
begin
Memo1.Lines.Add('Missing or invalid expires_at.');
Exit;
end;
expires := CkDateTime_Create();
now := CkDateTime_Create();
success := CkDateTime_SetFromTimestamp(expires,CkJsonObject__stringOf(token,'expires_at'));
if (success <> True) then
begin
Memo1.Lines.Add('Invalid token expiration.');
Exit;
end;
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_name'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'key_name') <> 1) or (stringMatch4 = True)) then
begin
Memo1.Lines.Add('Missing or invalid key_name.');
Exit;
end;
validate23 := CkStringBuilder_Create();
alphabet23 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate23,CkJsonObject__stringOf(config,'key_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate key_name.');
Exit;
end;
success := CkStringBuilder_Append(alphabet23,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate23) < 1) or (CkStringBuilder_getLength(validate23) > 127)) then
begin
Memo1.Lines.Add('Invalid key_name length.');
Exit;
end;
charIndex23 := 0;
while (charIndex23 < CkStringBuilder_getLength(validate23)) do
begin
if (CkStringBuilder_Contains(alphabet23,CkStringBuilder__getRange(validate23,charIndex23,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in key_name.');
Exit;
end;
charIndex23 := charIndex23 + 1;
end;
resourceBase := CkStringBuilder_Create();
success := CkStringBuilder_Append(resourceBase,CkStringBuilder__getAsString(origin));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(resourceBase,'/keys/');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(resourceBase,CkJsonObject__stringOf(config,'key_name'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_type'));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'key_type') <> 1) or (stringMatch5 = True)) then
begin
Memo1.Lines.Add('Missing or invalid key_type.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_purpose'));
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'key_purpose') <> 1) or (stringMatch6 = True)) then
begin
Memo1.Lines.Add('Missing or invalid key_purpose.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_type'));
stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'RSA',True);
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_type'));
stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'EC',True);
if ((stringMatch7 <> True) and (stringMatch8 <> True)) then
begin
Memo1.Lines.Add('Choose RSA or EC.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_purpose'));
stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'sign',True);
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_purpose'));
stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'encrypt',True);
if ((stringMatch9 <> True) and (stringMatch10 <> True)) then
begin
Memo1.Lines.Add('Choose sign or encrypt.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_type'));
stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'EC',True);
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_purpose'));
stringMatch12 := CkStringBuilder_ContentsEqual(stringCompare,'sign',True);
if ((stringMatch11 = True) and (stringMatch12 <> True)) then
begin
Memo1.Lines.Add('This EC example supports signing only.');
Exit;
end;
body := CkJsonObject_Create();
success := CkJsonObject_UpdateString(body,'kty',CkJsonObject__stringOf(config,'key_type'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set kty.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_type'));
stringMatch13 := CkStringBuilder_ContentsEqual(stringCompare,'RSA',True);
if (stringMatch13 = True) then
begin
success := CkJsonObject_UpdateInt(body,'key_size',2048);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key size.');
Exit;
end;
end
else
begin
success := CkJsonObject_UpdateString(body,'crv','P-256');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set crv.');
Exit;
end;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'key_purpose'));
stringMatch14 := CkStringBuilder_ContentsEqual(stringCompare,'sign',True);
if (stringMatch14 = True) then
begin
success := CkJsonObject_UpdateString(body,'key_ops[0]','sign');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_ops[0].');
Exit;
end;
success := CkJsonObject_UpdateString(body,'key_ops[1]','verify');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_ops[1].');
Exit;
end;
end
else
begin
success := CkJsonObject_UpdateString(body,'key_ops[0]','encrypt');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_ops[0].');
Exit;
end;
success := CkJsonObject_UpdateString(body,'key_ops[1]','decrypt');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_ops[1].');
Exit;
end;
success := CkJsonObject_UpdateString(body,'key_ops[2]','wrapKey');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_ops[2].');
Exit;
end;
success := CkJsonObject_UpdateString(body,'key_ops[3]','unwrapKey');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set key_ops[3].');
Exit;
end;
end;
operationUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(operationUrl,CkStringBuilder__getAsString(resourceBase));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(operationUrl,'/create?api-version=2026-04-01');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Token is expired or near expiration. Obtain a new token before continuing.');
Exit;
end;
success := CkHttp_HttpJson(http,'POST',CkStringBuilder__getAsString(operationUrl),body,'application/json',response);
if (success <> True) then
begin
Memo1.Lines.Add('Request failed; reconcile uncertain writes before retrying.');
Exit;
end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
begin
Memo1.Lines.Add('Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Invalid JSON response.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'key.kid'));
stringMatch15 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'key.kid') <> 1) or (stringMatch15 = True)) then
begin
Memo1.Lines.Add('Missing or invalid key.kid.');
Exit;
end;
returnedVersion24Prefix := CkStringBuilder_Create();
success := CkStringBuilder_Append(returnedVersion24Prefix,CkStringBuilder__getAsString(resourceBase));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
success := CkStringBuilder_Append(returnedVersion24Prefix,'/');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
returnedVersion24Id := CkStringBuilder_Create();
success := CkStringBuilder_Append(returnedVersion24Id,CkJsonObject__stringOf(result,'key.kid'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct URL or identifier.');
Exit;
end;
if (CkStringBuilder_StartsWith(returnedVersion24Id,CkStringBuilder__getAsString(returnedVersion24Prefix),True) <> True) then
begin
Memo1.Lines.Add('Returned identifier is outside the expected vault/resource.');
Exit;
end;
returnedVersion24 := CkStringBuilder__getRange(returnedVersion24Id,CkStringBuilder_getLength(returnedVersion24Prefix),CkStringBuilder_getLength(returnedVersion24Id) - CkStringBuilder_getLength(returnedVersion24Prefix),False);
validate25 := CkStringBuilder_Create();
alphabet25 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate25,returnedVersion24);
if (success <> True) then
begin
Memo1.Lines.Add('Could not validate resource identifier.');
Exit;
end;
success := CkStringBuilder_Append(alphabet25,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
begin
Memo1.Lines.Add('Could not initialize validation.');
Exit;
end;
if ((CkStringBuilder_getLength(validate25) < 1) or (CkStringBuilder_getLength(validate25) > 127)) then
begin
Memo1.Lines.Add('Invalid resource identifier length.');
Exit;
end;
charIndex25 := 0;
while (charIndex25 < CkStringBuilder_getLength(validate25)) do
begin
if (CkStringBuilder_Contains(alphabet25,CkStringBuilder__getRange(validate25,charIndex25,1,False),True) <> True) then
begin
Memo1.Lines.Add('Invalid character in resource identifier.');
Exit;
end;
charIndex25 := charIndex25 + 1;
end;
Memo1.Lines.Add('Created key version: ' + CkJsonObject__stringOf(result,'key.kid'));
Memo1.Lines.Add('Example completed.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(validate1);
CkStringBuilder_Dispose(alphabet1);
CkStringBuilder_Dispose(vaultName);
CkStringBuilder_Dispose(letters);
CkStringBuilder_Dispose(origin);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(request);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(token);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkStringBuilder_Dispose(validate23);
CkStringBuilder_Dispose(alphabet23);
CkStringBuilder_Dispose(resourceBase);
CkJsonObject_Dispose(body);
CkStringBuilder_Dispose(operationUrl);
CkStringBuilder_Dispose(returnedVersion24Prefix);
CkStringBuilder_Dispose(returnedVersion24Id);
CkStringBuilder_Dispose(validate25);
CkStringBuilder_Dispose(alphabet25);