Module: WechatPublicApi::Aes

Included in:
WechatPublicApi
Defined in:
lib/wechat_public_api/aes.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(key, dicrypted_string) ⇒ Object

解密



11
12
13
14
15
16
17
18
# File 'lib/wechat_public_api/aes.rb', line 11

def decrypt(key, dicrypted_string)
  cipher = OpenSSL::Cipher::AES.new(256, :CBC)
  cipher.decrypt
  cipher.key = key
  cipher.iv = '0000000000000000'
  cipher.padding = 0
  cipher.update(dicrypted_string) << cipher.final
end

#encrypt(aes_key, text, app_id) ⇒ Object

加密



21
22
23
24
25
26
27
28
29
# File 'lib/wechat_public_api/aes.rb', line 21

def encrypt(aes_key, text, app_id)
  text    = text.force_encoding("ASCII-8BIT")
  random  = SecureRandom.hex(8)
  msg_len = [text.length].pack("N")
  text    = "#{random}#{msg_len}#{text}#{app_id}"
  text    = WxAuth.encode(text)
  text    = handle_cipher(:encrypt, aes_key, text)
  Base64.encode64(text)
end