Microsoft graph api decrypt change notification (php)

Microsoft graph api decrypt change notification (php)
0

Upvotes

Upvote

 Nov 26 2023
0 Comments 
New

Hello my website is in PHP languge. I am not able to decrypt change notification (php). Current language availble C#, Java and Javascript. 
For encryption using
https://learn.microsoft.com/en-us/graph/changenotifications-for-onlinemeeting
And decryption
https://learn.microsoft.com/en-us/graph/webhooks-with-resource-data?tabs=csharp
How can I do in PHP

1) Decrypt the symmetric key

// Initialize with the private key that matches the encryptionCertificateId. RSACryptoServiceProvider rsaProvider = ...; byte[] encryptedSymmetricKey = Convert.FromBase64String(<value from dataKey property>); // Decrypt using OAEP padding. byte[] decryptedSymmetricKey = rsaProvider.Decrypt(encryptedSymmetricKey, fOAEP: true); // Can now use decryptedSymmetricKey with the AES algorithm.

 

2)Compare data signature using HMAC-SHA256

byte[] decryptedSymmetricKey = <the aes key decrypted in the previous step>;
byte[] encryptedPayload = <the value from the data property, still encrypted>;
byte[] expectedSignature = <the value from the dataSignature property>;
byte[] actualSignature;

using (HMACSHA256 hmac = new HMACSHA256(decryptedSymmetricKey))
{
actualSignature = hmac.ComputeHash(encryptedPayload);
}
if (actualSignature.SequenceEqual(expectedSignature))
{
// Continue with decryption of the encryptedPayload.
}
else
{
// Do not attempt to decrypt encryptedPayload. Assume notification payload has been tampered with and investigate.
}
3.Decrypt the resource data content

AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider();
aesProvider.Key = decryptedSymmetricKey;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.Mode = CipherMode.CBC;

// Obtain the intialization vector from the symmetric key itself.
int vectorSize = 16;
byte[] iv = new byte[vectorSize];
Array.Copy(decryptedSymmetricKey, iv, vectorSize);
aesProvider.IV = iv;

byte[] encryptedPayload = Convert.FromBase64String(<value from data property>);

string decryptedResourceData;
// Decrypt the resource data content.
using (var decryptor = aesProvider.CreateDecryptor())
{
using (MemoryStream msDecrypt = new MemoryStream(encryptedPayload))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decryptedResourceData = srDecrypt.ReadToEnd();
}
}
}
}

// decryptedResourceData now contains a JSON string that represents the resource.

 

PLease collaborate me