Skip to content

Commit

Permalink
Add cert create template
Browse files Browse the repository at this point in the history
  • Loading branch information
timscriptov committed Jul 27, 2023
1 parent ae08307 commit cb243f1
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 54 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 60,12 @@
JksToPem.convert(new File("path/key.jks"), "password", "alias_password", new File("path/key.pk8"), new File("path/key.x509.pem"));
JksToPem.convert(new File("path/key.jks"), "password", "alias", "alias_password", new File("path/key.pk8"), new File("path/key.x509.pem"));
```

## Create keystore
```kotlin
CertCreator.createKeystoreAndKey("path/key.jks", "password", "alias", DistinguishedNameValues())
```

```java
CertCreator.createKeystoreAndKey("path/key.jks", "password", "alias", new DistinguishedNameValues());
```
192 changes: 146 additions & 46 deletions app/src/main/java/com/mcal/apksigner/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 13,12 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.mcal.apksigner.ApkSigner
import com.mcal.apksigner.CertCreator
import com.mcal.apksigner.JksToBks
import com.mcal.apksigner.JksToPem
import com.mcal.apksigner.app.databinding.ActivityMainBinding
import com.mcal.apksigner.app.filepicker.FilePickHelper
import com.mcal.apksigner.utils.DistinguishedNameValues
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -123,10 125,99 @@ class MainActivity : AppCompatActivity() {
binding.selectJksKey.setOnClickListener {
pickJks.launch(FilePickHelper.pickFile(false))
}
val v1View = binding.v1SigningEnabled
val v2View = binding.v2SigningEnabled
val v3View = binding.v3SigningEnabled
val v4View = binding.v4SigningEnabled
jks2bks()
jks2pem()
signApkWithJks()
signApkWithPem()
createKey()
}

private fun createKey() {
binding.createKey.setOnClickListener {
val password = binding.newPassword.text.toString().trim()
if (password.isNotEmpty()) {
val alias = binding.newAlias.text.toString().trim()
if (alias.isNotEmpty()) {
val country = binding.country.text.toString().trim()
if (country.isNotEmpty()) {
val state = binding.state.text.toString().trim()
if (state.isNotEmpty()) {
val locality = binding.locality.text.toString().trim()
if (locality.isNotEmpty()) {
val street = binding.street.text.toString().trim()
if (street.isNotEmpty()) {
val organization = binding.organization.text.toString().trim()
if (organization.isNotEmpty()) {
val organizationalUnit =
binding.organizationalUnit.text.toString().trim()
if (organizationalUnit.isNotEmpty()) {
val commonName =
binding.commonName.text.toString().trim()
if (commonName.isNotEmpty()) {
CertCreator.createKeystoreAndKey(
File(
getExternalFilesDir(null),
"$alias.jks"
).path,
password.toCharArray(),
alias,
DistinguishedNameValues().apply {
setCountry(country)
setState(state)
setLocality(locality)
setStreet(street)
setOrganization(organization)
setOrganizationalUnit(organizationalUnit)
setCommonName(commonName)
})
} else {
Toast.makeText(
this,
"Enter Name!",
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(
this,
"Enter Department/Unit!",
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(
this,
"Enter Company/Organization!",
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(
this,
"Enter Street Address!",
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(this, "Enter City/Locality!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Enter State/Province!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Enter Country code!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Enter Alias!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Enter Password!", Toast.LENGTH_SHORT).show()
}
}
}

private fun signApkWithJks() {
binding.signApkWithJks.setOnClickListener { view ->
setEnabled(view, false)
val apk = apkFile
Expand All @@ -150,33 241,36 @@ class MainActivity : AppCompatActivity() {
password,
alias,
aliasPassword,
v1View.isChecked,
v2View.isChecked,
v3View.isChecked,
v4View.isChecked,
binding.v1SigningEnabled.isChecked,
binding.v2SigningEnabled.isChecked,
binding.v3SigningEnabled.isChecked,
binding.v4SigningEnabled.isChecked,
)
withContext(Dispatchers.Main) {
dialog.dismiss()
setEnabled(view, true)
}
}
} else {
Toast.makeText(this, "Enter alias password!", Toast.LENGTH_SHORT)
Toast.makeText(this, "Enter Alias Password!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Enter alias!", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Enter Alias!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Enter password!", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Enter Password!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Please select JKS keystore!", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Please select JKS Keystore!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Please select APK File!", Toast.LENGTH_SHORT).show()
}
}
}

private fun jks2bks() {
binding.jksToBks.setOnClickListener {
val jks = jksFile
if (jks != null && jks.exists()) {
Expand All @@ -189,39 283,16 @@ class MainActivity : AppCompatActivity() {
aliasPassword
)
} else {
Toast.makeText(this, "Enter alias password!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Please select JKS keystore!", Toast.LENGTH_SHORT).show()
}
}
binding.jksToPk8AndX509.setOnClickListener {
val jks = jksFile
if (jks != null && jks.exists()) {
val password = binding.password.text.toString().trim()
if (password.isNotEmpty()) {
val aliasPassword = binding.aliasPassword.text.toString().trim()
if (aliasPassword.isNotEmpty()) {
JksToPem.convert(
jks,
password,
aliasPassword,
File(getExternalFilesDir(null), jks.name ".pk8"),
File(getExternalFilesDir(null), jks.name ".x509.pem"),
)
} else {
Toast.makeText(this, "Enter alias password!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Enter password!", Toast.LENGTH_SHORT)
Toast.makeText(this, "Enter Alias Password!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Please select JKS keystore!", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Please select JKS Keystore!", Toast.LENGTH_SHORT).show()
}
}
}

private fun signApkWithPem() {
binding.signApkWithPem.setOnClickListener { view ->
setEnabled(view, false)
val apk = apkFile
Expand All @@ -239,29 310,58 @@ class MainActivity : AppCompatActivity() {
File(getExternalFilesDir(null), "app_signed.apk"),
pk8,
x509,
v1View.isChecked,
v2View.isChecked,
v3View.isChecked,
v4View.isChecked,
binding.v1SigningEnabled.isChecked,
binding.v2SigningEnabled.isChecked,
binding.v3SigningEnabled.isChecked,
binding.v4SigningEnabled.isChecked,
)
withContext(Dispatchers.Main) {
dialog.dismiss()
setEnabled(view, true)
}
}
} else {
Toast.makeText(this, "Please select x509.pem keystore!", Toast.LENGTH_SHORT)
Toast.makeText(this, "Please select x509.pem Keystore!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Please select pk8 keystore!", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Please select pk8 Keystore!", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Please select APK File!", Toast.LENGTH_SHORT).show()
}
}
}

private fun jks2pem() {
binding.jksToPk8AndX509.setOnClickListener {
val jks = jksFile
if (jks != null && jks.exists()) {
val password = binding.password.text.toString().trim()
if (password.isNotEmpty()) {
val aliasPassword = binding.aliasPassword.text.toString().trim()
if (aliasPassword.isNotEmpty()) {
JksToPem.convert(
jks,
password,
aliasPassword,
File(getExternalFilesDir(null), jks.name ".pk8"),
File(getExternalFilesDir(null), jks.name ".x509.pem"),
)
} else {
Toast.makeText(this, "Enter Alias Password!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Enter Password!", Toast.LENGTH_SHORT)
.show()
}
} else {
Toast.makeText(this, "Please select JKS Keystore!", Toast.LENGTH_SHORT).show()
}
}
}

private fun dialog(): AlertDialog {
return MaterialAlertDialogBuilder(this).apply {
setView(LinearLayout(context).apply {
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 216,81 @@

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/new_alias"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/alias" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/common_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/organizational_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Department/Unit" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/organization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Company/Organization" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/street"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Street Address" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/locality"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="City/Locality" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="State/Province" />

<com.google.android.material.textfield.TextInputEditText
android:id="@ id/country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Country code" />

<com.google.android.material.button.MaterialButton
android:id="@ id/create_key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create" />

</LinearLayout>

</com.google.android.material.card.MaterialCardView>

</LinearLayout>

</ScrollView>
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 14,7 @@ afterEvaluate {
from components.java
groupId = 'com.mcal'
artifactId = 'apksigner'
version = "1.7"
version = "1.8"
}
}
}
Expand Down
Loading

0 comments on commit cb243f1

Please sign in to comment.