收藏
回答

刚login 换回来的值 服务端解密手机号失败

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug 服务端调用微信接口 微信iOS客户端 8.0.6 2.17.0

appid wx3f1a286c0eda961d

回答关注问题邀请回答
收藏

1 个回答

  • ؞咬了你؞F࿆ū࿆t࿆ū࿆r࿆ē࿆
    ؞咬了你؞F࿆ū࿆t࿆ū࿆r࿆ē࿆
    2021-06-08

    确定小程序没有多次login?

    wx.login 获取 code

    通过code2session获取session

    小程序用户按钮授权获得encryptedData,iv提交的服务器

    服务器通过AES/CBC/PKCS5Padding,session进行base64解码作为密钥

    iv进行base64解码后作为向量

    encryptedData进行base64解码后输入就能获取明文

    建议你把解密过程的代码贴一贴

    2021-06-08
    有用
    回复 3
    • 莫唐
      莫唐
      2021-06-10
      package cn.binarywang.wx.miniapp.util.crypt;


      import java.nio.charset.Charset;
      import java.nio.charset.StandardCharsets;
      import java.security.AlgorithmParameters;
      import java.security.Key;
      import java.security.Security;
      import java.util.Arrays;
      import javax.crypto.Cipher;
      import javax.crypto.spec.IvParameterSpec;
      import javax.crypto.spec.SecretKeySpec;


      import com.google.common.base.CharMatcher;
      import com.google.common.io.BaseEncoding;
      import org.apache.commons.codec.binary.Base64;
      import org.bouncycastle.jce.provider.BouncyCastleProvider;


      import cn.binarywang.wx.miniapp.config.WxMaConfig;
      import me.chanjar.weixin.common.util.crypto.PKCS7Encoder;


      /**
       * @author <a href="https://github.com/binarywang">Binary Wang</a>
       */
      public class WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCryptUtil {
        private static final Charset UTF_8 = StandardCharsets.UTF_8;


        public WxMaCryptUtils(WxMaConfig config) {
          this.appidOrCorpid = config.getAppid();
          this.token = config.getToken();
          this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(config.getAesKey()));
        }


        /**
         * AES解密.
         *
         * @param sessionKey session_key
         * @param encryptedData 消息密文
         * @param ivStr iv字符串
         */
        public static String decrypt(String sessionKey, String encryptedData, String ivStr) {
          try {
            AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
            params.init(new IvParameterSpec(Base64.decodeBase64(ivStr)));


            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), params);


            return new String(PKCS7Encoder.decode(cipher.doFinal(Base64.decodeBase64(encryptedData))), UTF_8);
          } catch (Exception e) {
            throw new RuntimeException("AES解密失败!", e);
          }
        }




        /**
         * AES解密.
         *
         * @param sessionKey session_key
         * @param encryptedData 消息密文
         * @param ivStr iv字符串
         */
        public static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr) {
          byte[] keyBytes = Base64.decodeBase64(sessionKey.getBytes(UTF_8));


          int base = 16;
          if (keyBytes.length % base != 0) {
            int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
            byte[] temp = new byte[groups * base];
            Arrays.fill(temp, (byte) 0);
            System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
            keyBytes = temp;
          }


          Security.addProvider(new BouncyCastleProvider());
          Key key = new SecretKeySpec(keyBytes, "AES");
          try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decodeBase64(ivStr.getBytes(UTF_8))));
            return new String(cipher.doFinal(Base64.decodeBase64(encryptedData.getBytes(UTF_8))), UTF_8);
          } catch (Exception e) {
            throw new RuntimeException("AES解密失败!", e);
          }
        }


      }
      2021-06-10
      回复
    • 莫唐
      莫唐
      2021-06-10
      在login后 会同步到后端最新的code 所以解密拿到的前端值一定是最新的
      2021-06-10
      回复
    • ؞咬了你؞F࿆ū࿆t࿆ū࿆r࿆ē࿆
      ؞咬了你؞F࿆ū࿆t࿆ū࿆r࿆ē࿆
      2021-06-16回复莫唐
      具体报了什么错,无限制的jce的policy有没有安装
      2021-06-16
      回复
登录 后发表内容