PHP code example of bariew / whatsapp-crypto

1. Go to this page and download the library: Download bariew/whatsapp-crypto library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bariew / whatsapp-crypto example snippets


use GuzzleHttp\Psr7\Stream;
use bariew\WhatsAppCrypto\Factory\EncryptedStreamFactory;
use bariew\WhatsAppCrypto\Enum\MediaType;

// Your 32-byte mediaKey (raw binary)
$mediaKey = random_bytes(32);

// Open the source file and encrypted output
$sourceStream = fopen('/path/to/original/file.jpg', 'rb');
$encryptedStream = fopen('/path/to/encrypted/file.encrypted', 'wb');

$encryptedPsr7Stream = new Stream($encryptedStream);

// Create encrypted stream
$encryptor = new EncryptedStreamFactory();
$encrypted = $encryptor->create($encryptedPsr7Stream, $mediaKey, MediaType::IMAGE);

// Encrypt the file
$encrypted->readAndWrite(new Stream($sourceStream));

use GuzzleHttp\Psr7\Stream;
use bariew\WhatsAppCrypto\Factory\DecryptedStreamFactory;
use bariew\WhatsAppCrypto\Enum\MediaType;

// Your 32-byte mediaKey (raw binary)
$mediaKey = file_get_contents('/path/to/file.key');
$decryptedStream = fopen('/path/to/decrypted/file.jpg', 'wb');

// Create decrypted stream
$decryptor = new DecryptedStreamFactory();
$decrypted = $decryptor->createFromFile('/path/to/encrypted/file.encrypted', $mediaKey, MediaType::IMAGE);

// Decrypt the file
while (!$decrypted->eof()) {
    fwrite($decryptedStream, $decrypted->read(8192));
}
$decrypted->close();