Class: Steam::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/steam/crypto.rb

Overview

Handles Steam AES key encryption and decrptyion of raw packet data.

Constant Summary collapse

KEY =

The Steam public key

'MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDf7BrWLBBmLBc1OhSwfFkRf53'\
'T2Ct64+AVzRkeRuh7h3SiGEYxqQMUeYKO6UWiSRKpI2hzic9pobFhRr3Bvr/WARvY'\
'gdTckPv+T1JzZsuVcNfFjrocejN1oWI0Rrtgt4Bo+hOneoo3S57G9F1fOpn5nsQ66'\
'WOiu4gZKODnFMBCiQIBEQ=='

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decrypt(data, key) ⇒ Object

Decrypt a given string with a given key, get the decrypted data back.

Parameters:

  • data (String)

    the data to decrypt

  • key (String)

    the key to decrypt the data with



27
28
29
# File 'lib/steam/crypto.rb', line 27

def self.decrypt(data, key)
  new.decrypt(StringIO.new(data), key).string
end

.encrypt(data, key) ⇒ Object

Encrypt a given string with a given key, get the encrypted data back.

Parameters:

  • data (String)

    the data to encrypt

  • key (String)

    the key to encrypt the data with



19
20
21
# File 'lib/steam/crypto.rb', line 19

def self.encrypt(data, key)
  new.encrypt(StringIO.new(data), key).string
end

Instance Method Details

#decrypt(io, key) ⇒ :read

Decrypts the given IO stream using the given key

Examples:

Encrypting data

crypto = Crypto.new
crypto.decrypt(StringIO.new('data'), 'key')

Parameters:

  • io (#read)

    The IO object to encrypt

  • key (String)

    The key

Returns:

  • (:read)

    an IO object represeting the decrypted data



75
76
77
78
79
80
81
82
83
# File 'lib/steam/crypto.rb', line 75

def decrypt(io, key)
  iv_cipher = iv_cipher(key, :decrypt)
  crypted_iv = io.read(16)
  iv = iv_cipher.update(crypted_iv) + iv_cipher.final

  cipher = data_cipher(key, iv, :decrypt)

  StringIO.new(cipher.update(io.read) + cipher.final)
end

#encrypt(io, key) ⇒ :read

Encrypts the given IO stream using the given key

Examples:

Encrypting data

crypto = Crypto.new
crypto.encrypt('data', 'key')

Parameters:

  • io (#read)

    The IO object to encrypt

  • key (String)

    The key

Returns:

  • (:read)

    an IO object represeting the encrypted data



56
57
58
59
60
61
62
63
64
# File 'lib/steam/crypto.rb', line 56

def encrypt(io, key)
  iv = Random.new.bytes(16)

  iv_cipher = iv_cipher(key, :encrypt)
  cipher    = data_cipher(key, iv, :encrypt)

  StringIO.new(iv_cipher.update(iv) + iv_cipher.final +
               cipher.update(io.read) + cipher.final)
end

#generate_keyArray

Generates a tuple representing the session key, both plain text and encrypted

Examples:

Generating a key

crypto = Cyrpto.new
crypted_key, key = crypto.generate_key

Returns:

  • (Array)

    the crypted_key, plain_key



39
40
41
42
43
44
45
# File 'lib/steam/crypto.rb', line 39

def generate_key
  key   = OpenSSL::PKey::RSA.new(Base64.strict_decode64(KEY))
  plain = Random.new.bytes(32)
  padding = OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING

  [key.public_encrypt(plain, padding), plain]
end

#session_key_crc(key) ⇒ Integer

Get the 32 bit crc for the given key

Parameters:

Returns:

  • (Integer)

    the crc



89
90
91
# File 'lib/steam/crypto.rb', line 89

def session_key_crc(key)
  Zlib.crc32(key)
end