<head>
...
<scripttype="application/json"cryptokeys="">
{
"local": ['y0^r$t^ff'], // This is for your environment
"google.com": ['g00g|e$t^ff'], // This is for Google's environment
}
</script>
…
</head>
您需要使用本地环境和 Google 的公钥来加密文档密钥。包括 Google 的公钥允许 Google AMP 缓存提供您的文档。您必须实例化一个 Tink Keyset 来从其 URL 接受 Google 公钥
stringdecryptDocumentKey(stringencryptedKey,List<string>usersEntitlements,HybridDecrypthybridDecrypter){// 1. Base64 decode the input encrypted key.bytesencryptedKeyBytes=base64.decode(encryptedKey);// 2. Try to decrypt the encrypted key.bytesdecryptedKeyBytes;try{decryptedKeyBytes=hybridDecrypter.decrypt(encryptedKeyBytes,null/* contextInfo */);}catch(errore){// Decryption error occurred. Handle it how you want.LOG("Error occurred decrypting: ",e);return"";}// 3. Parse the decrypted text into a JSON object.stringdecryptedKey=newstring(decryptedKeyBytes,UTF_8);json::objectdecryptedParsedJson=JsonParser.parse(decryptedKey);// 4. Check to see if the requesting user has the entitlements specified in // the AccessRequirements section of the JSON object.for(entitlementinusersEntitlements){if(decryptedParsedJson["AccessRequirements"].contains(entitlement)){// 5. Return the document key if the user has entitlements.returndecryptedParsedJson["Key"];}}// User doesn't have correct requirements, return empty string.return"";}JsonResponsegetEntitlements(stringrequestUri){// Do normal handling of entitlements here…List<string>usersEntitlements=getUsersEntitlementInfo();// Check if request URI has "crypt" parameter.StringdocumentCrypt=requestUri.getQueryParameters().getFirst("crypt");// If URI has "crypt" param, try to decrypt it.stringdocumentKey;if(documentCrypt!=null){documentKey=decryptDocumentKey(documentCrypt,usersEntitlements,this.hybridDecrypter_);}// Construct JSON response.JsonResponseresponse=JsonResponse{signedEntitlements:getSignedEntitlements(),isReadyToPay:getIsReadyToPay(),};if(!documentKey.empty()){response.decryptedDocumentKey=documentKey;}returnresponse;}