-5:signature verification failed

qq登陆sig错误问题-5:signature verification failed 生成sig

本文章为原创文章,转载请标明出处,有问题敬请联系我。我的账号有联系方式

问题原因:由于qq获取信息时,需要根据数据动态生成sig,由于在生成sig时会一个不小心就生成错误的sig

以获取qq用户信息为例,有两个步骤:
- qq登陆
- 获取信息

在获取信息时可能会遇到-5:signature verification failed 问题
现在来生成在sig,我会提出一些要注意的地方从而生成正确的sig

生成四个的步骤:
- Step 1. 构造源串
- * 第1步:将请求的URI路径进行URL编码,*
- * 第2步:将除“sig”外的所有参数按key进行字典升序排列*
- * 第3步:将第2步中排序后的参数(key=value)用&拼接起来*
- * 第4步:将HTTP请求方式,第1步以及第3步中的到的字符串用&拼接起来,得到源串*
- Step 2. 构造密钥
- Step 3. 生成签名值

代码案例:

import android.util.Base64;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by ancode on 2017/9/15.
 */

public class CreatSignature_HMAC_SHA1 {
    private static String sig;
    private static String source;//被加密的原串
    private static String key;//用于加密

    public static String getSig(String openid, String openkey) {
        String tempuri;
        String format = "json";
        String pf = "qzone";
        String userip = "10.0.0.1";
//        String mopenid = "";
//        String mopenkey = "";
        try {
    /*
     *   Step 1. 构造源串
     *
     */

//        第1步:将请求的URI路径进行URL编码,得到: %2Fv3%2Fuser%2Fget_info

            tempuri = URLEncoder.encode(QQConstants.GET_USER_INFO, "UTF-8");
            Log.d("showhmacsha1", tempuri);


//        第2步:将除“sig”外的所有参数按key进行字典升序排列,排列结果为:appid,format,openid,openkey,pf,userip

//        第3步:将第2步中排序后的参数(key=value)用&拼接起来:
//        appid=123456&format=json&openid=11111111111111111&openkey=2222222222222222&pf=qzone&userip=112.90.139.30
//        然后进行URL编码( 编码时请关注URL编码注意事项,否则容易导致后面签名不能通过验证),编码结果为:
//        appid%3D123456%26format%3Djson%26openid%3D11111111111111111%26openkey%3D2222222222222222%26pf%3Dqzone%26
//        userip%3D112.90.139.30
            String tempStr = "appid=" + QQConstants.QQ_APP_ID + "&" + "format=" + format + "&" + "openid=" + openid + "&" + "openkey=" + openkey + "&" + "pf=" + pf + "&" + "userip=" + userip;
            Log.d("showhmacsha1", tempStr);
            tempStr = URLEncoder.encode(tempStr, "UTF-8");
            Log.d("showhmacsha1", tempStr);

//        第4步:将HTTP请求方式,第1步以及第3步中的到的字符串用&拼接起来,得到源串:
//        GET&%2Fv3%2Fuser%2Fget_info&appid%3D123456%26format%3Djson%26openid%3D11111111111111111%26
//        openkey%3D2222222222222222%26pf%3Dqzone%26userip%3D112.90.139.30
            source = "GET&" + tempuri + "&" + tempStr;

        } catch (Exception e) {
            Log.e("showhmacsha1", e.getMessage());
        }
    /*
     *   Step 2. 构造密钥
     *  得到密钥的方式:在应用的appkey末尾加上一个字节的“&”,即appkey&
     */
        key = QQConstants.QQ_APP_KEY + "&";

     /*
     *   Step 3. 生成签名值
     *   使用加密算法加密
     */
        sig = hmacSha1(source, key);
        Log.e("showhmacsha1", sig);
    /*
     *   Step 4. 返回sig
     *
     */
        return sig;
    }

    public static String hmacSha1(String base, String key) {
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secret = new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm());
            mac.init(secret);
            return Base64.encodeToString(mac.doFinal(base.getBytes()), Base64.NO_WRAP);
        } catch (NoSuchAlgorithmException e) {
            Log.e("showhmacsha1", "Hash algorithm SHA-1 is not supported", e);
        } catch (UnsupportedEncodingException e) {
            Log.e("showhmacsha1", "Encoding UTF-8 is not supported", e);
        } catch (InvalidKeyException e) {
            Log.e("showhmacsha1", "Invalid key", e);
        }
        return "";
    }
}

注意的问题:

官方介绍:
http://wiki.open.qq.com/wiki/%E8%85%BE%E8%AE%AF%E5%BC%80%E6%94%BE%E5%B9%B3%E5%8F%B0%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E7%AD%BE%E5%90%8D%E5%8F%82%E6%95%B0sig%E7%9A%84%E8%AF%B4%E6%98%8E

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值