NAME

Net::BitTorrent::Protocol::MSE::KeyExchange - Diffie-Hellman Exchange for MSE

SYNOPSIS

use Net::BitTorrent::Protocol::MSE::KeyExchange;

my $kx = Net::BitTorrent::Protocol::MSE::KeyExchange->new(
    infohash    => $bin_ih,
    is_initiator => 1
);

# Share this with the peer
my $my_pub = $kx->pub_key;

# Compute shared secret once peer's key arrives
$kx->compute_secret( $remote_pub );

# Get ready-to-use ciphers
my $cipher = $kx->encrypt_rc4;
$cipher->crypt( $plaintext );

DESCRIPTION

Net::BitTorrent::Protocol::MSE::KeyExchange handles the low level Diffie-Hellman math and cipher initialization required for Message Stream Encryption (MSE/PE).

It uses a fixed 768-bit safe prime (P) and generator (G=2) as defined in the MSE specification.

METHODS

compute_secret( $remote_public_key )

Computes the shared Diffie-Hellman secret and derives the RC4 encryption and decryption keys using the infohash as a salt.

This method automatically discards the first 1024 bytes of both RC4 keystreams to mitigate known weaknesses in the RC4 cipher's initial output (a requirement of the MSE spec).

pub_key( )

Returns the raw binary representation of the local public key (128 bytes).

encrypt_rc4( ) / decrypt_rc4( )

Returns instances of Crypt::Stream::RC4 correctly seeded and ready for payload obfuscation.

shared_secret( )

Returns the raw binary shared secret derived from the exchange.

AUTHOR

Sanko Robinson <sanko@cpan.org>

COPYRIGHT

Copyright (C) 2008-2026 by Sanko Robinson.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.