接口非对称加密+Retrofit2(未完成)
照常例打出我搜到的相关文章
http://blog.csdn.net/ouyang_peng/article/details/50983574
https://www.zhihu.com/question/20874499
在讲接口加密之前,先了解AES与RSA的区别。
(具体算法咱们就不讨论了,如果一定要讨论:https://www.cnblogs.com/awsqsh/articles/4349114.html)
AES:对称加密。加密速度快,相比RSA的安全性就没那么高。
RSA:非对称加密。算法的密钥很长,具有较好的安全性,但加密的计算量很大,加密速度较慢限制了其应用范围。
1.AES与RSA
对称加密AES:

如图所说,服务器(男方)和客户端(女方)都彼此知道该钥匙是0101000101,他们通过彼此的钥匙来进行加密解密。
非对称加密RSA:
如果上面对称加密AES的一个钥匙被偷走的话,那么就很容易破解了。
那么非对称加密就是很好的解决了这个方法。

上面的图片可以首先看到钥匙已经不再是一个了,而是公钥跟私钥一起组合的钥匙,公钥是可以公开出来的。首先加密是通过服务器(男方)加密的,然后客户端(女方)通过该公钥和自己客户端私下的私钥进行解密。
也许你们会认为rsa不就多了个公钥私钥的区别么,其一来说单纯一个钥匙同时在服务端和客户端容易泄漏出去,其二来说用公钥和明文推导密文很容易,但根据公钥、明文和密文推导私钥极其难
2.AES与RSA如何配合加密
从上面比较得知,由于RSA加解密速度慢,不适合大量数据文件加密。AES加密速度很快。
这样在传送机密信息的双方
1:使用AES对传输数据加密
2:使用RSA不对称密码体制来传送AES的密钥
这样就可以综合发挥AES和RSA的优点同时避免它们缺点来实现一种新的数据加密方案。加解密实现流程如图(同样是网上找的)。

那么流程我们明白了,开始深入了解代码。假设一个场景,app客户端向服务端传输一个数据

这个流程我分开5个步骤,让我们一个个步骤讲:
第一步解析:handleRSA
%……&¥%……&¥#……%#@……留待清明节编辑。
好了,加解密讲完了,那么如何配合Retrofit2来进行加解密呢?
首先,我们先解决提交数据的方面的加密。那么,提交数据又分为get和post形式提交。那么让我们先解决get形式的提交。
在讲get形式的提交,我们先封装Retrofit2的一些方法,有个统一方法处理数据的提交。
添加Retrofit2公共处理方法
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
File cacheFile = new File(BaseApplication.getInstance().getBaseContext().getCacheDir(), "cache");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
SignInterceptor signInterceptor=new SignInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(7676, TimeUnit.MILLISECONDS)
.connectTimeout(7676, TimeUnit.MILLISECONDS)
.addInterceptor(signInterceptor)
.cache(cache)
.build();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(CustomConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BuildConfig.ENDPOINT)
.build();
我们可以看到,有signInterceptor的一个类和CustomConverterFactory这么一个工厂类
signInterceptor
:统一处理get形式的请求数据
CustomConverterFactory
:统一处理post形式的请求数据和返回数据
/**
* 重写父类的发送数据、返回数据等格式
* Created by zhongjh on 2017/6/1.
*/
public final class CustomConverterFactory extends Converter.Factory {
public static CustomConverterFactory create() {
return create(new Gson());
}
public static CustomConverterFactory create(Gson gson) {
return new CustomConverterFactory(gson);
}
private final Gson gson;
private CustomConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
/**
* 需要重写父类中responseBodyConverter,该方法用来转换服务器返回数据
*/
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new CustomResponseBodyConverter<>(type);
}
/**
* 需要重写父类中responseBodyConverter,该方法用来转换发送给服务器的数据
*/
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new GsonRequestBodyConverter<>(gson, adapter);
}
@Override
public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return super.stringConverter(type, annotations, retrofit);
}
}
在上面的方法看到,有分别CustomResponseBodyConverter处理返回的数据和GsonRequestBodyConverter处理请求的数据
get形式提交的加密
%……&¥%……&¥#……%#@……留待清明节编辑。