Class: DecodeWechatEncrypted

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appId, sessionKey) ⇒ DecodeWechatEncrypted

Returns a new instance of DecodeWechatEncrypted.



7
8
9
10
# File 'lib/wechat_data_crypt.rb', line 7

def initialize appId, sessionKey
  self.appId = appId
  self.sessionKey = sessionKey
end

Instance Attribute Details

#appIdObject

Returns the value of attribute appId.



6
7
8
# File 'lib/wechat_data_crypt.rb', line 6

def appId
  @appId
end

#sessionKeyObject

Returns the value of attribute sessionKey.



6
7
8
# File 'lib/wechat_data_crypt.rb', line 6

def sessionKey
  @sessionKey
end

Instance Method Details

#decryptData(encryptedData, iv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wechat_data_crypt.rb', line 12

def decryptData encryptedData, iv
  sessionKey = Base64.decode64(self.sessionKey)
  encryptedData = Base64.decode64(encryptedData)
  iv = Base64.decode64(iv)
  cipher = OpenSSL::Cipher.new("AES-128-CBC")
  cipher.decrypt
  cipher.key = sessionKey
  cipher.iv = iv

  result = cipher.update(encryptedData) + cipher.final
  JSON.parse result
end