module WechatPublicApi::Aes
Public Instance Methods
decrypt(key, dicrypted_string)
click to toggle source
解密
# 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)
click to toggle source
加密
# 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
Private Instance Methods
handle_cipher(action, aes_key, text)
click to toggle source
# File lib/wechat_public_api/aes.rb, line 32 def handle_cipher(action, aes_key, text) cipher = OpenSSL::Cipher.new('AES-256-CBC') cipher.send(action) cipher.padding = 0 cipher.key = aes_key cipher.iv = aes_key[0...16] cipher.update(text) + cipher.final end