Create KeyStore and sign apk in processing (android mode) (java can be used if some changes are made)

Purpose

Sign with processing Maybe it can be used to create an IDE (though people who make IDEs can make it without looking at it ...) library Please install ZipSigner and Spongy Castle import

import java.security.KeyStore;
import java.io.*; 
import android.os.Environment;
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
import kellinwood.security.zipsigner.ZipSigner;
import java.io.InputStream;
import kellinwood.security.zipsigner.optional.CustomKeySigner;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import java.net.*;

Authority

If processing

if (!hasPermission("android.permission.WRITE_EXTERNAL_STORAGE"))requestPermission("android.permission.WRITE_EXTERNAL_STORAGE", "initLocation");

Can be done with If it is java, please add WRITE_EXTERNAL_STORAGE without permission

Creating a Keystore

KeyStore ks;
void createKey(String keyName,String keyPassword,String Alias,String AliasPassword){
  try{
    String directory=Environment.getExternalStorageDirectory()+"/"+keyName+"."+KeyStore.getDefaultType();
    ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, keyPassword.toCharArray());
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    SecretKey mySecretKey=keyGen.generateKey();
    KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(mySecretKey); 
    ks.setEntry(Alias, skEntry, new KeyStore.PasswordProtection(AliasPassword.toCharArray()));
    File file=new File(directory);
    FileOutputStream fos = new FileOutputStream(file);
    ks.store(fos, keyPassword.toCharArray());
  }catch(Exception e){
    e.printStackTrace();
  }
}

Go out

signature

void sign(String keystore,String keystorePassword,String keyAlias,String StringkeyAliasPassword,String inFilename,String outputFilename){
  try {
    ZipSigner signer = new ZipSigner();
    CustomKeySigner.signZip(signer, keystore, keystorePassword.toCharArray(), keyAlias, keyAliasPassword.toCharArray(), "SHA1WITHRSA", inFilename, outputFilename);
  }catch (Exception e) {
     e.printStackTrace();
  }
}

Go out

Afterword

It took a lot of time because there were few explanations in Japanese, but the work itself was simple.

Recommended Posts

Create KeyStore and sign apk in processing (android mode) (java can be used if some changes are made)
[Android / Java] Screen transition and return processing in fragments
Syntax and exception occurrence conditions that can be used when comparing with null in Java
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
Summary of ORM "uroboroSQL" that can be used in enterprise Java