新增:HttpServer请求/应答报文进行RSA非对称加密传输

This commit is contained in:
pppscn 2022-10-19 21:13:51 +08:00
parent 50960c94c8
commit a624ca5e8e

View File

@ -1,10 +1,14 @@
package com.idormy.sms.forwarder.server.component
import android.util.Log
import com.idormy.sms.forwarder.utils.Base64
import com.idormy.sms.forwarder.utils.HttpServerUtils
import com.idormy.sms.forwarder.utils.RSACrypt
import com.yanzhenjie.andserver.annotation.Resolver
import com.yanzhenjie.andserver.error.HttpException
import com.yanzhenjie.andserver.framework.ExceptionResolver
import com.yanzhenjie.andserver.framework.body.JsonBody
import com.yanzhenjie.andserver.framework.body.StringBody
import com.yanzhenjie.andserver.http.HttpRequest
import com.yanzhenjie.andserver.http.HttpResponse
import com.yanzhenjie.andserver.http.StatusCode
@ -12,6 +16,8 @@ import com.yanzhenjie.andserver.http.StatusCode
@Resolver
class AppExceptionResolver : ExceptionResolver {
private val TAG: String = "AppExceptionResolver"
override fun onResolve(request: HttpRequest, response: HttpResponse, e: Throwable) {
e.printStackTrace()
if (e is HttpException) {
@ -23,7 +29,16 @@ class AppExceptionResolver : ExceptionResolver {
}
//返回统一结构报文
response.setBody(JsonBody(HttpServerUtils.response(e.message.toString())))
var resp = HttpServerUtils.response(e.message.toString())
Log.d(TAG, "resp: $resp")
if (HttpServerUtils.safetyMeasures != 2) {
response.setBody(JsonBody(resp))
} else {
val privateKey = RSACrypt.getPrivateKey(HttpServerUtils.serverPrivateKey.toString())
resp = Base64.encode(resp.toByteArray())
resp = RSACrypt.encryptByPrivateKey(resp, privateKey)
response.setBody(StringBody(resp))
}
}
}