C# Class Universal.Torrent.Client.Encryption.EncryptedSocket

The class that handles.Message Stream Encryption for a connection
Inheritance: IEncryptor.IEncryptor
Afficher le fichier Open project: haroldma/Universal.Torrent

Protected Properties

Свойство Type Description
AsyncResult AsyncResult
CryptoProvide byte[]
CryptoSelect byte[]
InitialPayload byte[]
PadC byte[]
PadD byte[]
RemoteInitialPayload byte[]
S byte[]
Skey InfoHash
VerificationConstant byte[]

Méthodes publiques

Méthode Description
AddPayload ( byte buffer ) : void
AddPayload ( byte buffer, int offset, int count ) : void
BeginHandshake ( IConnection socket, AsyncCallback callback, object state ) : IAsyncResult

Begins the message stream encryption handshaking process

BeginHandshake ( IConnection socket, byte initialBuffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult

Begins the message stream encryption handshaking process, beginning with some data already received from the socket.

Decrypt ( byte data, int offset, int length ) : void

Decrypts some data (should only be called after onEncryptorReady)

Encrypt ( byte data, int offset, int length ) : void

Encrypts some data (should only be called after onEncryptorReady)

EncryptedSocket ( EncryptionTypes allowedEncryption ) : System
EndHandshake ( IAsyncResult result ) : void

Méthodes protégées

Méthode Description
Combine ( ) : byte[]

Concatenates several byte buffers

CreateCryptors ( string encryptionSalt, string decryptionSalt ) : void

Instantiate the cryptors with the keys: Hash(encryptionSalt, S, SKEY) for the encryptor and Hash(encryptionSalt, S, SKEY) for the decryptor. (encryptionSalt should be "keyA" if you're A, "keyB" if you're B, and reverse for decryptionSalt)

DeLen ( byte data ) : int

Converts a 2-byte big endian integer into an int (reverses operation of Len())

DoDecrypt ( byte data ) : byte[]

Decrypts some data with the RC4 encryptor used in handshaking

DoDecrypt ( byte data, int offset, int length ) : void

Decrypts some data with the RC4 decryptor used in handshaking

DoEncrypt ( byte data ) : byte[]
DoEncrypt ( byte data, int offset, int length ) : void

Encrypts some data with the RC4 encryptor used in handshaking

DoneReceiveY ( ) : void
DoneSynchronize ( ) : void
FillSynchronizeBytes ( bool succeeded, int count, object state ) : void
GeneratePad ( ) : byte[]

Returns a 0 to 512 byte 0-filled pad.

Hash ( ) : byte[]

Hash some data with SHA1

Len ( byte data ) : byte[]

Returns a 2-byte buffer with the length of data

Ready ( ) : void

Signal that the cryptor is now in a state ready to encrypt and decrypt payload data

ReceiveMessage ( byte buffer, int length, AsyncCallback callback ) : void
ReceiveY ( ) : void

Receive the first 768 bits of the transmission from the remote client, which is Y in the protocol (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")

SelectCrypto ( byte remoteCryptoBytes, bool replace ) : int

Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.

SendMessage ( byte toSend ) : void
SendY ( ) : void

Send Y to the remote client, with a random padding that is 0 to 512 bytes long (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")

SetMinCryptoAllowed ( EncryptionTypes allowedEncryption ) : void
Synchronize ( byte syncData, int syncStopPoint ) : void

Read data from the socket until the byte string in syncData is read, or until syncStopPoint is reached (in that case, there is an EncryptionError). (Either "3 A->B: HASH('req1', S)" or "4 B->A: ENCRYPT(VC)")

Private Methods

Méthode Description
DoneReceive ( bool succeeded, int count, object state ) : void
DoneSend ( bool succeeded, int count, object state ) : void
GenerateX ( ) : void

Generate a 160 bit random number for X

GenerateY ( ) : void

Calculate 2^X mod P

RandomNumber ( int max ) : int

Method Details

AddPayload() public méthode

public AddPayload ( byte buffer ) : void
buffer byte
Résultat void

AddPayload() public méthode

public AddPayload ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
Résultat void

BeginHandshake() public méthode

Begins the message stream encryption handshaking process
public BeginHandshake ( IConnection socket, AsyncCallback callback, object state ) : IAsyncResult
socket IConnection The socket to perform handshaking with
callback AsyncCallback The callback.
state object The state.
Résultat IAsyncResult

BeginHandshake() public méthode

Begins the message stream encryption handshaking process, beginning with some data already received from the socket.
public BeginHandshake ( IConnection socket, byte initialBuffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
socket IConnection The socket to perform handshaking with
initialBuffer byte Buffer containing soome data already received from the socket
offset int Offset to begin reading in initialBuffer
count int Number of bytes to read from initialBuffer
callback AsyncCallback The callback.
state object The state.
Résultat IAsyncResult

Combine() protected méthode

Concatenates several byte buffers
protected Combine ( ) : byte[]
Résultat byte[]

CreateCryptors() protected méthode

Instantiate the cryptors with the keys: Hash(encryptionSalt, S, SKEY) for the encryptor and Hash(encryptionSalt, S, SKEY) for the decryptor. (encryptionSalt should be "keyA" if you're A, "keyB" if you're B, and reverse for decryptionSalt)
protected CreateCryptors ( string encryptionSalt, string decryptionSalt ) : void
encryptionSalt string The salt to calculate the encryption key with
decryptionSalt string The salt to calculate the decryption key with
Résultat void

DeLen() protected méthode

Converts a 2-byte big endian integer into an int (reverses operation of Len())
protected DeLen ( byte data ) : int
data byte 2 byte buffer
Résultat int

Decrypt() public méthode

Decrypts some data (should only be called after onEncryptorReady)
public Decrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin decryption
length int The length.
Résultat void

DoDecrypt() protected méthode

Decrypts some data with the RC4 encryptor used in handshaking
protected DoDecrypt ( byte data ) : byte[]
data byte Buffers with the data to decrypt
Résultat byte[]

DoDecrypt() protected méthode

Decrypts some data with the RC4 decryptor used in handshaking
protected DoDecrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin decryption
length int The length.
Résultat void

DoEncrypt() protected méthode

protected DoEncrypt ( byte data ) : byte[]
data byte
Résultat byte[]

DoEncrypt() protected méthode

Encrypts some data with the RC4 encryptor used in handshaking
protected DoEncrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin encryption
length int The length.
Résultat void

DoneReceiveY() protected méthode

protected DoneReceiveY ( ) : void
Résultat void

DoneSynchronize() protected méthode

protected DoneSynchronize ( ) : void
Résultat void

Encrypt() public méthode

Encrypts some data (should only be called after onEncryptorReady)
public Encrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin encryption
length int The length.
Résultat void

EncryptedSocket() public méthode

public EncryptedSocket ( EncryptionTypes allowedEncryption ) : System
allowedEncryption EncryptionTypes
Résultat System

EndHandshake() public méthode

public EndHandshake ( IAsyncResult result ) : void
result IAsyncResult
Résultat void

FillSynchronizeBytes() protected méthode

protected FillSynchronizeBytes ( bool succeeded, int count, object state ) : void
succeeded bool
count int
state object
Résultat void

GeneratePad() protected méthode

Returns a 0 to 512 byte 0-filled pad.
protected GeneratePad ( ) : byte[]
Résultat byte[]

Hash() protected méthode

Hash some data with SHA1
protected Hash ( ) : byte[]
Résultat byte[]

Len() protected méthode

Returns a 2-byte buffer with the length of data
protected Len ( byte data ) : byte[]
data byte
Résultat byte[]

Ready() protected méthode

Signal that the cryptor is now in a state ready to encrypt and decrypt payload data
protected Ready ( ) : void
Résultat void

ReceiveMessage() protected méthode

protected ReceiveMessage ( byte buffer, int length, AsyncCallback callback ) : void
buffer byte
length int
callback AsyncCallback
Résultat void

ReceiveY() protected méthode

Receive the first 768 bits of the transmission from the remote client, which is Y in the protocol (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")
protected ReceiveY ( ) : void
Résultat void

SelectCrypto() protected méthode

Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.
protected SelectCrypto ( byte remoteCryptoBytes, bool replace ) : int
remoteCryptoBytes byte /// The cryptographic methods supported/wanted by the remote client in CryptoProvide /// format. The highest order one available will be selected ///
replace bool if set to true [replace].
Résultat int

SendMessage() protected méthode

protected SendMessage ( byte toSend ) : void
toSend byte
Résultat void

SendY() protected méthode

Send Y to the remote client, with a random padding that is 0 to 512 bytes long (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")
protected SendY ( ) : void
Résultat void

SetMinCryptoAllowed() protected méthode

protected SetMinCryptoAllowed ( EncryptionTypes allowedEncryption ) : void
allowedEncryption EncryptionTypes
Résultat void

Synchronize() protected méthode

Read data from the socket until the byte string in syncData is read, or until syncStopPoint is reached (in that case, there is an EncryptionError). (Either "3 A->B: HASH('req1', S)" or "4 B->A: ENCRYPT(VC)")
protected Synchronize ( byte syncData, int syncStopPoint ) : void
syncData byte Buffer with the data to synchronize to
syncStopPoint int /// Maximum number of bytes (measured from the total received from the socket since connection) /// to read before giving up ///
Résultat void

Property Details

AsyncResult protected_oe property

protected AsyncResult AsyncResult
Résultat AsyncResult

CryptoProvide protected_oe property

protected byte[] CryptoProvide
Résultat byte[]

CryptoSelect protected_oe property

protected byte[] CryptoSelect
Résultat byte[]

InitialPayload protected_oe property

protected byte[] InitialPayload
Résultat byte[]

PadC protected_oe property

protected byte[] PadC
Résultat byte[]

PadD protected_oe property

protected byte[] PadD
Résultat byte[]

RemoteInitialPayload protected_oe property

protected byte[] RemoteInitialPayload
Résultat byte[]

S protected_oe property

protected byte[] S
Résultat byte[]

Skey protected_oe property

protected InfoHash Skey
Résultat InfoHash

VerificationConstant protected_oe property

protected byte[] VerificationConstant
Résultat byte[]