forked from REAndroid/APKEditor
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to fix sign failing on some/older Android versions
- Loading branch information
1 parent
1a74d1c
commit a39129f
Showing
17 changed files
with
278 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
app/src/main/java/com/abdurazaaqmohammed/AntiSplit/main/SignUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.abdurazaaqmohammed.AntiSplit.main; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
|
||
import com.abdurazaaqmohammed.AntiSplit.R; | ||
import com.aefyr.pseudoapksigner.IOUtils; | ||
import com.aefyr.pseudoapksigner.PseudoApkSigner; | ||
import com.android.apksig.ApkSigner; | ||
import com.android.apksig.apk.ApkFormatException; | ||
import com.reandroid.apkeditor.merge.LogUtil; | ||
import com.starry.FileUtils; | ||
|
||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.InvalidKeyException; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SignatureException; | ||
import java.security.UnrecoverableEntryException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
import java.util.Collections; | ||
|
||
public class SignUtil { | ||
public static void signApk(InputStream key, String password, File inputApk, File output) throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException, ApkFormatException, SignatureException, InvalidKeyException, UnrecoverableEntryException { | ||
signApk(key, password, inputApk, output, true, true, true); | ||
} | ||
|
||
public static void signApk(InputStream key, String password, File inputApk, File output, boolean v1, boolean v2, boolean v3) throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException, ApkFormatException, SignatureException, InvalidKeyException, UnrecoverableEntryException { | ||
char[] pw = password.toCharArray(); | ||
|
||
KeyStore keystore = KeyStore.getInstance("BKS"); | ||
keystore.load(key, pw); | ||
|
||
String alias = keystore.aliases().nextElement(); | ||
|
||
new ApkSigner.Builder(Collections.singletonList(new ApkSigner.SignerConfig.Builder("CERT", | ||
((KeyStore.PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(pw))).getPrivateKey(), | ||
Collections.singletonList((X509Certificate) keystore.getCertificate(alias))).build())) | ||
.setInputApk(inputApk) | ||
.setOutputApk(output) | ||
.setCreatedBy("Android Gradle 8.0.2") | ||
.setV1SigningEnabled(v1) | ||
.setV2SigningEnabled(v2) | ||
.setV3SigningEnabled(v3).build().sign(); | ||
} | ||
|
||
public static void signDebugKey(Context c, File inputApk, File output, boolean v1, boolean v2, boolean v3) throws IOException, ApkFormatException, UnrecoverableEntryException, CertificateException, KeyStoreException, NoSuchAlgorithmException, SignatureException, InvalidKeyException { | ||
signApk(c.getAssets().open("debug23.keystore"), "android", inputApk, output, v1, v2, v3); | ||
} | ||
|
||
public static void signDebugKey(Context c, File inputApk, File output) throws IOException, ApkFormatException, UnrecoverableEntryException, CertificateException, KeyStoreException, NoSuchAlgorithmException, SignatureException, InvalidKeyException { | ||
signApk(c.getAssets().open("debug23.keystore"), "android", inputApk, output); | ||
} | ||
|
||
public static void signPseudoApkSigner(File temp, Context context, Uri out, Exception e) throws IOException { | ||
String msg = com.abdurazaaqmohammed.AntiSplit.main.MainActivity.rss.getString(R.string.sign_failed); | ||
if(Build.VERSION.SDK_INT < 30) { | ||
// When I tried signing with apksig in AVD with sdk 10 java.security is throwing some error saying something not found | ||
// Apparently 11 is the last version that supports v1 signing alone. | ||
try (InputStream fis = FileUtils.getInputStream(temp)) { | ||
final String FILE_NAME_PAST = "testkey.past"; | ||
final String FILE_NAME_PRIVATE_KEY = "testkey.pk8"; | ||
File signingEnvironment = new File(context.getFilesDir(), "signing"); | ||
File pastFile = new File(signingEnvironment, FILE_NAME_PAST); | ||
File privateKeyFile = new File(signingEnvironment, FILE_NAME_PRIVATE_KEY); | ||
|
||
if (!pastFile.exists() || !privateKeyFile.exists()) { | ||
signingEnvironment.mkdir(); | ||
IOUtils.copyFileFromAssets(context, FILE_NAME_PAST, pastFile); | ||
IOUtils.copyFileFromAssets(context, FILE_NAME_PRIVATE_KEY, privateKeyFile); | ||
} | ||
|
||
PseudoApkSigner.sign(fis, FileUtils.getOutputStream(out, context), pastFile, privateKeyFile); | ||
} catch (Exception e2) { | ||
LogUtil.logMessage(msg); | ||
FileUtils.copyFile(temp, FileUtils.getOutputStream(out, context)); | ||
throw(new RuntimeException(msg, e)); // for showError | ||
} | ||
} else { | ||
LogUtil.logMessage(msg); | ||
FileUtils.copyFile(temp, FileUtils.getOutputStream(out, context)); | ||
throw(new RuntimeException(msg, e)); // for showError | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.