APP端实现证书请求批量生成

main
Matthew 12 months ago
parent a159ab00ac
commit faf981d40d

@ -2,7 +2,13 @@ package com.xypower.secapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
@ -11,6 +17,8 @@ import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.dowse.devicesdk.DsDeviceSdk;
@ -19,33 +27,46 @@ import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class CertActivity extends AppCompatActivity {
private static String TAG = "CERTTAG";
private final static String ACTION_IMP_PUBKEY = "imp_pubkey";
private final static String ACTION_EXP_PUBKEY = "exp_pubkey";
private final static String ACTION_EXP_PRIKEY = "exp_prikey";
private final static String ACTION_GEN_KEYS = "gen_keys";
private final static String ACTION_CERT_REQ = "cert_req";
private final static String ACTION_CERT_REQ_BATCH = "batch_cert_req";
private final static String ACTION_IMP_PUBKEY = "com.xypower.secapp.imp_pubkey";
private final static String ACTION_EXP_PUBKEY = "com.xypower.secapp.exp_pubkey";
private final static String ACTION_EXP_PRIKEY = "com.xypower.secapp.exp_prikey";
private final static String ACTION_GEN_KEYS = "com.xypower.secapp.gen_keys";
private final static String ACTION_CERT_REQ = "com.xypower.secapp.cert_req";
private final static String ACTION_CERT_REQ_BATCH = "com.xypower.secapp.batch_cert_req";
private final static String ACTION_EXIT_APP = "com.xypower.secapp.exit";
private static long AUTO_CLOSE_TIMEOUT = 200;
private Handler mHandler = null;
TextView mTextView = null;
private CommandReceiver mCommandReceiver;
private Thread mCommandThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cert);
int res = 0;
boolean bres = false;
mCommandReceiver = new CommandReceiver();
IntentFilter intentFilter = new IntentFilter(ACTION_CERT_REQ);
intentFilter.addAction(ACTION_EXIT_APP);
intentFilter.addAction(ACTION_IMP_PUBKEY);
intentFilter.addAction(ACTION_EXP_PUBKEY);
intentFilter.addAction(ACTION_EXP_PRIKEY);
intentFilter.addAction(ACTION_GEN_KEYS);
intentFilter.addAction(ACTION_CERT_REQ_BATCH);
getApplicationContext().registerReceiver(mCommandReceiver, intentFilter, /* Context.RECEIVER_EXPORTED | */Context.RECEIVER_VISIBLE_TO_INSTANT_APPS);
DsDeviceSdk dsSdk = DsDeviceSdk.getInstance();
dsSdk.init();
@ -53,7 +74,12 @@ public class CertActivity extends AppCompatActivity {
res = dsSdk.mcuInit();
res = dsSdk.mcuOpen();
boolean bres = dsSdk.mcuPowerOffCPR();
bres = dsSdk.mcuPowerOffCPR();
try {
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
}
bres = dsSdk.mcuPowerOnCPR();
// String v = dsSdk.nrsecGetVersion();
@ -77,17 +103,39 @@ public class CertActivity extends AppCompatActivity {
mHandler = new Handler();
Spinner spinnerView = (Spinner) findViewById(R.id.format);
spinnerView.setSelection(1);
SharedPreferences sharedPreferences = getSharedPreferences("CertReq", MODE_PRIVATE);
setEditText(R.id.batch_no, sharedPreferences.getString("batchNo", ""));
setEditText(R.id.serial_no, sharedPreferences.getString("serialNo", ""));
setEditText(R.id.uid_start, sharedPreferences.getString("uidStart", ""));
setEditText(R.id.uid_end, sharedPreferences.getString("uidEnd", ""));
setSpinnerPosition(R.id.company, sharedPreferences.getInt("company", 0));
{
final Intent intent = getIntent();
if (intent != null) {
Thread th = new Thread(new Runnable() {
if (mCommandThread != null) {
try {
mCommandThread.interrupt();
} catch (Exception ex) {
}
}
mCommandThread = new Thread(new Runnable() {
@Override
public void run() {
handleCommand(intent);
try {
handleCommand(intent);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
th.start();
mCommandThread.start();
}
}
@ -129,20 +177,55 @@ public class CertActivity extends AppCompatActivity {
}
});
findViewById(R.id.output_dir).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView textView = (TextView)view;
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("label", textView.getText().toString());
clipboardManager.setPrimaryClip(clipData);
}
});
findViewById(R.id.btnGenCertReq).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int keyPairIndex = getItemData(R.id.keypair_index);
int uidStart = getIntegerData(R.id.uid_start);
int uidEnd = getIntegerData(R.id.uid_end);
int type = getItemData(R.id.format);
String sn = getStringData(R.id.serial_no);
String bn = getStringData(R.id.batch_no);
String company = getItemStringData(R.id.company);
String keyPrefix = getStringData(R.id.key_prefix);
if (keyPrefix == null) {
keyPrefix = "";
}
Spinner spinnerView = (Spinner) findViewById(R.id.company);
SharedPreferences sharedPreferences = getSharedPreferences("CertReq", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("batchNo", bn);
editor.putString("serialNo", sn);
editor.putString("uidStart", Integer.toString(uidStart));
editor.putString("uidEnd", Integer.toString(uidEnd));
editor.putInt("company", spinnerView.getSelectedItemPosition());
editor.commit();
Intent intent = new Intent();
intent.putExtra("index", 0);
intent.putExtra("index", keyPairIndex);
intent.putExtra("action", ACTION_CERT_REQ_BATCH);
intent.putExtra("start", 701);
intent.putExtra("end", 710);
intent.putExtra("type", 0);
intent.putExtra("sn", "XY2024ICQ111");
intent.putExtra("subject", "C=CN,ST=SH,L=SH,O=SGCC,CN=123456_%%IDX%%_XY2024ICQ111%%IDX%%,OU=NC");
intent.putExtra("pubKeyFile", "device_pub.key");
intent.putExtra("priKeyFile", "device_pri.key");
intent.putExtra("start", uidStart);
intent.putExtra("end", uidEnd);
intent.putExtra("type", type);
intent.putExtra("sn", sn);
intent.putExtra("subject", "C=CN,ST=SH,L=SH,O=SGCC,OU=SGEP,OU=" + company + ",CN=" + bn + "_%%IDX%%_" + sn + "%%IDX%%");
intent.putExtra("pubKeyFile", keyPrefix + "pub.key");
intent.putExtra("priKeyFile", keyPrefix + "pri.key");
intent.putExtra("dnFile", "dn.txt");
File file = CertActivity.this.getFilesDir();
@ -151,12 +234,86 @@ public class CertActivity extends AppCompatActivity {
String absPath = file.getAbsolutePath();
intent.putExtra("path", absPath);
handleCommand(intent);
TextView outpathView = (TextView) findViewById(R.id.output_dir);
outpathView.setText(absPath);
final Intent finalIntent = intent;
if (mCommandThread != null) {
try {
mCommandThread.interrupt();
} catch (Exception ex) {
}
}
mCommandThread = new Thread(new Runnable() {
@Override
public void run() {
try {
CertActivity.this.handleCommand(finalIntent);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
mCommandThread.start();
}
});
//
}
protected int getItemData(int viewId) {
Spinner spinnerView = (Spinner) findViewById(viewId);
if (spinnerView != null) {
String text = (String)spinnerView.getSelectedItem();
text = text.substring(0, text.indexOf(" -"));
return Integer.parseInt(text);
}
return -1;
}
protected String getItemStringData(int viewId) {
Spinner spinnerView = (Spinner) findViewById(viewId);
if (spinnerView != null) {
String text = (String)spinnerView.getSelectedItem();
text = text.substring(0, text.indexOf(" -"));
return text;
}
return null;
}
protected void setEditText(int viewId, String text) {
EditText editView = (EditText) findViewById(viewId);
if (editView != null) {
editView.setText(TextUtils.isEmpty(text) ? "" : text);
}
}
protected void setSpinnerPosition(int viewId, int pos) {
Spinner spinnerView = (Spinner) findViewById(viewId);
if (spinnerView != null) {
spinnerView.setSelection(pos);
}
}
protected int getIntegerData(int viewId) {
EditText editView = (EditText) findViewById(viewId);
if (editView != null) {
String text = editView.getText().toString();
return Integer.parseInt(text);
}
return -1;
}
protected String getStringData(int viewId) {
EditText editView = (EditText) findViewById(viewId);
if (editView != null) {
String text = editView.getText().toString();
return text;
}
return null;
}
protected static void writeTextFile(String path, String content) {
FileOutputStream fileOutputStream = null;
try {
@ -340,14 +497,14 @@ public class CertActivity extends AppCompatActivity {
f = new File(f, "progress.txt");
writeTextFile(f.getAbsolutePath(), Integer.toString(idx - startIdx + 1));
final String fullSn = sn + idxStr;
final String finalSn = sn;
mHandler.post(new Runnable() {
@Override
public void run() {
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date dt = new Date();
mTextView.setText(format.format(dt) + " " + fullSn + " type=" + Integer.toString(type));
mTextView.setText(format.format(dt) + " " + finalSn + " type=" + Integer.toString(type));
}
});
}
@ -361,16 +518,15 @@ public class CertActivity extends AppCompatActivity {
} catch (Exception ex) {
}
} else if (TextUtils.equals(ACTION_EXIT_APP, action)) {
final Activity activity = CertActivity.this;
mHandler.post(new Runnable() {
@Override
public void run() {
activity.finish();
}
});
}
final Activity activity = this;
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// activity.finish();
System.exit(0);
}
}, AUTO_CLOSE_TIMEOUT);
}
}
@ -382,12 +538,20 @@ public class CertActivity extends AppCompatActivity {
file.getParentFile().mkdirs();
}
} catch (Exception ex) {
}
}
@Override
protected void onDestroy() {
if (mCommandThread != null) {
try {
mCommandThread.interrupt();
} catch (Exception ex) {
}
}
DsDeviceSdk dsSdk = DsDeviceSdk.getInstance();
int res = 0;
@ -398,6 +562,39 @@ public class CertActivity extends AppCompatActivity {
dsSdk.release();
getApplicationContext().unregisterReceiver(mCommandReceiver);
super.onDestroy();
}
public class CommandReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent) {
final Intent finalIntent = intent;
if (intent != null) {
if (mCommandThread != null) {
try {
mCommandThread.interrupt();
} catch (Exception ex) {
}
}
mCommandThread = new Thread(new Runnable() {
@Override
public void run() {
try {
handleCommand(finalIntent);
} catch (Exception ex) {
}
}
});
mCommandThread.start();
}
}
}
}

@ -4,54 +4,242 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".CertActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="32sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/keypair_index_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="密钥对索引号"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView"
/>
<Spinner
android:id="@+id/keypair_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:entries="@array/keypair_indexs"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@id/keypair_index_text"
app:layout_constraintTop_toTopOf="@+id/keypair_index_text"
app:layout_constraintBottom_toBottomOf="@+id/keypair_index_text" />
<TextView
android:id="@+id/format_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="格式"
app:layout_constraintStart_toEndOf="@id/keypair_index"
app:layout_constraintTop_toTopOf="@id/keypair_index_text"
/>
<Spinner
android:id="@+id/format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:entries="@array/formats"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@id/format_text"
app:layout_constraintTop_toTopOf="@+id/format_text"
app:layout_constraintBottom_toBottomOf="@+id/format_text" />
<TextView
android:id="@+id/batch_no_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="批次号"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/keypair_index_text"
/>
<EditText
android:id="@+id/batch_no"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none|numberDecimal"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@+id/batch_no_text"
app:layout_constraintTop_toTopOf="@+id/batch_no_text"
app:layout_constraintBottom_toBottomOf="@+id/batch_no_text" />
<TextView
android:id="@+id/sn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="序列号"
app:layout_constraintStart_toEndOf="@+id/batch_no"
app:layout_constraintTop_toTopOf="@+id/batch_no_text"
/>
<EditText
android:id="@+id/serial_no"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@+id/sn_text"
app:layout_constraintTop_toTopOf="@+id/batch_no" />
<TextView
android:id="@+id/uid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="设备唯一Id"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/batch_no_text"
/>
<EditText
android:id="@+id/uid_start"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none|numberDecimal"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@+id/uid_text"
app:layout_constraintTop_toTopOf="@+id/uid_text"
app:layout_constraintBottom_toBottomOf="@+id/uid_text" />
<TextView
android:id="@+id/uid_to_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="-"
app:layout_constraintStart_toEndOf="@id/uid_start"
app:layout_constraintTop_toTopOf="@+id/uid_text"
/>
<EditText
android:id="@+id/uid_end"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none|numberDecimal"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@+id/uid_to_text"
app:layout_constraintTop_toTopOf="@+id/uid_start" />
<TextView
android:id="@+id/key_prefix_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="密钥文件前缀"
app:layout_constraintStart_toEndOf="@id/uid_end"
app:layout_constraintTop_toTopOf="@+id/uid_text"
/>
<EditText
android:id="@+id/key_prefix"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none"
android:text="device_"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@+id/key_prefix_text"
app:layout_constraintTop_toTopOf="@+id/key_prefix_text"
app:layout_constraintBottom_toBottomOf="@+id/key_prefix_text" />
<TextView
android:id="@+id/company_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="省公司"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/uid_text"
/>
<Spinner
android:id="@+id/company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:entries="@array/companies"
android:layout_marginStart="8dp"
app:layout_constraintStart_toEndOf="@id/company_text"
app:layout_constraintTop_toTopOf="@+id/company_text"
app:layout_constraintBottom_toBottomOf="@+id/company_text" />
<Button
android:id="@+id/btnGenCertReq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="生成证书请求"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/company_text" />
<TextView
android:id="@+id/output_dir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text=""
app:layout_constraintStart_toEndOf="@id/btnGenCertReq"
app:layout_constraintTop_toTopOf="@+id/btnGenCertReq"
app:layout_constraintBottom_toBottomOf="@+id/btnGenCertReq"
/>
<Button
android:id="@+id/btnGenKeys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="Gen Keys"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnGenCertReq"
/>
<Button
android:id="@+id/btnExpPriKey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:text="Exp Pri Key"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGenKeys" />
app:layout_constraintStart_toEndOf="@id/btnGenKeys"
app:layout_constraintTop_toTopOf="@+id/btnGenKeys" />
<Button
android:id="@+id/btnExpPubKey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:text="Exp Pub Key"
app:layout_constraintLeft_toRightOf="@id/btnExpPriKey"
app:layout_constraintTop_toBottomOf="@+id/btnGenKeys" />
app:layout_constraintTop_toTopOf="@+id/btnGenKeys" />
<Button
android:id="@+id/btnGenCertReq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Gen Cert Request"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnExpPriKey" />
</android.support.constraint.ConstraintLayout>

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="companies">
<item>NC - 华北电网有限公司</item>
<item>TJ - 天津市电力公司</item>
<item>HE - 河北省电力公司</item>
<item>SX - 山西省电力公司</item>
<item>SD - 山东电力集团公司</item>
<item>BJ - 北京电力公司</item>
<item>NE - 东北电网有限公司</item>
<item>LN - 辽宁省电力有限公司</item>
<item>JL - 吉林省电力有限公司</item>
<item>HL - 黑龙江省电力有限公司</item>
<item>HD - 华东电网有限公司</item>
<item>SH - 上海市电力公司</item>
<item>JS - 江苏省电力公司</item>
<item>ZJ - 浙江省电力公司</item>
<item>AH - 安徽省电力公司</item>
<item>FJ - 福建省电力有限公司</item>
<item>CC - 华中电网有限公司</item>
<item>HB - 湖北省电力公司</item>
<item>HN - 湖南省电力公司</item>
<item>HA - 河南省电力公司</item>
<item>JX - 江西省电力公司</item>
<item>SC - 四川省电力公司</item>
<item>CQ - 重庆市电力公司</item>
<item>NW - 西北电网有限公司</item>
<item>SN - 陕西省电力公司</item>
<item>GS - 甘肃省电力公司</item>
<item>NX - 宁夏回族自治区电力公司</item>
<item>QH - 青海省电力公司</item>
<item>XJ - 新疆电力公司</item>
<item>MD - 内蒙古东部电力公司</item>
<item>XZ - 西藏自治区电力公司</item>
</string-array>
</resources>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="formats">
<item>0 - 格式一</item>
<item>1 - 格式二</item>
</string-array>
</resources>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="keypair_indexs">
<item>0 - 00</item>
<item>1 - 01</item>
<item>2 - 02</item>
<item>3 - 03</item>
</string-array>
</resources>
Loading…
Cancel
Save