pusher / pusher-websocket-dotnet

Pusher Channels Client Library for .NET
MIT License
111 stars 113 forks source link

PusherClient 2.1.0 doesn't decrypts messages #125

Closed ederjbezerra closed 2 years ago

ederjbezerra commented 2 years ago

I'm used to implementing pusher client with javascript and having messages decrypted automatically, what we need is to just configure an auth point that authorizes connection and provides a master encryption key. With DotNet I'm experiencing issues, what seems to me is that this approach doesn't work with dotnet pusher client(I'm using Xamarin).

Trying to solve this issue, I have searched and found this code at github.com/pusher/pusher-websocket-dotnet/blob/master/PusherClient/ChannelDataDecrypter.cs but I'm still not achieving to decrypt, It fails and I keep getting Decryption failed for channel.

I'm sure the key I'm passing to DecryptData() is the same of the one at auth endpoint, and to be more specific, I will provide it, plus the relevant part of the code that suppostly should decrypt:

This is the json (json_event.data):

{"data":{"nonce":"Cr3K2CDnVXH1s65jtQpjorV8PnSvy+At","ciphertext":"R+Bg+aJ1VciAGPdhqVH7vB/NjBzsVLcBjLU1Q8LSA5Czd34DLyJaaVpsQCexYt/bYtUo78e0VY2U5lfv7vFna2NGOq7T+6yE0uj9xlvQSHzpfavErQ1XMaZ6LXoFqZakv786BLZ9BsW2jGA4U4+iBzDA1/kPfeJ15w1qbLink8AihtgNAo0v65YUJpmomWlAHwucFCP+psYmPzhk9Sci9RvLxjKIhq13B/G9urSU+EF990Ox05oqGt3+aoIM7XYnqSw69uqskXJE0WOLUbZYrZNbFgoVuZ8BzoUm82OpBCZVSxtlplFFpKA9oz3mh/jOQg7i8jVftLkgF9Ci2pNmjGsKoxuexTp8suwXLygD7Qkffj/cwdEYPglJ+EebCm/FItnL+Lt/ZsXJaAaeSbsGzsDU+gXNbgSwbRgXt38AI/evllhz8g51mhz6W056ON3IKPzpEgiEc5WhcsGb2tNSzpk4rxN41ZCQ3c/TnXXCMySfL0rh54zTAKXfSMWaOXRXAcUgNyD2NnPYgbtLQ6+QCtYRZITxdETqQqyFpDF0TbJueK5eG3CJUCHOoT0WyOlMs1Ry8XEDLA6Mk4M+Xed917b14imgM4lfOiU+o/tp0sg/x271EpMciKbZ8TXNhN+yCJgR2vLJZlnJ5CT7kxC7TaNaap0XBp9h4Hdu4FTevlIu0XLxd9o3EMht1o0COcpvcBO6nZmQZHgDJ/0jJSkSK6BnCEU2snIHqMJUSjtKl5AMvUISjLN+l/CS7T6FIObtSmlDLRVkFBY4IaYDrDfnsxM+b9Zq4vSVT5+nhnbLg+NrFgwjzQQbBi5GObIPxjxhm4HZ9A8nwBmNM8DWQe1o/OZP3XFqGpYYzpx8+05OW1zBB2gk+1lhHlZdA+dFJa9dko83j5Q2BEZc00HL"}}

string key = "60e4a9540aa77099695ca4aa4e5a746f";  //got it from auth endpoint server logs
string msgDecrypted = channelDataDecrypter.DecryptData(System.Text.Encoding.ASCII.GetBytes(key), json_event.data);

This is how I'm generating the key at server side(PHP):

require_once 'pusher/autoload.php';
require_once('pem_files/chat_privatekey_salt.php');
$master_key=md5(base64_encode(openssl_digest($salt.$_POST['channel_name'],'SHA256', true))); 

//using md5 because pusher validator was complaining about the key length and saying that it must have 32 chars, but I think this is not the point since decryption works in javascript client using this very same key.

$options = array('cluster' => 'us2','useTLS' => true,'encrypted' => true,'encryption_master_key'=>$master_key);
$pusher = new Pusher\Pusher('xxxxxxxx','yyyyyyyy','0000000',$options);
echo $pusher->socket_auth($_POST['channel_name'],$_POST['socket_id']);

As I said before, this fails with Decryption failed for channel. What can I do to fix this?

benjamin-tang-pusher commented 2 years ago

Fixed for ederjbezerra, just needed to read the incoming PusherEvent correctly:

MyPrivateChannel.Bind("chat", (PusherEvent eventData) => { ChatMessage data = JsonConvert.DeserializeObject(eventData.Data); Console.WriteLine($"{data.Message}"); });