日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)百度小程序教程:用戶數(shù)據(jù)的簽名驗(yàn)證和加解密
  • 用戶數(shù)據(jù)的簽名驗(yàn)證和加解密
    • 開放數(shù)據(jù)說明
    • 開發(fā)者后臺校驗(yàn)與解密開放數(shù)據(jù)
    • 解密算法說明
      • 會話密鑰 session_key 有效性說明
      • 解密后內(nèi)容
      • 解密示例代碼
    • 涉及加解密的開放數(shù)據(jù)接口
    • 解密常見問題

    用戶數(shù)據(jù)的簽名驗(yàn)證和加解密

    開放數(shù)據(jù)說明

    智能小程序可以通過各種前端接口獲取百度提供的開放數(shù)據(jù)。為了確保開放接口返回用戶數(shù)據(jù)的安全性,百度會對這些數(shù)據(jù)做簽名和加密處理。開發(fā)者后臺拿到開放數(shù)據(jù)后可以對數(shù)據(jù)進(jìn)行校驗(yàn)簽名和解密。

    開發(fā)者后臺校驗(yàn)與解密開放數(shù)據(jù)

    接口如果涉及敏感數(shù)據(jù),接口的明文內(nèi)容將不包含這些敏感數(shù)據(jù)。開發(fā)者如需要獲取敏感數(shù)據(jù),需要對接口返回的加密數(shù)據(jù)(data)進(jìn)行對稱解密。
    解密過程:開發(fā)者智能小程序(通過 swan.request)將加密數(shù)據(jù)發(fā)送至自身 Server 進(jìn)行解密后返回智能小程序。

    解密算法說明

    1. 對稱解密使用的算法為 AES-192-CBC,數(shù)據(jù)采用 PKCS#7 填充;
    2. 對稱解密的目標(biāo)密文為 Base64_Decode(data);
    3. 對稱解密秘鑰 AESKey = Base64_Decode(session_key),AESKey 是 24 字節(jié);
    4. 對稱解密算法初始向量 為 Base64_Decode(iv),其中 iv 由數(shù)據(jù)接口返回。

    會話密鑰 session_key 有效性說明

    基于 session_key 的開發(fā)者請關(guān)注下面幾個(gè)與 session_key 有關(guān)的注意事項(xiàng):

    1、 session_key 是具有時(shí)效性的,過期的 sessionn_key 將無法使用。開發(fā)者在 session_key 失效時(shí),需要通過重新執(zhí)行登錄流程獲取session_key。
    2、 使用 checkSession() 可以校驗(yàn) Session Key 是否有效,從而避免小程序反復(fù)執(zhí)行登錄流程,參考授權(quán)流程圖中 checkSession() 使用。
    3、 智能小程序不會把 session_key 的有效期告知開發(fā)者。我們會根據(jù)用戶使用小程序的行為對 session_key 進(jìn)行續(xù)期。用戶越頻繁使用小程序, session_key 有效期越長。

    注意
    session_key過期會導(dǎo)致開放數(shù)據(jù)解密失敗。要判斷當(dāng)前用戶的授權(quán)會話是否仍處于有效期,可調(diào)用 swan.checkSession() 方法進(jìn)行判斷,詳情參照登錄授權(quán)流程說明。

    解密后內(nèi)容

    內(nèi)容 長度
    隨機(jī)填充內(nèi)容16 字節(jié)
    用戶數(shù)據(jù)長度4 字節(jié),大端序無符號 32 位整型
    用戶數(shù)據(jù)由用戶數(shù)據(jù)長度描述
    app_key與 app_key 長度相同

    解密示例代碼

    特別說明:受美國軟件出口限制,JDK 默認(rèn)使用的 AES 算法最高只能支持 128 位。如需要更高位的支持需要從 oracle 官網(wǎng)下載 Java 密碼技術(shù)擴(kuò)展(JCE)更換 JAVA_HOME/jre/lib/security 目錄下的: local_policy.jar 和 US_export_policy.jar。

    下載地址:https://www.oracle.com/technetwork/java/javase/downloads/jce-all-download-5170447.html

    • PHP 版本
    • JAVA 版本
     
     
     
    1. /**
    2. * @Author: smartprogram_rd@baidu.com
    3. * Copyright 2018 The BAIDU. All rights reserved.
    4. *
    5. * 百度小程序用戶信息加解密示例代碼(面向過程版)
    6. * 示例代碼未做異常判斷,請勿用于生產(chǎn)環(huán)境
    7. */
    8. function test() {
    9. $app_key = 'y2dTfnWfkx2OXttMEMWlGHoB1KzMogm7';
    10. $session_key = '1df09d0a1677dd72b8325aec59576e0c';
    11. $iv = "1df09d0a1677dd72b8325Q==";
    12. $ciphertext = "OpCoJgs7RrVgaMNDixIvaCIyV2SFDBNLivgkVqtzq2GC10egsn+PKmQ/+5q+chT8xzldLUog2haTItyIkKyvzvmXonBQLIMeq54axAu9c3KG8IhpFD6+ymHocmx07ZKi7eED3t0KyIxJgRNSDkFk5RV1ZP2mSWa7ZgCXXcAbP0RsiUcvhcJfrSwlpsm0E1YJzKpYy429xrEEGvK+gfL+Cw==";
    13. $plaintext = decrypt($ciphertext, $iv, $app_key, $session_key);
    14. // 解密結(jié)果應(yīng)該是 '{"openid":"open_id","nickname":"baidu_user","headimgurl":"url of image","sex":1}'
    15. echo $plaintext, PHP_EOL;
    16. }
    17. test();
    18. /**
    19. * 數(shù)據(jù)解密:低版本使用mcrypt庫(PHP < 5.3.0),高版本使用openssl庫(PHP >= 5.3.0)。
    20. *
    21. * @param string $ciphertext 待解密數(shù)據(jù),返回的內(nèi)容中的data字段
    22. * @param string $iv 加密向量,返回的內(nèi)容中的iv字段
    23. * @param string $app_key 創(chuàng)建小程序時(shí)生成的app_key
    24. * @param string $session_key 登錄的code換得的
    25. * @return string | false
    26. */
    27. function decrypt($ciphertext, $iv, $app_key, $session_key) {
    28. $session_key = base64_decode($session_key);
    29. $iv = base64_decode($iv);
    30. $ciphertext = base64_decode($ciphertext);
    31. $plaintext = false;
    32. if (function_exists("openssl_decrypt")) {
    33. $plaintext = openssl_decrypt($ciphertext, "AES-192-CBC", $session_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
    34. } else {
    35. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, null, MCRYPT_MODE_CBC, null);
    36. mcrypt_generic_init($td, $session_key, $iv);
    37. $plaintext = mdecrypt_generic($td, $ciphertext);
    38. mcrypt_generic_deinit($td);
    39. mcrypt_module_close($td);
    40. }
    41. if ($plaintext == false) {
    42. return false;
    43. }
    44. // trim pkcs#7 padding
    45. $pad = ord(substr($plaintext, -1));
    46. $pad = ($pad < 1 || $pad > 32) ? 0 : $pad;
    47. $plaintext = substr($plaintext, 0, strlen($plaintext) - $pad);
    48. // trim header
    49. $plaintext = substr($plaintext, 16);
    50. // get content length
    51. $unpack = unpack("Nlen/", substr($plaintext, 0, 4));
    52. // get content
    53. $content = substr($plaintext, 4, $unpack['len']);
    54. // get app_key
    55. $app_key_decode = substr($plaintext, $unpack['len'] + 4);
    56. return $app_key == $app_key_decode ? $content : false;
    57. }
     
     
     
    1. /*
    2. * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
    3. */
    4. /*
    5. * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
    6. */
    7. package com.baidu.utils.secruity;
    8. import java.nio.charset.Charset;
    9. import java.util.Arrays;
    10. import javax.crypto.Cipher;
    11. import javax.crypto.spec.IvParameterSpec;
    12. import javax.crypto.spec.SecretKeySpec;
    13. import org.apache.commons.codec.binary.Base64;
    14. public class Demo {
    15. private static Charset CHARSET = Charset.forName("utf-8");
    16. /**
    17. * 對密文進(jìn)行解密
    18. *
    19. * @param text 需要解密的密文
    20. *
    21. * @return 解密得到的明文
    22. *
    23. * @throws Exception 異常錯誤信息
    24. */
    25. public String decrypt(String text, String sessionKey,String ivStr) throws Exception {
    26. byte[] aesKey = Base64.decodeBase64(sessionKey + "=");
    27. byte[] original;
    28. try {
    29. Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    30. SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
    31. byte[] ivBytes = Base64.decodeBase64(ivStr);
    32. IvParameterSpec iv = new IvParameterSpec(ivBytes);
    33. cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
    34. byte[] encrypted = Base64.decodeBase64(text);
    35. original = cipher.doFinal(encrypted);
    36. } catch (Exception e) {
    37. throw new Exception(e);
    38. }
    39. String xmlContent;
    40. String fromClientId;
    41. try {
    42. // 去除補(bǔ)位字符
    43. byte[] bytes = PKCS7Encoder.decode(original);
    44. // 分離16位隨機(jī)字符串,網(wǎng)絡(luò)字節(jié)序和ClientId
    45. byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
    46. int xmlLength = recoverNetworkBytesOrder(networkOrder);
    47. xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);
    48. fromClientId = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET);
    49. } catch (Exception e) {
    50. throw new Exception(e);
    51. }
    52. return xmlContent;
    53. }
    54. public static String getType(Object test) {
    55. return test.getClass().getName().toString();
    56. }
    57. /**
    58. * 還原4個(gè)字節(jié)的網(wǎng)絡(luò)字節(jié)序
    59. *
    60. * @param orderBytes 字節(jié)碼
    61. *
    62. * @return sourceNumber
    63. */
    64. private int recoverNetworkBytesOrder(byte[] orderBytes) {
    65. int sourceNumber = 0;
    66. int length = 4;
    67. int number = 8;
    68. for (int i = 0; i < length; i++) {
    69. sourceNumber <<= number;
    70. sourceNumber |= orderBytes[i] & 0xff;
    71. }
    72. return sourceNumber;
    73. }
    74. /**
    75. * 加密解密demo
    76. *
    77. * @param args
    78. * @throws Exception
    79. */
    80. public static void main(String[] args) throws Exception {
    81. String dy = "toMIrTrp2WovaM4RUoqsrBX4kR7p5JThrzY8bW4ZTBKm4YPRr0CfxY8ZZFwk0RJIPEVCVNebRuN3h6zOIHrHrjdvz5hcKkRfX3VO4OfoHJ3LiZv5uVRl6056iLBgNm+x2HY6T07A40aKeYJQDT3kmgdaAi3UB7NUlrEFUpAuZ2Tsm5B+bF3lnbmUzhskTCFE";
    82. String sessionKey = "a28bea08d86e426f8d51e024194eae6f";
    83. String iv = "a28bea08d86e426f8d51ew==";
    84. Demo demo = new Demo();
    85. String dd = demo.decrypt(dy, sessionKey, iv);
    86. System.out.println(dd);
    87. }
    88. }
    89. class PKCS7Encoder {
    90. static Charset CHARSET = Charset.forName("utf-8");
    91. static int BLOCK_SIZE = 32;
    92. /**
    93. * 獲得對明文進(jìn)行補(bǔ)位填充的字節(jié).
    94. *
    95. * @param count 需要進(jìn)行填充補(bǔ)位操作的明文字節(jié)個(gè)數(shù)
    96. *
    97. * @return 補(bǔ)齊用的字節(jié)數(shù)組
    98. */
    99. static byte[] encode(int count) {
    100. // 計(jì)算需要填充的位數(shù)
    101. int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
    102. if (amountToPad == 0) {
    103. amountToPad = BLOCK_SIZE;
    104. }
    105. // 獲得補(bǔ)位所用的字符
    106. char padChr = chr(amountToPad);
    107. String tmp = new String();
    108. for (int index = 0; index < amountToPad; index++) {
    109. tmp += padChr;
    110. }
    111. return tmp.getBytes(CHARSET);
    112. }
    113. /**
    114. * 刪除解密后明文的補(bǔ)位字符
    115. *
    116. * @param decrypted 解密后的明文
    117. *
    118. * @return 刪除補(bǔ)位字符后的明文
    119. */
    120. static byte[] decode(byte[] decrypted) {
    121. int pad = (int) decrypted[decrypted.length - 1];
    122. if (pad < 1 || pad > 32) {
    123. pad = 0;
    124. }
    125. return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
    126. }
    127. /**
    128. * 將數(shù)字轉(zhuǎn)化成ASCII碼對應(yīng)的字符,用于對明文進(jìn)行補(bǔ)碼
    129. *
    130. * @param a 需要轉(zhuǎn)化的數(shù)字
    131. *
    132. * @return 轉(zhuǎn)化得到的字符
    133. */
    134. static char chr(int a) {
    135. byte target = (byte) (a & 0xFF);
    136. return (char) target;
    137. }
    138. }

    涉及加解密的開放數(shù)據(jù)接口

    各場景解密獲取的數(shù)據(jù)結(jié)構(gòu)請參照相應(yīng)功能的接口文檔說明
    獲取用戶信息 - button 組件方式、獲取用戶手機(jī)號

    解密常見問題

    java 解密報(bào) OOM 錯誤解決辦法:受美國軟件出口限制,JDK 默認(rèn)使用的 AES 算法最高只能支持 128 位。如需要更高位的支持需要從 oracle 官網(wǎng)下載 Java 密碼技術(shù)擴(kuò)展(JCE)更換 JAVA_HOME/jre/lib/security 目錄下的:local_policy.jar 和 US_export_policy.jar。

    下載地址:https://www.oracle.com/technetwork/java/javase/downloads/jce-all-download-5170447.html


    當(dāng)前題目:創(chuàng)新互聯(lián)百度小程序教程:用戶數(shù)據(jù)的簽名驗(yàn)證和加解密
    文章鏈接:http://m.5511xx.com/article/dhsocih.html