decryptJWE function
Decrypts the provided jweCompact
using the specified key
.
Returns the decrypted payload as a string.
Implementation
Future<String> decryptJWE(String jweCompact, Map<String, dynamic> key) async {
var split = jweCompact.split(".");
if (split[0].length % 4 != 0) {
var paddingLength = 4 - (split[0].length % 4);
split[0] += "=" * paddingLength;
}
String jsonString = utf8.decode(base64Url.decode(split[0]));
var jsonObject = json.decode(jsonString);
if (jsonObject["alg"] == "ECDH-ES") {
jsonObject["alg"] = "dir";
split[0] = base64Url.encode(utf8.encode(json.encode(jsonObject)));
}
var jweString = split.join(".");
var keyJwk = JsonWebKey.fromJson(key);
var jwe = JsonWebEncryption.fromCompactSerialization(jweString);
var keyStore = JsonWebKeyStore()..addKey(keyJwk);
var payload = await jwe.getPayload(keyStore);
return payload.stringContent;
}