Last active
March 8, 2021 02:10
-
-
Save xximjasonxx/eacdb172b24aef7f16b905bef88ce9d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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