Android-加密总结+支付集成问题

本文详细介绍了Android应用中加密算法的应用,包括数字签名、对称加密与非对称RSA,以及消息摘要算法。同时,文章讲解了银联、支付宝和微信支付的集成过程,包括支付步骤、SDK集成、异步通知等关键环节。此外,还提到了混合开发中Android与H5的三种通信方式。

> 加密算法总结


> 银联支付


> 支付宝:要求需要公司账号


> 微信支付:微信更新测试服务器,Demo,丢失聊天记录(安全码策略:keystore)


> 扩展:混合开发(android+H5,通信三种方式)


* 1.js主动调用android
* 2.android主动调用js
* 3.js callback回调式调用android


### 01.数字签名应用实战-签名


> 对登录信息(用户名、密码、时间戳 签名)


InputStream ins = null;
String usrename = "heima104";
String password = "123456";
System.out.println(System.currentTimeMillis());
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("username="+usrename)
.append("&password="+MD5Utils.md5(password))
.append("&timestamp="+System.currentTimeMillis());

String input = stringBuilder.toString();

String sign = SignatureUtils.sign(input);

try {
String url = "http://120.77.241.119/EncryptServer/login_v5?"+input+"&sign="+sign;
URL url2 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
System.out.println(url2.toURI().toString());
ins = conn.getInputStream();
String result = IoUtils.convertStreamToString(ins);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
IoUtils.close(ins);
}


### 02.数字签名应用实战-避免抓包


> 对提交参数md5:如果用户没有登录过,存储到数据库;如果登录过不让登录,该url已经失效


String url = "http://120.77.241.119/EncryptServer/login_v6?"+input+"&sign="+sign;
URL url2 = new URL(url);
String md5 = MD5Utils.md5(input+"&sign="+sign);
System.out.println(md5);
HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
System.out.println(url2.toURI().toString());
ins = conn.getInputStream();
String result = IoUtils.convertStreamToString(ins);


> 手机验证码


### 03.加密算法总结


> 对称加密(DES、AES):

* 1.优先使用DES,如果提高安全度使用AES
* 2.可逆:开发中只要可逆都可以选择对称加密,缓存联系人信息

> 非对称RSA


* 1.可逆:公钥加密私钥解密;私钥加密公钥解密
* 2.秘钥对:公钥和私钥,不能手动指定,必须由系统生成
* 3.加密速度慢:不能加密大文件
* 4.分段加密:每次最大加密长度117字节
* 5.分段解密:每次最大解密长度128字节
* 应用场景:一般很少使用RSA加密和解密,用的最多的是它的数字签名


> 消息摘要:


* md5:16个字节,转成16进制32个字节
* sha1:20个字节,转成16进制40个字节
* sha256:32个字节,转成16进制64个字节
* 应用:md5使用最多
* 特点:不可逆,比如微信缓存用户密码,下一次不需要重新输入登录
* 避免撞库:加密多次,加盐


> 数字签名


* 为了校验参数安全:支付传递参数,有可能被篡改
* 签名:必须使用私钥
* 校验:必须使用公钥
* 最常用算法SHA256withRSA




### 04.第三方支付介绍


> 三大支付:支付宝支付、微信支付、银联支付、京东钱包、百度钱包


> ping++:集成了很多第三方支付平台的 第三方平台,为了便于开发者使用集成。不建议使用,支付很简单不需要使用别人集成好的SDK


> 以后开发能不能独立实现支付平台:不可能,需要支付牌照




> 支付难不难:不难,银联5分钟、支付宝10分钟、微信25分钟




### 05.结合美团支付分析支付四部曲


> 美团支付创建:挑选商品:价格、数量、名称 -> 选择支付方式




![](img/pay_four_step.png)




### 06.银联支付SDK


> 第三方支付开发平台:开发者网站,下载SDK


> 任何开发平台:不要着急集成,先运行Demo


> 提供测试账号


### 07.银联支付前2步


> 前2步不需要集成SDK


> 提交参数到服务器


> 解析服务器返回的支付串码




private void requestVolley() {
        //1.创建请求对象
        StringRequest request = new StringRequest("http://101.231.204.84:8091/sim/getacptn", this, this);
        //2.创建请求队列
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }


    @Override
    public void onResponse(String s) {
        //支付四部曲第2步:解析服务器返回的“支付串码”(银联:交易流水号,一串数字)
        String trun = s;
        showLog(s);
    }


### 08.银联支付后2步


> 先集成银联SDK:开发平台集成思想,李白“飞流直下三千尺”(从上往下开官方Demo,asserts、libs、清单文件<权限+Activity>)


> 调用银联支付sdk的,传入“支付串码”


String mode = "01";
        UPPayAssistEx.startPayByJAR(MainActivity.this, PayActivity.class, null, null,trun, mode);


> 处理支付结果


  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String msg = null;
        /** 支付控件返回字符串:success、fail、cancel 分别代表支付成功,支付失败,支付取消*/
        String str = data.getExtras().getString("pay_result");
        if (str.equalsIgnoreCase("success")) {
            msg = "支付成功104!";
        } else if (str.equalsIgnoreCase("fail")) {
            msg = "支付失败104!";
        } else if (str.equalsIgnoreCase("cancel")) {
            msg = "用户取消了支付104";
        }
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();


    }


### 09.银联支付-异步通知同步返回


> 同步返回:客户端直接获取支付结果


> 异步通知:必须有银联服务器通知商户服务器,千万不能让客户端通知服务器,因为客户端通知不可靠


> 异步通知过程中商户服务器断网怎么办:订单发起30分钟后没有收到支付结果,主动向银联请求


### 10.支付宝支付SDK


> 支付宝支付SDKDemo:[https://docs.open.alipay.com/54/104509](https://docs.open.alipay.com/54/104509)




### 11.秘钥对配置1


> 默认运行SDKDemo不能调起支付,因为没有秘钥信息、商家账号


PARTNER:商家pid


SELLER:商家收款账号


> 参考资料/pdf:配置秘钥对




### 12.秘钥对配置2


RSA_PRIVATE:商家私钥


RSA_PUBLIC:支付宝公钥




> 参考资料/pdf:配置秘钥对


> 2中方式:openssl命令生成,一键生成




### 13.支付宝支付-公钥互换


> 公钥互换入口:[https://openhome.alipay.com/platform/keyManage.htm](https://openhome.alipay.com/platform/keyManage.htm)




### 14.支付宝支付服务器签名


> 为了保证安全,支付宝要求生成签名信息让商家服务器操作




### 15.支付宝支付前2步


> 提交参数到服务器


  StringRequest request = new StringRequest("http://120.77.241.119/tmall/pay?goodId=111&count=1&price=1"
                , this, this);
        RequestQueue queue = Volley.newRequestQueue(mContext);
        queue.add(request);


> 解析服务器返回的支付串码


  // 2.解析服务器返回的支付串码
        // String -> Bean:Gson、FastJson
        AlipayInfo alipayInfo = JSON.parseObject(s, AlipayInfo.class);
        Toast.makeText(mContext, ""+alipayInfo, Toast.LENGTH_SHORT).show();




### 16.支付宝支付集成SDK




> 导入一个jar包:支付宝sdk


> 配置清单文件


权限
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


activity


<activity
            android:name="com.alipay.sdk.app.H5PayActivity"
            android:configChanges="orientation|keyboardHidden|navigation|screenSize"
            android:exported="false"
            android:screenOrientation="behind"
            android:windowSoftInputMode="adjustResize|stateHidden" >
        </activity>






### 17.支付宝支付-后2步


> 开发者文档:[https://docs.open.alipay.com/204/105296/](https://docs.open.alipay.com/204/105296/)


> 调用支付功能




PayTask task = new PayTask(MainActivity.this);
                String payInfo = alipayInfo.getPayInfo();//支付串码
                //参数1:支付串码,参数2:是否显示横条加载进度
                String payResult = task.pay(payInfo, false);//同步返回支付结果


> 处理支付结果


  PayResult payResult = new PayResult((String) msg.obj);
            /**
             * 同步返回的结果必须放置到服务端进行验证(验证的规则请看https://doc.open.alipay.com/doc2/
             * detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665&
             * docType=1) 建议商户依赖异步通知
             */
            String resultInfo = payResult.getResult();// 同步返回需要验证的信息


            String resultStatus = payResult.getResultStatus();
            // 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档
            if (TextUtils.equals(resultStatus, "9000")) {
                showToast("支付成功");
                Toast.makeText(mContext, ""+(String)(msg.obj), Toast.LENGTH_SHORT).show();
            } else {
                // 判断resultStatus 为非"9000"则代表可能支付失败
                // "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
                if (TextUtils.equals(resultStatus, "8000")) {
                    showToast("支付结果确认中");
                } else {
                    // 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
                    showToast("支付失败");
                }
            }








### 18.支付宝支付-同步返回异步通知


> 同步返回:客户端直接获取支付结果


> 异步通知:[支付宝服务器通知商户服务器](https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.KUnD0S&treeId=59&articleId=103666&docType=1#s1)


> 通知过程中商户服务器停止:25小时以内完成8次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h);
程序执行完成后,该页面不能执行页面跳转。如果执行页面跳转,支付宝会收不到success字符,会被支付宝服务器判定为该页面程序运行出现异常,而重发处理结果通知;




### 19.微信支付SDK


> 微信支付:只要掌握支付、微信分享、微信登录,都一样


> 注意注意注意:微信支付Demo运行(只能运行资料AS Demo)


> 安全码处理:包名+应用名+keystore,为了唯一确定一个应用程序


> 想要运行官方Demo:必须使用它提供的debug.keystore,替换C:\Users\youliang.ji\.android目录


> 如果运行了没有替换debug.keystore不能调起支付,想要调起支付必须卸载微信(或者清空所有缓存),因为你微信缓存上一次的keystore






### 20.微信支付-获取appid


> 创建应用,提交keystore md5


> 微信限制创建应用个数:10个


> 必须通过审核才能调用、而且必须是商家账号






### 21.微信支付-前2步


> 1.提交参数给服务器


   StringRequest request = new StringRequest("http://wxpay.wxutil.com/pub_v2/app/app_pay.php", this, this);
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);


> 2.解析服务器返回的“支付串码”


  s = s.replace("package","packageValue");
        //2.解析获取服务器返回的支付串码
        wechatPayInfo = JSON.parseObject(s, WechatPayInfo.class);


### 22.微信支付-集成SDK


> 导入jar包
 
> 配置清单文件:权限


> 配置清单文件:配置Activity(WXPayEntryActivity),包名和类名不能变






### 23.微信支付-后2步


> 1.调用微信支付SDK




   private void callWechatPay() {
        PayReq req = new PayReq();
        req.appId = wechatPayInfo.getAppid();
        req.partnerId = wechatPayInfo.getPartnerid();
        req.prepayId = wechatPayInfo.getPrepayid();//微信支付核心参数:预支付订单号
        req.nonceStr = wechatPayInfo.getNoncestr();
        req.timeStamp = wechatPayInfo.getTimestamp()+"";
        req.packageValue = wechatPayInfo.getPackageValue();
        req.sign = wechatPayInfo.getSign();
        // 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信
        //3.调用微信支付sdk支付方法
        api.sendReq(req);


    }


> 2.处理支付结果:在WXPayEntryActivity类onResp处理支付结果


public void onResp(BaseResp resp) {


if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("微信支付结果104");
builder.setMessage("支付状态码:"+String.valueOf(resp.errCode));
builder.show();
}
}






### 24.微信支付-同步返回异步通知


> 同步返回:客户端支付直接获取支付结果WXPayEntryActivity类onResp方法


> 异步通知:微信服务器通知账号服务器,并且商户需要响应结果


> 通知过程中如果断网怎么办:商户需要响应结果




### 25.混合开发-初始化WebView


> 开启js和android通信开关


> 设置2个WebClient


### 26.混合开发-android调用js


> android代码


   JSONObject json = new JSONObject();
        json.put("name", "android");
        json.put("msg", "你好,我是安卓,想加你为蚝友!");
//核心代码
        mWebview.loadUrl("javascript:showMessage("+json.toString()+")");


> js代码


  function showMessage(json) {
        //弱类型
        alert(JSON.stringify(json));
    }


### 27.混合开发-js调用android


> android代码


mWebview.addJavascriptInterface(new JavaScriptMethods(this), "jsInterface");


public class JavaScriptMethods {

    private Context mContext;

    public JavaScriptMethods(Context mContext) {
        this.mContext = mContext;
    }

    @JavascriptInterface //android17以上,如果不加上该注解,js无法调用android方法(js反射调用android,为了安全)
    public void showToast(String json){
        Toast.makeText(mContext, ""+json, Toast.LENGTH_SHORT).show();
    }
}


> js代码


$("#btn1").on("click", function () {
        console.log("点击了按钮");
        //window.location.href = "http://m.ctrip.com/html5/";
        //调用android方法
        //window.android对象别名.方法名(参数)
        var json = {"msg":"不想加你蚝友!"}
        window.jsInterface.showToast(JSON.stringify(json));
    });


### 28.混合开发-js回调式调用android


> callback:H5显示数据,但是数据必须有android请求网络获取,js跨域问题(通过callback可以解决)


> 需求:H5显示酒店数据,来源服务器;通过android请求,返回给js









### 一.银联支付文档


* 1.[银联支付SDK下载](https://open.unionpay.com/ajweb/help/file/techFile?productId=3)


* 2.开发者平台:[https://open.unionpay.com/ajweb/help/file](https://open.unionpay.com/ajweb/help/file "https://open.unionpay.com/ajweb/help/file")


* 3.异步通知说明:


    * 图片所在路径:**资料\第三方支付平台\银联支付\手机控件支付产品技术开发包3.1.0\手机控件支付产品入网材料\PPT**
    * 
    ![](img/unionpay_notify.png)


* 3.模拟器可能会遇到的错误


![](img/uupError.PNG)


* 银联支付测试接口:[http://101.231.204.84:8091/sim/getacptn](http://101.231.204.84:8091/sim/getacptn "http://101.231.204.84:8091/sim/getacptn")


* 3 支付结果处理


onActivityResult方法中


* 4 支付参数


交易流水号,是一串数字,数字如何区别是哪个商家的订单?银行如何将该笔交易转账到指定商家?
请看下面的图:交易流水号是银联生成的,所有银行当然知道是谁的


* 5 支付流程图


![](img/unionpay_flow_chart.png)


----------


### 二.支付宝支付文档


* 1 开发者平台:[https://doc.open.alipay.com/doc2/alipayDocIndex.htm](https://doc.open.alipay.com/doc2/alipayDocIndex.htm "https://doc.open.alipay.com/doc2/alipayDocIndex.htm")


* 2 异步通知说明:[https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.KUnD0S&treeId=59&articleId=103666&docType=1#s1](https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.KUnD0S&treeId=59&articleId=103666&docType=1#s1 "支付宝异步通知支付结果")


* 3 异步通知图文:


![](img/alipay_notify.png)


* 4.[支付宝支付开发者登录入口](https://auth.alipay.com/login/ant_sso_index.htm?goto=https%3A%2F%2Fopenhome.alipay.com%2Fplatform%2FmanageApp.htm)


* 5.[SDK下载](https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1)


* 6.[支付宝支付状态码](https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.S07WKC&treeId=193&articleId=105302&docType=1#s2)


* 7.[支付宝参数说明](https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.IANz0U&treeId=59&articleId=103663&docType=1)


----------




### 三.微信支付文档


* 运行Demo报错:安全码策略:包名+应用名称+keystore,任何一个参数变化,支付失败


![](img/wechat_pay_error.gif)


> 解决方案:如果第一次没有使用微信Demo自己的keystore,会报错;改成微信的keystore还是报错,可以清理微信客户端缓冲(注意备份自己的聊天数据),在重新支付就没问题


* **1.微信支付开发者平台**:[https://pay.weixin.qq.com/wiki/doc/api/index.html](https://pay.weixin.qq.com/wiki/doc/api/index.html "https://pay.weixin.qq.com/wiki/doc/api/index.html")


* **2.异步通知文档**:[https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3](https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3 "https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3")


* **3.微信支付很特殊**:keystore签名+包名绑定+应用名称,安全码策略,为了给大家演示微信支付,上课的时候老师使用了微信Demo的包名、应用名称、keystore。到公司肯定使用公司的keystore+包名+应用名称(如何创建提交应用:请看微信支付创建应用视频)


* **4.微信支付测试接口**:[http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android](http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android")


* **5.微信开发者平台登录入口**:[https://open.weixin.qq.com/](https://open.weixin.qq.com/ "https://open.weixin.qq.com/")


* **6.微信支付签名工具**:[https://open.weixin.qq.com/zh_CN/htmledition/res/dev/download/sdk/Gen_Signature_Android.apk](https://open.weixin.qq.com/zh_CN/htmledition/res/dev/download/sdk/Gen_Signature_Android.apk)


* 7.[微信支付参数生成规则](https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3)




----------





Microsoft 为 Mac 提供了测试版远程客户端,您可转到 Microsoft Remote Desktop for Mac 进行下载。该测试版本客户端由微软官方维护,我们推荐您优先使用该版本客户端(微软已于 2017 年取消其官网提供的下载链接,转而通过其子公司 HockeyApp 的页面进行 Beta 版本的发布)。 地址:https://rink.hockeyapp.net/apps/5e0c144289a51fca2d3bfa39ce7f2b06/ Version 10.2.2 (1285) 更新于05 OCT 2018, 14:35 What's new in this update: Thanks for all the feedback! We have some exciting features and fixes in this release. A brand new Connection Center that supports drag and drop, manual arrangement of items, resizable columns in list view, column-based sorting, and easier group management. Settings import from the version 8 client has been improved (App Store client only). RDP files pointing to RemoteApp endpoints can now be imported into the Connection Center. The Connection Center now remembers the last active pivot (Desktops or Feeds) when closing the app. Retina display optimizations for Remote Desktop scenarios. Support for specifying the graphics interpolation level when not using Retina optimizations. 256-color support to enable connectivity to Windows 2000. Fixed clipping of the right and bottom edges of the screen when connecting to Windows 7/Windows Server 2008 R2 and earlier. Copying a local file into Outlook (running in a remote session) now adds the file as an attachment. Fixed an issue that was slowing down pasteboard-based file transfers if the files originated from a local network share. Addressed a bug that was causing to Excel (running in a remote session) to hang when saving data to a file on a redirected folder. Fixed an issue that was causing no free space to be reported for redirected folders. Added support for enforcing Remote Desktop Gateway device redirection policies. Remote Desktop Gateway feedback is now part of the connecting status UI. Fixed an issue that prevented session windows from closing when disconnecting. If NLA is not enforced by the server, you will now be routed to the login screen if your password has expired. The credential prompting UI and flows have been overhauled. Fixed performance issues that surfaced when lots of data was being transferred over the network. Smart card redirection fixes. Support for all possible values of the "EnableCredSspSupport" and "Authentication Level" RDP file settings if the ClientSettings.EnforceCredSSPSupport user default is set to 0. Support for the "Prompt for Credentials on Client" RDP file setting when NLA is not negotiated. Support for smart card-based login via smart card redirection at the Winlogon prompt when NLA is not negotiated. Fixed a bug that caused thumbnails to consume too much disk storage on macOS 10.14. Please keep the feedback coming. We listen to it all. If you encounter any errors, you can always contact us via Help > Report an Issue. If this does not work, you can mail us at rdios@microsoft.com. Get the app in the store Go to https://aka.ms/rdmac. If you would like to test drive new features and fixes continue using the app from this channel.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值