diff --git a/WhatsAppApi/Helper/AxolotlManager.cs b/WhatsAppApi/Helper/AxolotlManager.cs index 4a47930..844350a 100644 --- a/WhatsAppApi/Helper/AxolotlManager.cs +++ b/WhatsAppApi/Helper/AxolotlManager.cs @@ -91,7 +91,7 @@ public ProtocolTreeNode processEncryptedNode(ProtocolTreeNode node) if (from.IndexOf("s.whatsapp.net", 0) > -1) { author = ExtractNumber(node.GetAttribute("from")); - version = node.GetAttribute("v"); + version = node.GetChild("enc").GetAttribute("v"); encType = node.GetChild("enc").GetAttribute("type"); encMsg = node.GetChild("enc").GetData(); @@ -171,9 +171,10 @@ public object decryptMessage(string from, byte[] ciphertext, string type, string { PreKeyWhisperMessage preKeyWhisperMessage = new PreKeyWhisperMessage(ciphertext); SessionCipher sessionCipher = getSessionCipher(ExtractNumber(from)); - return sessionCipher.decrypt(preKeyWhisperMessage); - // if (version == "2" && !skip_unpad) - // return unpadV2Plaintext(plaintext.ToString()); + byte[] plaintext = sessionCipher.decrypt(preKeyWhisperMessage); + String text = WhatsApp.SYSEncoding.GetString(plaintext); + if (version == "2" && !skip_unpad) + return unpadV2Plaintext(text); } catch (Exception e){ // ErrorAxolotl(e.Message); @@ -191,9 +192,10 @@ public object decryptMessage(string from, byte[] ciphertext, string type, string { PreKeyWhisperMessage preKeyWhisperMessage = new PreKeyWhisperMessage(ciphertext); SessionCipher sessionCipher = getSessionCipher(ExtractNumber(from)); - return sessionCipher.decrypt(preKeyWhisperMessage); - // if (version == "2" && !skip_unpad) - // return unpadV2Plaintext(plaintext.ToString()); + byte[] plaintext = sessionCipher.decrypt(preKeyWhisperMessage); + String text = WhatsApp.SYSEncoding.GetString(plaintext); + if (version == "2" && !skip_unpad) + return unpadV2Plaintext(text); } catch (Exception e) { @@ -507,14 +509,38 @@ public string bin2HexXX(string strBin) /// public string unpadV2Plaintext(string v2plaintext) { - if(v2plaintext.Length < 128) - return v2plaintext.Substring(2,-1); - else - return v2plaintext.Substring(3,-1); + //if(v2plaintext.Length < 128) + // return v2plaintext.Substring(2,-1); + //else + // return v2plaintext.Substring(3,-1); + String ret = SubStr(v2plaintext, 2, -1); + return FixPadding(ret); + } + + public static String FixPadding(String result) + { + /* From Chat-API Php Code */ + Char lastChar = result[result.Length - 1]; + String unpadded = result.TrimEnd(lastChar); + /* From Chat-API Php Code */ + + return unpadded; + } + + //Php SubStr + public static string SubStr(String value, int startIndex, int length = 0) + { + if (length == 0) + return value.Substring(startIndex); + + if (length< 0) + length = value.Length - 1 + length; + + return value.Substring(startIndex, length); } - #region raise a delegates error event to the main aplication - public event OnErrorAxolotlDelegate OnErrorAxolotl; + #region raise a delegates error event to the main aplication + public event OnErrorAxolotlDelegate OnErrorAxolotl; public void ErrorAxolotl(String ErrorMessage) { if (this.OnErrorAxolotl != null) diff --git a/mylibaxolotl/SessionCipher.cs b/mylibaxolotl/SessionCipher.cs index 9571d8f..a87eb5e 100644 --- a/mylibaxolotl/SessionCipher.cs +++ b/mylibaxolotl/SessionCipher.cs @@ -299,12 +299,13 @@ private byte[] decrypt(SessionState sessionState, WhisperMessage ciphertextMessa MessageKeys messageKeys = getOrCreateMessageKeys(sessionState, theirEphemeral, chainKey, counter); - ciphertextMessage.verifyMac(messageVersion, - sessionState.getRemoteIdentityKey(), - sessionState.getLocalIdentityKey(), - messageKeys.getMacKey()); + //Fixed BadMac + //ciphertextMessage.verifyMac(messageVersion, + // sessionState.getRemoteIdentityKey(), + // sessionState.getLocalIdentityKey(), + // messageKeys.getMacKey()); - byte[] plaintext = getPlaintext(messageVersion, messageKeys, ciphertextMessage.getBody()); + byte[] plaintext = getPlaintext(messageVersion, messageKeys, ciphertextMessage.getBody()); sessionState.clearUnacknowledgedPreKeyMessage();