Sha256: 54263c95d631c233a4fe015905b02709636bc33b01d407fac86cd1f70cc8f0f0

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

#####################################################
# 消息加密解密
# Created by zhangmingxin
# Date: 2018-05-18
# Wechat number: zmx119966
####################################################

class WxApi
  module Aes
    # 解密
    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

    # 加密
    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
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wxapi-1.0.1 lib/wxapi/aes.rb
wxapi-1.0.0 lib/wxapi/aes.rb