Compare commits
9 Commits
contacts-v
...
appbase-v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5099d00050 | ||
|
|
515d14e896 | ||
|
|
f630e27ed8 | ||
|
|
cd7ed01216 | ||
|
|
bb24bbfbd1 | ||
|
|
2ba2f88510 | ||
|
|
db3a3644a8 | ||
|
|
556bfa7024 | ||
|
|
4842a1ec30 |
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Tue Jun 03 13:40:08 HKT 2025
|
#Wed Jun 04 15:04:39 HKT 2025
|
||||||
stageCount=5
|
stageCount=8
|
||||||
libraryProject=libappbase
|
libraryProject=libappbase
|
||||||
baseVersion=15.8
|
baseVersion=15.8
|
||||||
publishVersion=15.8.4
|
publishVersion=15.8.7
|
||||||
buildCount=0
|
buildCount=0
|
||||||
baseBetaVersion=15.8.5
|
baseBetaVersion=15.8.8
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:theme="@style/MyAPPBaseTheme"
|
android:theme="@style/MyAPPBaseTheme"
|
||||||
android:resizeableActivity="true"
|
android:resizeableActivity="true"
|
||||||
android:process=":App">
|
android:process=":App"
|
||||||
|
android:networkSecurityConfig="@xml/network_security_config">
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
@@ -39,7 +40,8 @@
|
|||||||
android:resizeableActivity="true"
|
android:resizeableActivity="true"
|
||||||
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"/>
|
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"/>
|
||||||
|
|
||||||
<activity android:name=".activities.New2Activity"
|
<activity
|
||||||
|
android:name=".activities.New2Activity"
|
||||||
android:label="New2Activity"
|
android:label="New2Activity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:resizeableActivity="true"
|
android:resizeableActivity="true"
|
||||||
@@ -74,7 +76,8 @@
|
|||||||
|
|
||||||
<service android:name=".services.AssistantService"/>
|
<service android:name=".services.AssistantService"/>
|
||||||
|
|
||||||
<receiver android:name="cc.winboll.studio.appbase.receivers.MainReceiver"
|
<receiver
|
||||||
|
android:name="cc.winboll.studio.appbase.receivers.MainReceiver"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
@@ -105,7 +108,8 @@
|
|||||||
|
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
<receiver android:name=".receivers.APPNewsWidgetClickListener"
|
<receiver
|
||||||
|
android:name=".receivers.APPNewsWidgetClickListener"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
@@ -122,7 +126,6 @@
|
|||||||
android:name="android.max_aspect"
|
android:name="android.max_aspect"
|
||||||
android:value="4.0"/>
|
android:value="4.0"/>
|
||||||
|
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -62,6 +62,11 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
if(item.getItemId() == R.id.item_yun) {
|
||||||
|
GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(this, cc.winboll.studio.libappbase.activities.YunActivity.class);
|
||||||
|
} else if(item.getItemId() == R.id.item_logon) {
|
||||||
|
GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(this, cc.winboll.studio.libappbase.activities.LogonActivity.class);
|
||||||
|
}
|
||||||
// 在switch语句中处理每个ID,并在处理完后返回true,未处理的情况返回false。
|
// 在switch语句中处理每个ID,并在处理完后返回true,未处理的情况返回false。
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,14 @@
|
|||||||
android:id="@+id/item_home"
|
android:id="@+id/item_home"
|
||||||
android:title="HOME"
|
android:title="HOME"
|
||||||
android:icon="@drawable/ic_winboll"/>
|
android:icon="@drawable/ic_winboll"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/item_yun"
|
||||||
|
android:title="YUN"
|
||||||
|
android:icon="@drawable/ic_winboll"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/item_logon"
|
||||||
|
android:title="Logon"
|
||||||
|
android:icon="@drawable/ic_winboll"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/item_log"
|
android:id="@+id/item_log"
|
||||||
android:title="LOG"
|
android:title="LOG"
|
||||||
|
|||||||
12
appbase/src/main/res/xml/network_security_config.xml
Normal file
12
appbase/src/main/res/xml/network_security_config.xml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<network-security-config>
|
||||||
|
<!-- 允许访问 winboll.cc 及其子域名(原配置) -->
|
||||||
|
<domain-config cleartextTrafficPermitted="true">
|
||||||
|
<domain includeSubdomains="true">winboll.cc</domain>
|
||||||
|
</domain-config>
|
||||||
|
|
||||||
|
<!-- **新增:允许访问 IP 地址 10.8.0.250** -->
|
||||||
|
<domain-config cleartextTrafficPermitted="true">
|
||||||
|
<domain includeSubdomains="false">10.8.0.250</domain> <!-- 不包含子域名 -->
|
||||||
|
</domain-config>
|
||||||
|
</network-security-config>
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Wed Jun 04 00:08:54 HKT 2025
|
#Tue May 20 13:02:18 HKT 2025
|
||||||
stageCount=4
|
stageCount=3
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.3
|
baseVersion=15.3
|
||||||
publishVersion=15.3.3
|
publishVersion=15.3.2
|
||||||
buildCount=0
|
buildCount=0
|
||||||
baseBetaVersion=15.3.4
|
baseBetaVersion=15.3.3
|
||||||
|
|||||||
@@ -22,4 +22,7 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api fileTree(dir: 'libs', include: ['*.jar'])
|
api fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
|
// 网络连接类库
|
||||||
|
api 'com.squareup.okhttp3:okhttp:4.4.1'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Tue Jun 03 13:40:01 HKT 2025
|
#Wed Jun 04 15:04:39 HKT 2025
|
||||||
stageCount=5
|
stageCount=8
|
||||||
libraryProject=libappbase
|
libraryProject=libappbase
|
||||||
baseVersion=15.8
|
baseVersion=15.8
|
||||||
publishVersion=15.8.4
|
publishVersion=15.8.7
|
||||||
buildCount=0
|
buildCount=0
|
||||||
baseBetaVersion=15.8.5
|
baseBetaVersion=15.8.8
|
||||||
|
|||||||
@@ -103,6 +103,10 @@
|
|||||||
|
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
|
<activity android:name="cc.winboll.studio.libappbase.activities.YunActivity"/>
|
||||||
|
|
||||||
|
<activity android:name="cc.winboll.studio.libappbase.activities.LogonActivity"/>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package cc.winboll.studio.libappbase.activities;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import cc.winboll.studio.libappbase.LogView;
|
||||||
|
import cc.winboll.studio.libappbase.R;
|
||||||
|
import cc.winboll.studio.libappbase.utils.RSAUtils;
|
||||||
|
import cc.winboll.studio.libappbase.winboll.IWinBoLLActivity;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen<zhangsken@188.com>
|
||||||
|
* @Date 2025/06/04 13:29
|
||||||
|
* @Describe 用户登录框
|
||||||
|
*/
|
||||||
|
public class LogonActivity extends Activity implements IWinBoLLActivity {
|
||||||
|
|
||||||
|
public static final String TAG = "LogonActivity";
|
||||||
|
|
||||||
|
LogView mLogView;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Activity getActivity() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_logon);
|
||||||
|
mLogView = findViewById(R.id.logview);
|
||||||
|
mLogView.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
mLogView.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onTestRSA(View view) {
|
||||||
|
LogUtils.d(TAG, "onTestRSA");
|
||||||
|
String keyPath = getFilesDir() + "/home/keys/"; // 密钥保存路径
|
||||||
|
LogUtils.d(TAG, String.format("keyPath %s", keyPath));
|
||||||
|
RSAUtils utils = RSAUtils.getInstance();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 测试 1:首次生成密钥对
|
||||||
|
LogUtils.d(TAG, "==== 首次生成密钥对 ====");
|
||||||
|
if (utils.keysExist(keyPath)) {
|
||||||
|
LogUtils.d(TAG, "密钥对已生成");
|
||||||
|
} else {
|
||||||
|
utils.generateAndSaveKeys(keyPath);
|
||||||
|
LogUtils.d(TAG, "密钥对生成成功,保存至:" + keyPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试 2:获取密钥对(自动读取已生成的文件)
|
||||||
|
KeyPair keyPair = utils.getOrGenerateKeys(keyPath);
|
||||||
|
PublicKey publicKey = keyPair.getPublic();
|
||||||
|
PrivateKey privateKey = keyPair.getPrivate();
|
||||||
|
|
||||||
|
// 打印密钥信息
|
||||||
|
LogUtils.d(TAG, "\n==== 密钥信息 ====");
|
||||||
|
LogUtils.d(TAG, "公钥算法:" + publicKey.getAlgorithm());
|
||||||
|
LogUtils.d(TAG, "公钥编码长度:" + publicKey.getEncoded().length + "字节");
|
||||||
|
LogUtils.d(TAG, "私钥算法:" + privateKey.getAlgorithm());
|
||||||
|
LogUtils.d(TAG, "私钥编码长度:" + privateKey.getEncoded().length + "字节");
|
||||||
|
|
||||||
|
// 测试 3:重复调用时检查是否复用文件
|
||||||
|
LogUtils.d(TAG, "\n==== 二次调用 ====");
|
||||||
|
KeyPair reusedPair = utils.getOrGenerateKeys(keyPath);
|
||||||
|
LogUtils.d(TAG, "是否为同一公钥:" + (publicKey.equals(reusedPair.getPublic()))); // true(单例引用)
|
||||||
|
LogUtils.d(TAG, "操作完成");
|
||||||
|
|
||||||
|
String testMessage = "Hello, RSA Encryption!";
|
||||||
|
|
||||||
|
// 1. 获取或生成密钥对
|
||||||
|
PublicKey publicKeyReused = reusedPair.getPublic();
|
||||||
|
PrivateKey privateKeyReused = reusedPair.getPrivate();
|
||||||
|
|
||||||
|
// 2. 公钥加密
|
||||||
|
byte[] encryptedData = utils.encryptWithPublicKey(testMessage, publicKeyReused);
|
||||||
|
LogUtils.d(TAG, "加密后数据(字节长度):" + encryptedData.length);
|
||||||
|
|
||||||
|
// 3. 私钥解密
|
||||||
|
String decryptedMessage = utils.decryptWithPrivateKey(encryptedData, privateKeyReused);
|
||||||
|
LogUtils.d(TAG, "解密结果:" + decryptedMessage);
|
||||||
|
|
||||||
|
// 4. 验证解密是否成功
|
||||||
|
if (testMessage.equals(decryptedMessage)) {
|
||||||
|
LogUtils.d(TAG, "加密解密测试通过!");
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "测试失败:内容不一致");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package cc.winboll.studio.libappbase.activities;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
import cc.winboll.studio.libappbase.BuildConfig;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import cc.winboll.studio.libappbase.R;
|
||||||
|
import cc.winboll.studio.libappbase.winboll.IWinBoLLActivity;
|
||||||
|
import java.io.IOException;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import android.widget.RadioButton;
|
||||||
|
import cc.winboll.studio.libappbase.LogView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen<zhangsken@188.com>
|
||||||
|
* @Date 2025/06/04 11:06
|
||||||
|
* @Describe 云宝云
|
||||||
|
*/
|
||||||
|
public class YunActivity extends Activity implements IWinBoLLActivity {
|
||||||
|
|
||||||
|
public static final String TAG = "YunActivity";
|
||||||
|
|
||||||
|
public static final String DEBUG_HOST = "http://10.8.0.250:456";
|
||||||
|
public static final String YUN_HOST = "https://yun.winboll.cc";
|
||||||
|
|
||||||
|
String mHost = "";
|
||||||
|
RadioButton mrbYunHost;
|
||||||
|
RadioButton mrbDebugHost;
|
||||||
|
LogView mLogView;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Activity getActivity() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_yun);
|
||||||
|
mLogView = findViewById(R.id.logview);
|
||||||
|
mLogView.start();
|
||||||
|
|
||||||
|
mHost = BuildConfig.DEBUG ? DEBUG_HOST: YUN_HOST;
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
mrbYunHost = findViewById(R.id.rb_yunhost);
|
||||||
|
mrbDebugHost = findViewById(R.id.rb_debughost);
|
||||||
|
mrbYunHost.setChecked(!BuildConfig.DEBUG);
|
||||||
|
mrbDebugHost.setChecked(BuildConfig.DEBUG);
|
||||||
|
} else {
|
||||||
|
findViewById(R.id.ll_hostbar).setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onSwitchHost(View view) {
|
||||||
|
if (view.getId() == R.id.rb_yunhost) {
|
||||||
|
mrbDebugHost.setChecked(false);
|
||||||
|
mHost = YUN_HOST;
|
||||||
|
} else if (view.getId() == R.id.rb_debughost) {
|
||||||
|
mrbYunHost.setChecked(false);
|
||||||
|
mHost = DEBUG_HOST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
mLogView.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onTestYun(View view) {
|
||||||
|
LogUtils.d(TAG, "onTestYun");
|
||||||
|
(new Thread(new Runnable(){
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
testYun();
|
||||||
|
}
|
||||||
|
})).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testYun() {
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(mHost + "/backups/")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Response response = null;
|
||||||
|
try {
|
||||||
|
response = client.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
String responseBody = "";
|
||||||
|
if (response.body() != null) {
|
||||||
|
responseBody = response.body().string();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 正则匹配:任意主机名 -> Test OK(主机名部分匹配非空字符)
|
||||||
|
boolean isMatch = responseBody.matches(".+? -> Test OK");
|
||||||
|
|
||||||
|
if (isMatch) {
|
||||||
|
LogUtils.d(TAG, responseBody);
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "响应内容不匹配,内容:" + responseBody);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "请求失败,状态码:" + response.code());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogUtils.d(TAG, "读取响应体失败:" + e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtils.d(TAG, "异常:" + e.getMessage());
|
||||||
|
e.printStackTrace(); // Java 7 需显式打印堆栈
|
||||||
|
} finally {
|
||||||
|
// 手动关闭 Response(Java 7 不支持 try-with-resources)
|
||||||
|
if (response != null && response.body() != null) {
|
||||||
|
response.body().close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,8 @@ public class APPModel extends BaseBean {
|
|||||||
// 应用是否处于正在调试状态
|
// 应用是否处于正在调试状态
|
||||||
//
|
//
|
||||||
boolean isDebuging = false;
|
boolean isDebuging = false;
|
||||||
|
// 用本机 RSA 加密后保存的令牌
|
||||||
|
String rsaToken = "";
|
||||||
|
|
||||||
public APPModel() {
|
public APPModel() {
|
||||||
this.isDebuging = false;
|
this.isDebuging = false;
|
||||||
@@ -26,6 +28,14 @@ public class APPModel extends BaseBean {
|
|||||||
this.isDebuging = isDebuging;
|
this.isDebuging = isDebuging;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRsaToken(String rsaToken) {
|
||||||
|
this.rsaToken = rsaToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRsaToken() {
|
||||||
|
return rsaToken;
|
||||||
|
}
|
||||||
|
|
||||||
public void setIsDebuging(boolean isDebuging) {
|
public void setIsDebuging(boolean isDebuging) {
|
||||||
this.isDebuging = isDebuging;
|
this.isDebuging = isDebuging;
|
||||||
}
|
}
|
||||||
@@ -43,6 +53,7 @@ public class APPModel extends BaseBean {
|
|||||||
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
|
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
|
||||||
super.writeThisToJsonWriter(jsonWriter);
|
super.writeThisToJsonWriter(jsonWriter);
|
||||||
jsonWriter.name("isDebuging").value(isDebuging());
|
jsonWriter.name("isDebuging").value(isDebuging());
|
||||||
|
jsonWriter.name("rsaToken").value(getRsaToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -50,6 +61,8 @@ public class APPModel extends BaseBean {
|
|||||||
if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
|
if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
|
||||||
if (name.equals("isDebuging")) {
|
if (name.equals("isDebuging")) {
|
||||||
setIsDebuging(jsonReader.nextBoolean());
|
setIsDebuging(jsonReader.nextBoolean());
|
||||||
|
} else if (name.equals("rsaToken")) {
|
||||||
|
setRsaToken(jsonReader.nextString());
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
package cc.winboll.studio.libappbase.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen<zhangsken@188.com>
|
||||||
|
* @Date 2025/06/04 13:36
|
||||||
|
* @Describe RSA加密工具
|
||||||
|
*/
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
|
||||||
|
public class RSAUtils {
|
||||||
|
public static final String TAG = "RSAUtils";
|
||||||
|
private static final RSAUtils INSTANCE = new RSAUtils();
|
||||||
|
private static final int KEY_SIZE = 2048;
|
||||||
|
private static final String KEY_ALGORITHM = "RSA";
|
||||||
|
private static final String PUBLIC_KEY_FILE = "public.key";
|
||||||
|
private static final String PRIVATE_KEY_FILE = "private.key";
|
||||||
|
|
||||||
|
private RSAUtils() {}
|
||||||
|
|
||||||
|
public static RSAUtils getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查密钥文件是否存在
|
||||||
|
*/
|
||||||
|
public boolean keysExist(String path) {
|
||||||
|
File publicKeyFile = new File(path + PUBLIC_KEY_FILE);
|
||||||
|
File privateKeyFile = new File(path + PRIVATE_KEY_FILE);
|
||||||
|
return publicKeyFile.exists() && privateKeyFile.exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成密钥对并保存为字节文件
|
||||||
|
*/
|
||||||
|
public void generateAndSaveKeys(String path) throws Exception {
|
||||||
|
LogUtils.d(TAG, "正在生成密钥对");
|
||||||
|
// 生成密钥对
|
||||||
|
KeyPairGenerator generator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
|
||||||
|
generator.initialize(KEY_SIZE);
|
||||||
|
KeyPair keyPair = generator.generateKeyPair();
|
||||||
|
|
||||||
|
// 保存公钥(X.509字节)
|
||||||
|
saveKey(path, PUBLIC_KEY_FILE, keyPair.getPublic().getEncoded());
|
||||||
|
// 保存私钥(PKCS#8字节)
|
||||||
|
saveKey(path, PRIVATE_KEY_FILE, keyPair.getPrivate().getEncoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取密钥对(存在则读取,不存在则生成)
|
||||||
|
*/
|
||||||
|
public KeyPair getOrGenerateKeys(String path) throws Exception {
|
||||||
|
if (keysExist(path)) {
|
||||||
|
LogUtils.d(TAG, "密钥对已存在");
|
||||||
|
return readKeysFromFile(path);
|
||||||
|
} else {
|
||||||
|
LogUtils.d(TAG, "未生成密钥对");
|
||||||
|
generateAndSaveKeys(path);
|
||||||
|
return readKeysFromFile(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从文件读取密钥对
|
||||||
|
*/
|
||||||
|
private KeyPair readKeysFromFile(String path) throws Exception {
|
||||||
|
LogUtils.d(TAG, "正在读取密钥对");
|
||||||
|
byte[] publicKeyBytes = Files.readAllBytes(Paths.get(path + PUBLIC_KEY_FILE));
|
||||||
|
byte[] privateKeyBytes = Files.readAllBytes(Paths.get(path + PRIVATE_KEY_FILE));
|
||||||
|
|
||||||
|
X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKeyBytes);
|
||||||
|
PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKeyBytes);
|
||||||
|
|
||||||
|
KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||||
|
PublicKey publicKey = factory.generatePublic(publicSpec);
|
||||||
|
PrivateKey privateKey = factory.generatePrivate(privateSpec);
|
||||||
|
|
||||||
|
return new KeyPair(publicKey, privateKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用保存密钥字节方法
|
||||||
|
*/
|
||||||
|
private void saveKey(String path, String fileName, byte[] keyBytes) throws IOException {
|
||||||
|
File dir = new File(path);
|
||||||
|
if (!dir.exists()) dir.mkdirs(); // 自动创建目录
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(path + fileName)) {
|
||||||
|
fos.write(keyBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公钥加密
|
||||||
|
*/
|
||||||
|
public byte[] encryptWithPublicKey(String plainText, PublicKey publicKey) throws Exception {
|
||||||
|
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM + "/ECB/PKCS1Padding");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
|
return cipher.doFinal(plainText.getBytes("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私钥解密
|
||||||
|
*/
|
||||||
|
public String decryptWithPrivateKey(byte[] encryptedData, PrivateKey privateKey) throws Exception {
|
||||||
|
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM + "/ECB/PKCS1Padding");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
byte[] decryptedBytes = cipher.doFinal(encryptedData);
|
||||||
|
return new String(decryptedBytes, "UTF-8");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
37
libappbase/src/main/res/layout/activity_logon.xml
Normal file
37
libappbase/src/main/res/layout/activity_logon.xml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="right">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Test RSA"
|
||||||
|
android:onClick="onTestRSA"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1.0">
|
||||||
|
|
||||||
|
<cc.winboll.studio.libappbase.LogView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/logview"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
63
libappbase/src/main/res/layout/activity_yun.xml
Normal file
63
libappbase/src/main/res/layout/activity_yun.xml
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="right"
|
||||||
|
android:gravity="right"
|
||||||
|
android:padding="10dp"
|
||||||
|
android:id="@+id/ll_hostbar">
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="10.8.0.250:456"
|
||||||
|
android:id="@+id/rb_debughost"
|
||||||
|
android:onClick="onSwitchHost"/>
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="yun.winboll.cc"
|
||||||
|
android:id="@+id/rb_yunhost"
|
||||||
|
android:onClick="onSwitchHost"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="right"
|
||||||
|
android:gravity="right">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="TestYun"
|
||||||
|
android:onClick="onTestYun"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1.0">
|
||||||
|
|
||||||
|
<cc.winboll.studio.libappbase.LogView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/logview"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
Reference in New Issue
Block a user