Commit 0c6f88b2 by Fear1ess

4/26

parent b01b6d30
package com.reyun.sdktestdemo;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
......@@ -9,11 +11,19 @@ import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SocketChannel;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterInputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.ZipInputStream;
import javax.crypto.BadPaddingException;
......@@ -24,48 +34,81 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class HttpServer extends NanoHTTPD {
public HttpServer(int port) {
private TextView mTextView;
public HttpServer(int port, TextView textView) {
super(port);
mTextView = textView;
}
private String m_RSAPrivateKey=/*"-----BEGIN RSA PRIVATE KEY-----\n" +*/
"MIICXQIBAAKBgQDWuIwr3n0x5rgCVhPtwJVeg0NSQBOcu+nND4rNIqqBc3+eQLuT\n" +
"Dn19IfzX0n2lGRsY9x9EUn8WA4TTWA+Q4he/5NlEFBWaW2O7fz25pIeoilnTtJVt\n" +
"fc8KU1+i8rCKtWDS9jvabJWjDVcVIcASArkA8TL9AKqRAzC0RlTLRpj5jQIDAQAB\n" +
"AoGAWbI0E+tYXy0jNw6gOpRVkWrCB6zuxG2XSGsyr2jI01TdPc2Kze6iq1A0YC/0\n" +
"/q/TKwWokRDpN9f2d5b0wbBA/MtXkISGMJsEbOInGQr6HOO2Y9uUQYnFClmS7taV\n" +
"h1y3rSVqvUffR2TcTxSRYn7iPXNSxMoxPWMDuIYsbyqjUR0CQQD1NLlYpHBYmOzP\n" +
"AOZe0cgPPtan53KS0XIrFJPymEHSX/ewiHpgrxxdqu9EjCmNhDifw2GLiwCXtOB7\n" +
"rzUvkDmPAkEA4CxH90DT+KCiu9U5g7cE2SUcNQJQoNBQ7PyvSfayXN02TXQjROhk\n" +
"0N74fp0pTc3MOLXI6aUPzhQodKzGImG1IwJBAMS6PEomBAzRBI1AB5Pjc0LoA4hK\n" +
"ZN3uNE7j/58PESAb5rk3hhez5mowDi9M1Lm5b0SemNBAAYW1lLUm6OD0MXkCQFa4\n" +
"qqp7GMWBFxNE/Hf48FY0pgHXwGtFy1iys/kQB5YnOZ+jAxXgNUgcSdNpZxYjPBLg\n" +
"ODYy1bdqUvA3+npIhDMCQQDCuRfbht8EqB1LeTi0/4TVb5X7JFUlCp2askCBUetO\n" +
"zcfineSbkroY9/QTItUEkGtOxfWxHAMQ/Lle/rKBxXhm\n";
/*"-----END RSA PRIVATE KEY-----";*/
@Override
public Response serve(IHTTPSession session) {
if(!session.getMethod().equals(Method.POST)) return newFixedLengthResponse("not support GET");
if(!(session instanceof HTTPSession)) return newFixedLengthResponse("not support such method");
Log.d("wd_collect", "serve start...");
try{
int len = (int) ((HTTPSession)session).getBodySize();
byte[] b = new byte[(int) len];
InputStream in = session.getInputStream();
byte[] b = new byte[1024];
int len;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
DataInputStream di = new DataInputStream(in);
byte[] key = new byte[16];
byte[] iv = new byte[16];
in.read(b, 0, len);
ByteBuffer bb = ByteBuffer.wrap(b);
bb.order(ByteOrder.LITTLE_ENDIAN);
byte[] encodedkey = new byte[128];
byte[] encodediv = new byte[128];
byte[] data_md5 = new byte[16];
int ts;
int data_len;
if(di.read(key, 0, 16) == 16) {
if(di.read(iv, 0, 16) == 16) {
ts = di.readInt();
if(di.read(data_md5, 0, 16) == 16) {
data_len = di.readInt();
byte[] data = new byte[data_len];
if(di.read(data, 0, data_len) == data_len) {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
bb.get(encodedkey, 0, 128);
bb.get(encodediv, 0, 128);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(m_RSAPrivateKey, Base64.DEFAULT));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Cipher cipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher2.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(keySpec));
byte[] key = cipher2.doFinal(encodedkey);
byte[] iv = cipher2.doFinal(encodediv);
ts = bb.getInt();
bb.get(data_md5, 0, 16);
data_len = bb.getInt();
final byte[] data = new byte[data_len];
bb.get(data, 0, data_len);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
byte[] decodedData = cipher.doFinal(data);
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(decodedData));
Inflater inflater = new Inflater();
inflater.setInput(decodedData);
byte[] buf = new byte[1024];
ByteArrayOutputStream bo2 = new ByteArrayOutputStream();
int len2;
while((len2 = zis.read(buf, 0, 1024)) >= 0) {
bo2.write(buf, 0, len2);
while(!inflater.finished()){
int n = inflater.inflate(buf, 0, 1024);
bo2.write(buf, 0, n);
}
byte[] ori_data = bo2.toByteArray();
Log.d("wdsdk", "ori_data: " + ori_data);
}
}
}
final String xxx = new String(ori_data);
Log.d("wdsdk", "serve: " + xxx);
mTextView.post(new Runnable() {
@Override
public void run() {
mTextView.setText(xxx);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
......@@ -80,6 +123,10 @@ public class HttpServer extends NanoHTTPD {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (DataFormatException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return newFixedLengthResponse("hahaha...");
}
......
......@@ -67,9 +67,7 @@ public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = findViewById(R.id.data);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.READ_PHONE_STATE,
......@@ -79,7 +77,6 @@ public class MainActivity extends Activity {
Manifest.permission.ACCESS_COARSE_LOCATION
}, 100);
}
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Service.WIFI_SERVICE);
WifiInfo wi = wm.getConnectionInfo();
String ssid = wi.getSSID();
......@@ -97,7 +94,6 @@ public class MainActivity extends Activity {
} catch (IOException e) {
e.printStackTrace();
}
try {
PackageInfo pi = pm.getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
byte[] b = pi.signatures[0].toByteArray();
......@@ -106,14 +102,12 @@ public class MainActivity extends Activity {
} catch (PackageManager.NameNotFoundException | CertificateException e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode == 100) {
HttpServer hs = new HttpServer(9666);
HttpServer hs = new HttpServer(9666, mText);
try {
hs.start();
} catch (IOException e) {
......@@ -127,12 +121,6 @@ public class MainActivity extends Activity {
@Override
public void onWdId(final String wdId) {
Log.d("demo", "onWdId: " + wdId);
mText.postDelayed(new Runnable() {
@Override
public void run() {
mText.setText(wdId);
}
}, 0);
}
/*
@Override
......@@ -151,7 +139,6 @@ public class MainActivity extends Activity {
NetworkInfo networkInfo = cm.getNetworkInfo(network);
if (networkInfo.getType() == activeNetworkInfo.getType()) {
LinkProperties lp = cm.getLinkProperties(network);
for (InetAddress addr : lp.getDnsServers()) {
// Get DNS IP address here:
result += addr.getHostAddress() + ",";
......
......@@ -30,6 +30,7 @@ add_library(
rsaencode.c
mybase64.c
fake_dlfcn.c
data_handler.c
${ARCH_DEPENDED_SRC}
)
......
......@@ -13,6 +13,7 @@
#include <time.h>
#include <syscall.h>
#include <openssl/md5.h>
#include <rsaencode.h>
#define LOG_TAG "WD_COLLECT"
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__);
......@@ -28,14 +29,14 @@ char *padding_buf(char *buf,int size, int *final_size);
char *padding_buf(char *buf,int size, int *final_size) {
char *ret = NULL;
//填充区的大小
int pidding_size = AES_BLOCK_SIZE - (size % AES_BLOCK_SIZE);
int padding_size = AES_BLOCK_SIZE - (size % AES_BLOCK_SIZE);
int i;
*final_size = size + pidding_size;
ret = (char *)calloc(1,size+pidding_size);
*final_size = size + padding_size;
ret = (char *)calloc(1,size+padding_size);
memcpy( ret, buf, size);
if (pidding_size!=0) {
for (i =size;i < (size+pidding_size); i++ ) {
ret[i] = 0;
if (padding_size!=0) {
for (i =size;i < (size+padding_size); i++ ) {
ret[i] = padding_size;
}
}
return ret;
......@@ -48,32 +49,21 @@ int startAESEncode(char*m_key,char*m_iv, char* src,int src_Size,char**encode_ptr
char *after_padding_buf = NULL;
int padding_size = 0;
char *encrypt_buf = NULL;
//进行填充
//进行pkcs7填充
after_padding_buf = padding_buf(src,src_Size,&padding_size);
if(after_padding_buf==NULL){
return 0;
}
// 进行加密 key, iv, ts, data_md5, data_len
unsigned char* data_buf = (unsigned char*) calloc(1, padding_size + 16 + 16 + 4 + 16 + 4);
encrypt_buf = data_buf + 56;
encrypt_buf = (unsigned char*) calloc(1, padding_size);
encrpyt_buf(m_key,m_iv,after_padding_buf,&encrypt_buf, padding_size);
memcpy(data_buf, m_key, 16);
memcpy(data_buf + 16, m_iv, 16);
struct timespec ts;
if(WDSYSCALL(SYS_clock_gettime, CLOCK_REALTIME, &ts) == 0) {
snprintf(data_buf + 32, 4, "%x", ts.tv_sec);
}else {
memset(data_buf + 32, 0, 4);
}
char md5[16];
MD5(encrypt_buf, padding_size, md5);
memcpy(data_buf + 36, md5, 16);
memcpy(data_buf + 52, &padding_size, 4);
free(after_padding_buf);
//传出加密后的内容
*encode_ptr = data_buf;
*out_size=padding_size + 56;
*encode_ptr = encrypt_buf;
*out_size=padding_size;
//-----------------测试代码
//打印源字符串的16进制
unsigned char* temp = ( unsigned char*)calloc(1,padding_size * 2 + 1);
......
......@@ -23,9 +23,8 @@
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <zlib.h>
#include "https.h"
#include "aesencode.h"
#include "data_handler.h"
#define WD_COLLECT "wd_collect"
......@@ -88,36 +87,21 @@ jstring do_collect(JNIEnv* env) {
snprintf(srt, 31, "%.2lf", end_time - start_time);
cJSON_AddStringToObject(json, "srt", srt);
const char* data = cJSON_Print(json);
const char* send_data = cJSON_PrintUnformatted(json);
size_t data_len = strlen(send_data);
//zlib compress
void* z_lib = fake_dlopen("libz.so", 0);
if(z_lib) {
u_long (*compressBound_func) (u_long) = fake_dlsym(z_lib, "compressBound");
int (*compress_func) (unsigned char* dest, u_long* p_dest_len, const unsigned char* src, u_long src_len) =
fake_dlsym(z_lib, "compress");
if(compress && compressBound_func) {
u_long bound = compressBound_func(data_len);
unsigned char* compressed_data = malloc(bound);
int res = compress_func(compressed_data, &bound, send_data, data_len);
if(res == Z_OK) {
//加密
char aes_key[17] = {0}, aes_iv[17] = {0};
char* encoded_data;
size_t encoded_len = 0;
aes_Encoder_Base64(compressed_data, bound, &encoded_data, &encoded_len, aes_key, aes_iv);
// const char* data = cJSON_Print(json);
const char* raw_data = cJSON_Print(json);
size_t data_len = strlen(raw_data);
unsigned char* packed_data = NULL;
int packed_len = 0;
if(pack_data(raw_data, data_len, &packed_data, &packed_len) == 0) {
//http post
http_PostConnect(env, "http://172.23.0.3:9666", encoded_data, encoded_len);
}
}
logd(WD_COLLECT, "%s", "ready to post data");
http_PostConnect(env, "http://172.23.0.3:9666", packed_data, packed_len);
}
free(packed_data);
free(raw_data);
jstring data_jstr = (*env)->NewStringUTF(env, data);
return data_jstr;
return (*env)->NewStringUTF(env, "fldfjlfjslfjslfjs");
}
void collect_app_info(JNIEnv* env, cJSON *json) {
......@@ -135,6 +119,7 @@ void collect_app_info(JNIEnv* env, cJSON *json) {
jobject pkgInfo = wdCallObjectMethod(env, pkgManager, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;", pkgName, 0x40);
//last update time
jlong lastUpdateTime = wdGetLongField(env, pkgInfo, "lastUpdateTime", "J");
cJSON_AddNumberToObject(json, "last_update_time", lastUpdateTime);
......@@ -1141,6 +1126,7 @@ void collect_risk_info(JNIEnv *env, cJSON *json) {
(*env)->DeleteLocalRef(env, files_dir);
cJSON_AddItemToObject(json, "risk", risk_item);
logd(WD_COLLECT, "%s", "collect risk info finished...");
}
void collect_common_app(JNIEnv *env, cJSON *json) {
......@@ -1175,6 +1161,7 @@ void collect_common_app(JNIEnv *env, cJSON *json) {
}
}
cJSON_AddItemToObject(json, "common_used_apps", item);
logd(WD_COLLECT, "%s", "collect common used apps finished");
}
void collect_installed_apps(JNIEnv *env, cJSON *json) {
......@@ -1189,4 +1176,6 @@ void collect_installed_apps(JNIEnv *env, cJSON *json) {
pclose(fp);
}
cJSON_AddItemToObject(json, "installed_apps", item);
logd(WD_COLLECT, "%s", "collect installed apps finished");
}
\ No newline at end of file
//
// Created by Fear1ess on 2021/4/27.
//
#include "data_handler.h"
#include <malloc.h>
#include <rsaencode.h>
#include <aesencode.h>
#include <zlib.h>
#include <fake_dlfcn.h>
#include <wd_syscall.h>
#include <openssl/md5.h>
#include <syscall.h>
IMPORTWDSYSCALL
int pack_data(unsigned char *raw, int len, unsigned char **packed_data, int *packed_len) {
void* z_lib = fake_dlopen("libz.so", 0);
int res = 0;
if(z_lib) {
u_long (*compressBound_func) (u_long) = fake_dlsym(z_lib, "compressBound");
int (*compress_func) (unsigned char* dest, u_long* p_dest_len, const unsigned char* src, u_long src_len) =
fake_dlsym(z_lib, "compress");
if(compress && compressBound_func) {
u_long bound = compressBound_func(len);
unsigned char *compressed_data = malloc(bound);
int res = compress_func(compressed_data, &bound, raw, len);
if (res == Z_OK) {
//加密
char aes_key[17] = {0}, aes_iv[17] = {0};
char *encoded_data;
size_t encoded_len = 0;
aes_Encoder_Base64(compressed_data, bound, &encoded_data, &encoded_len, aes_key,
aes_iv);
free(compressed_data);
// 进行加密 key, iv, ts, data_md5, data_len
int data_len = encoded_len + 128 + 128 + 4 + 16 + 4;
unsigned char *data_buf = (unsigned char *) calloc(1, data_len);
char encoded_key[128] = {0};
char encoded_iv[128] = {0};
int enckey_len, enciv_len;
rsa_Encoder_Base64(aes_key, 16, encoded_key, &enckey_len);
rsa_Encoder_Base64(aes_iv, 16, encoded_iv, &enciv_len);
memcpy(data_buf, encoded_key, 128);
memcpy(data_buf + 128, encoded_iv, 128);
struct timespec ts;
if (WDSYSCALL(SYS_clock_gettime, CLOCK_REALTIME, &ts) == 0) {
memcpy(data_buf + 256, &ts.tv_sec, 4);
} else {
memset(data_buf + 256, 0, 4);
}
char md5[16];
MD5(encoded_data, encoded_len, md5);
memcpy(data_buf + 260, md5, 16);
memcpy(data_buf + 276, &encoded_len, 4);
memcpy(data_buf + 280, encoded_data, encoded_len);
free(encoded_data);
*packed_data = data_buf;
*packed_len = data_len;
return 0;
}
free(compressed_data);
return -1;
}
return -1;
}
return -1;
}
......@@ -14,16 +14,16 @@
char* http_PostConnect(JNIEnv *env, char* url, unsigned char* post_Body, size_t len){
int flag = 0;
jbyteArray body_Array = (*env)->NewByteArray(env, len);
(*env)->SetByteArrayRegion(env, body_Array, 0, 0, post_Body);
(*env)->SetByteArrayRegion(env, body_Array, 0, len, post_Body);
// jstring body = (*env)->NewStringUTF(env,post_Body);
// jstring body_Type = (*env)->NewStringUTF(env,"UTF-8");
// jarray body_Array = (wdCallObjectMethod(env, body, "getBytes","(Ljava/lang/String;)[B", body_Type));
char* result=NULL;
for(int i=0;i<3;++i){
// for(int i=0;i<3;++i){
jobject connection=NULL;
jobject bufferedReader=NULL;
jobject strUrl=(*env)->NewStringUTF(env, url);
jobject url=wdNewObject(env,"java/net/URL","(Ljava/lang/String;)V",strUrl);
jobject url_jstr=wdNewObject(env,"java/net/URL","(Ljava/lang/String;)V",strUrl);
jobject proxy=wdGetStaticObjectField(env,"java/net/Proxy","NO_PROXY","Ljava/net/Proxy;");
if(proxy!=0){
......@@ -31,7 +31,7 @@ char* http_PostConnect(JNIEnv *env, char* url, unsigned char* post_Body, size_t
jstring proxy_Type_String=(jstring)wdCallObjectMethod(env,proxy_Type,"toString","()Ljava/lang/String;");
char* proxy_Type_Str=(char* )(*env)->GetStringUTFChars(env,proxy_Type_String,0);
if(strstr(proxy_Type_Str,"DIRECT")!=NULL){
connection=wdCallObjectMethod(env,url,"openConnection","(Ljava/net/Proxy;)Ljava/net/URLConnection;",proxy);
connection=wdCallObjectMethod(env,url_jstr,"openConnection","(Ljava/net/Proxy;)Ljava/net/URLConnection;",proxy);
jstring post=(*env)->NewStringUTF(env,"POST");
wdCallVoidMethod(env,connection,"setRequestMethod","(Ljava/lang/String;)V",post);
jstring type=(*env)->NewStringUTF(env,"Content-Type");
......@@ -43,9 +43,9 @@ char* http_PostConnect(JNIEnv *env, char* url, unsigned char* post_Body, size_t
wdCallVoidMethod(env,connection,"setDoOutput",
"(Z)V",1);
wdCallVoidMethod(env,connection,"setConnectTimeout",
"(I)V",5000);
"(I)V", 5000);
wdCallVoidMethod(env,connection,"setReadTimeout",
"(I)V",5000);
"(I)V", 5000);
jobject outPutStream=wdCallObjectMethod(env,connection,"getOutputStream","()Ljava/io/OutputStream;");
jobject out=wdNewObject(env,"java/io/DataOutputStream","(Ljava/io/OutputStream;)V",outPutStream);
wdCallVoidMethod(env,out,"write","([B)V", body_Array);
......@@ -74,7 +74,6 @@ char* http_PostConnect(JNIEnv *env, char* url, unsigned char* post_Body, size_t
char* result_Str_UTF8= (char*)((*env)->GetStringUTFChars(env,result_String, 0));
result=(char*)calloc(1,strlen(result_Str_UTF8) + 1);
memcpy(result,result_Str_UTF8,strlen(result_Str_UTF8));
(*env)->ReleaseStringUTFChars(env,result_String, result_Str_UTF8);
(*env)->DeleteLocalRef(env,result_String);
(*env)->DeleteLocalRef(env,line);
......@@ -90,28 +89,14 @@ char* http_PostConnect(JNIEnv *env, char* url, unsigned char* post_Body, size_t
(*env)->DeleteLocalRef(env,type_Content);
(*env)->DeleteLocalRef(env,type);
(*env)->DeleteLocalRef(env,post);
}else{
//proxy被修改
}
(*env)->ReleaseStringUTFChars(env,proxy_Type_String, proxy_Type_Str);
(*env)->DeleteLocalRef(env,proxy_Type);
(*env)->DeleteLocalRef(env,proxy_Type_String);
}
(*env)->DeleteLocalRef(env,proxy);
(*env)->DeleteLocalRef(env,url);
(*env)->DeleteLocalRef(env,strUrl);
(*env)->DeleteLocalRef(env,bufferedReader);
(*env)->DeleteLocalRef(env,connection);
if(flag == 1){
break;
}else{
sleep(1);
}
}
// }
(*env)->DeleteLocalRef(env, body_Array);
return result;
}
\ No newline at end of file
//
// Created by Fear1ess on 2021/4/27.
//
#ifndef REYUNSDK_DATA_HANDLER_H
#define REYUNSDK_DATA_HANDLER_H
int pack_data(unsigned char* raw, int len, unsigned char** packed_data, int* packed_len);
void free_data(unsigned char* data);
int unpack_data(unsigned char* raw, int len, unsigned char** unpacked_data, int* unpacked_len);
#endif //REYUNSDK_DATA_HANDLER_H
......@@ -8,6 +8,6 @@
#include "string.h"
void testWriteRSA2PEM();
void rsa_Encoder_Base64(char *src, int src_len, char** out);
void rsa_Encoder_Base64(char *src, int src_len, char* out, int* outsize);
#endif //MYTESTACC_RSAENCODE_H
......@@ -12,6 +12,12 @@
#include <android/log.h>
#include <mybase64.h>
char* myDecode( char *content, int size);
char* public_key = "-----BEGIN PUBLIC KEY-----\n"
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWuIwr3n0x5rgCVhPtwJVeg0NS\n"
"QBOcu+nND4rNIqqBc3+eQLuTDn19IfzX0n2lGRsY9x9EUn8WA4TTWA+Q4he/5NlE\n"
"FBWaW2O7fz25pIeoilnTtJVtfc8KU1+i8rCKtWDS9jvabJWjDVcVIcASArkA8TL9\n"
"AKqRAzC0RlTLRpj5jQIDAQAB\n"
"-----END PUBLIC KEY-----";
char* m_RSAPublicKey=(char*)"oq4FoqCv_kUJMz8qk4-dkrhvM-uNE-Fr5q4Foq4FvHCJ_4S8x4UvKkC6OMhuA5Pzk0mw6kmw_4K9hhnjOR_eOh6vZ5WsWrKmLhuXMauE_k1wq-nFK0hEUlTCMrSlZk-GcyWycHnMTk1MZHsVx-CCWRQ1huSwT57fhRnVxyrMAkPrM0EPAkFhc5FPLyUDAH1nc4SaOy7l6rUjTMkVW5nf6Qj0qeuW606yq-Fhxrhqo068GCJvc-rk6CWlkC_8xCTmh4gST98m_MkCAuunL-PEoCSoLh_0K5UWKHr8_M4Goq4FoqCrMHKdkuW8Er8hKHPJKD8o_hHFoq4FoK";
char* m_RSAPrivateKey="-----BEGIN RSA PRIVATE KEY-----\n"
"MIICXAIBAAKBgQDGPeXspW0p/3jfxg91F4fbXPVQWnJeE/ImIC8hxDaBx5gnh8LO\n"
......@@ -63,15 +69,16 @@ RSA * initPublicKey(char* publicKeyPath){
//获取公钥
char* publicKey= myDecode(m_RSAPublicKey,strlen(m_RSAPublicKey));
LOGV("测试代码->公钥解密后:%s",publicKey);
pubio = BIO_new_mem_buf(publicKey,strlen(m_RSAPublicKey));
free(publicKey);
pubio = BIO_new(BIO_s_mem());
BIO_puts(pubio, public_key);
// pubio = BIO_new_file(publicKeyPath, "rb");
if(pubio==NULL){
RSA_free(pubkey);
return NULL;
}
pubkey = PEM_read_bio_RSAPublicKey(pubio, &pubkey, NULL, NULL);
pubkey = PEM_read_bio_RSA_PUBKEY(pubio, NULL, NULL, NULL);
BIO_free(pubio);
free(publicKey);
return pubkey;
}
......@@ -97,7 +104,7 @@ int RSADecoder(RSA *pubkey,RSA *prikey,char* src,int src_Size,char*des,int* des_
//初始化变量
unsigned char plain[128]= {0};
unsigned char cipper[128]={0};
size_t outl=128;
size_t outl=0;
memcpy(plain,src,src_Size);
//打印源字符串的16进制
// unsigned char temp[500]={0};
......@@ -105,12 +112,13 @@ int RSADecoder(RSA *pubkey,RSA *prikey,char* src,int src_Size,char*des,int* des_
//进行加密
outl=RSA_public_encrypt(strlen((char*)plain),plain,cipper,pubkey,RSA_PKCS1_PADDING);
//打印密文字符串的16进制
if(*des_Size<=outl*2||outl<128){
if(outl<128){
return 0;
}
memcpy(des,cipper,outl);
*des_Size=outl;
//----------------------------------------
/*
//进行解密的测试代码
char temp[500]={0};
for(int i=0;i<outl;++i){
......@@ -125,7 +133,7 @@ int RSADecoder(RSA *pubkey,RSA *prikey,char* src,int src_Size,char*des,int* des_
LOGV("测试代码->RSA加密后,重新私钥解密:%s", newplain);
RSA_free(prikey);
//------------------------------------------
*/
RSA_free(pubkey);
return 1;
......@@ -133,7 +141,7 @@ int RSADecoder(RSA *pubkey,RSA *prikey,char* src,int src_Size,char*des,int* des_
void rsa_Encoder_Base64(char *src,int src_len,char**out){
void rsa_Encoder_Base64(char *src,int src_len,char* out,int* out_size){
if(src==NULL||src_len<=0||src_len>=128){
return;
}
......@@ -149,7 +157,8 @@ void rsa_Encoder_Base64(char *src,int src_len,char**out){
//进行加密
char result[500]={0};
int outSize=sizeof(result);
int flag= RSADecoder(pubkey,prikey,src,src_len,result,&outSize);
RSADecoder(pubkey,prikey,src,src_len,out,out_size);
/*
if(flag>0){
//------------BASE64 加密
//计算加密后的长度
......@@ -176,7 +185,7 @@ void rsa_Encoder_Base64(char *src,int src_len,char**out){
free(decode);
}else{
LOGV("加密失败");
}
}*/
}
char* myDecode( char *content, int size){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment