I have been trying to decrypt a JWE token response that was signed with the public key of my code signing certificate. The header of the JWE is the following:
{
“alg”: “RSA-OAEP”,
“enc”: “A256GCM”
}
The private key of the code signing certificate is a RSA-HSM type, in azure key vault HSM and hence is non-exportable.
What I have done is using the key vault API to decrypt/unwrap the EncryptedKey component of the JWE, and initialized a System.Security.Cryptography.AesGcm using the decrypted/unwrapped key bytes. However upon decryption i will get the error message “The computed authentication tag did not match the input authentication tag.” Can anyone advise on this?
public async Task<string> decryptJweAsync(string response)
{
var jweToken = JweToken.FromString(response);
var keyClient = new KeyClient(new Uri(_azureKeyVault.Value.KeyVaultBaseUri), new DefaultAzureCredential());
var rsaHsmKey = await keyClient.GetKeyAsync(_myConfiguration.Value.PrivateKey);
var cryptoClient = new CryptographyClient(rsaHsmKey.Value.Id, new DefaultAzureCredential());
byte[] plaintextBytes = new byte[jweToken.Ciphertext.Length];
var cekBytes = await cryptoClient.DecryptAsync(EncryptionAlgorithm.RsaOaep, jweToken.Recipients[0].EncryptedCek);
System.Security.Cryptography.AesGcm aesGcm = new System.Security.Cryptography.AesGcm(cekBytes.Plaintext);
aesGcm.Decrypt(jweToken.Iv, jweToken.Ciphertext, jweToken.AuthTag, plaintextBytes);
var decrypted = Encoding.UTF8.GetString(plaintextBytes);
//other codes for verification below
}
user23501363 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.