-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MediaDecryption class to decrypt images, videos, audio and documents
- Loading branch information
Showing
33 changed files
with
158 additions
and
40 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
bin/main/icu/jnet/whatsjava/encryption/BinaryEncryption.class
Binary file not shown.
Binary file modified
BIN
-272 Bytes
(91%)
bin/main/icu/jnet/whatsjava/encryption/EncryptionKeys.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+123 Bytes
(100%)
bin/main/icu/jnet/whatsjava/web/WebConversationMessage.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+220 Bytes
(110%)
build/classes/java/main/icu/jnet/whatsjava/encryption/AES.class
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
build/classes/java/main/icu/jnet/whatsjava/encryption/BinaryDecoder.class
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
build/classes/java/main/icu/jnet/whatsjava/encryption/BinaryEncryption.class
Binary file not shown.
Binary file modified
BIN
-272 Bytes
(91%)
build/classes/java/main/icu/jnet/whatsjava/encryption/EncryptionKeys.class
Binary file not shown.
Binary file added
BIN
+1.74 KB
build/classes/java/main/icu/jnet/whatsjava/encryption/MediaEncryption.class
Binary file not shown.
Binary file modified
BIN
+1.25 KB
(120%)
build/classes/java/main/icu/jnet/whatsjava/helper/Utils.class
Binary file not shown.
Binary file modified
BIN
+240 Bytes
(110%)
build/classes/java/main/icu/jnet/whatsjava/web/WebImageMessage.class
Binary file not shown.
Binary file modified
BIN
+257 Bytes
(110%)
build/classes/java/main/icu/jnet/whatsjava/web/WebVideoMessage.class
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/icu/jnet/whatsjava/encryption/MediaEncryption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package icu.jnet.whatsjava.encryption; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
|
||
import icu.jnet.whatsjava.helper.Utils; | ||
|
||
public class MediaEncryption { | ||
|
||
/* | ||
* Decrypt E2E media | ||
* | ||
*/ | ||
|
||
// Depending on the media type a different info parameter is used for the HKDF function | ||
public static String MEDIA_TYPE_IMAGE = "WhatsApp Image Keys", | ||
MEDIA_TYPE_VIDEO = "WhatsApp Video Keys", | ||
MEDIA_TYPE_AUDIO = "WhatsApp Audio Keys", | ||
MEDIA_TYPE_DOCUMENT = "WhatsApp Document Keys"; | ||
|
||
|
||
public static byte[] decrypt(byte[] mediaKey, String url, String mediaType) { | ||
// Expand mediaKey to 112 bytes and add mediaInfo | ||
byte[] mediaKeyExpanded = Utils.expandUsingHKDF(mediaKey, 112, mediaType.getBytes()); | ||
|
||
byte[] iv = Arrays.copyOfRange(mediaKeyExpanded, 0, 16); | ||
byte[] cipherKey = Arrays.copyOfRange(mediaKeyExpanded, 16, 48); | ||
byte[] macKey = Arrays.copyOfRange(mediaKeyExpanded, 48, 80); | ||
// refKey mediaKeyExpanded[80:112] not used | ||
|
||
// Download encrypted media | ||
byte[] encryptedMedia = Utils.urlToEncMedia(url); | ||
|
||
if(encryptedMedia != null) { | ||
byte[] file = Arrays.copyOfRange(encryptedMedia, 0, encryptedMedia.length - 10); | ||
byte[] mac = Arrays.copyOfRange(encryptedMedia, encryptedMedia.length - 10, encryptedMedia.length); | ||
|
||
// Hmac sign message | ||
byte[] message = ByteBuffer.allocate(iv.length + file.length).put(iv).put(file).array(); | ||
|
||
// Validate macKey of mediaKeyExpanded with mac key of the encrypted media | ||
byte[] hmacSign = Utils.signHMAC(macKey, message); | ||
|
||
// Media validated | ||
if(Arrays.equals(mac, Arrays.copyOfRange(hmacSign, 0, 10))) { | ||
return AES.decrypt(file, cipherKey, iv); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters