From 91b7648d9e33487d4c2b4313e6c52e521b186ace Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 26 Nov 2019 17:09:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/libs/wechat/weapp.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/libs/wechat/weapp.rb b/app/libs/wechat/weapp.rb index cbf26efd2..aed14128b 100644 --- a/app/libs/wechat/weapp.rb +++ b/app/libs/wechat/weapp.rb @@ -22,8 +22,16 @@ class Wechat::Weapp end def decrypt(session_key, encrypted_data, iv) - session_key = Base64.decode64(session_key).force_encoding("utf-8") - encrypted_data = Base64.decode64(encrypted_data).force_encoding("utf-8") + session_key = Base64.decode64(session_key) + encrypted_data = Base64.decode64(encrypted_data) + cd = CharDet.detect(content) + encrypted_data = + if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8 + encrypted_data.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '}) + else + encrypted_data.force_encoding('UTF-8') + end + iv = Base64.decode64(iv) cipher = OpenSSL::Cipher::AES.new(128, :CBC)