Good day, i want to decrypt the messages i receive through the API https://api.pushbullet.com/v2/permanents/%7Biden%7D_threads but when using my E2E key in C# and generating a desencryption key it along with the iden, it indicates that the length of the message (ciphertext) is too long (2375 characters). Here is my C# code to see if I am implementing the functions correctly (keys and IDEN are censored):
public class PushbulletE2E
{
public static void Main(string[] args)
{
try
{
string jsonContent = File.ReadAllText(@"C:Users.DesktopTextJson.txt");
var jsonObj = JObject.Parse(jsonContent);
string encryptedMessage = jsonObj["ciphertext"].ToString();
string iden = "example";
string e2eKey = "example";
// Derive the key using e2eKey and iden
string derivedKey = DeriveKey(e2eKey, iden);
Console.WriteLine("Derived Key: " + derivedKey);
string decryptedMessage = DecryptMessage(encryptedMessage, derivedKey);
Console.WriteLine("Decrypted Message: " + decryptedMessage);
}
catch (Exception ex)
{
Console.WriteLine("Error decrypting the message: " + ex.Message);
}
}
public static string DeriveKey(string e2eKey, string iden)
{
var salt = Encoding.UTF8.GetBytes(e2eKey + iden);
using (var rfc2898 = new Rfc2898DeriveBytes(e2eKey, salt, 30000))
{
return Convert.ToBase64String(rfc2898.GetBytes(32));
}
}
public static string DecryptMessage(string encryptedMessage, string key)
{
var keyBytes = Convert.FromBase64String(key);
Console.WriteLine("Key (Base64): " + key);
Console.WriteLine("Key (Bytes): " + BitConverter.ToString(keyBytes));
var encryptedBytes = Convert.FromBase64String(encryptedMessage);
Console.WriteLine("Encrypted Bytes Length: " + encryptedBytes.Length);
var iv = new byte[16];
Array.Copy(encryptedBytes, iv, 16);
Console.WriteLine("IV: " + BitConverter.ToString(iv));
var cipherText = new byte[encryptedBytes.Length - 16];
Array.Copy(encryptedBytes, 16, cipherText, 0, cipherText.Length);
Console.WriteLine("CipherText Length: " + cipherText.Length);
using (var aes = new AesManaged())
{
aes.Key = keyBytes;
aes.IV = iv;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
{
try
{
var decryptedBytes = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
catch (CryptographicException ce)
{
Console.WriteLine("CryptographicException: " + ce.Message);
throw;
}
}
}
}
}
Does anyone have a solution implemented in C# without using WebSockets?
Best regards!
Desencrypt Messages from Pushbullet API
Ariel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.