Skip to content

Commit

Permalink
Fix Message Received with Extra Characters #164, Fix Bad Mac Issue!
Browse files Browse the repository at this point in the history
  • Loading branch information
bayrakmustafa committed Apr 7, 2016
1 parent 6e59e88 commit b689d3e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
52 changes: 39 additions & 13 deletions WhatsAppApi/Helper/AxolotlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -507,14 +509,38 @@ public string bin2HexXX(string strBin)
/// <returns></returns>
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)
Expand Down
11 changes: 6 additions & 5 deletions mylibaxolotl/SessionCipher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit b689d3e

Please sign in to comment.