Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Last active March 8, 2021 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/eacdb172b24aef7f16b905bef88ce9d8 to your computer and use it in GitHub Desktop.
Save xximjasonxx/eacdb172b24aef7f16b905bef88ce9d8 to your computer and use it in GitHub Desktop.
// getting the key
private KeyClient KeyClient => new KeyClient(
vaultUri: new Uri(_configuration["KeyVaultUri"]),
credential: _getCredentialService.GetKeyVaultCredentials());
public async Task<KeyVaultKey> GetEncryptionKey()
{
var keyResponse = await KeyClient.GetKeyAsync("encryption-key");
return keyResponse.Value;
}
// usage
public async Task<string> Encrypt(string rawValue)
{
var encryptionKey = await _keyVaultService.GetEncryptionKey();
var cryptoClient = new CryptographyClient(encryptionKey.Id, _getCredentialService.GetKeyVaultCredentials());
var byteData = Encoding.Unicode.GetBytes(rawValue);
var encryptResult = await cryptoClient.EncryptAsync(EncryptionAlgorithm.RsaOaep, byteData);
return Convert.ToBase64String(encryptResult.Ciphertext);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment