Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.annotation.SuppressLint;
import android.content.Context;
import android.security.KeyPairGeneratorSpec;

import javax.security.auth.x500.X500Principal;

import java.math.BigInteger;
import java.security.*;

import java.util.*;

public class Main {
    @SuppressLint("NewApi")
    public static KeyPair generateRsaPairWithGenerator(Context ctx, String alais)
            throws InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException {
        Calendar notBefore = Calendar.getInstance();
        Calendar notAfter = Calendar.getInstance();
        notAfter.add(1, Calendar.YEAR);
        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx).setAlias(alais)
                .setSubject(new X500Principal(String.format("CN=%s, OU=%s", alais, ctx.getPackageName())))
                .setSerialNumber(BigInteger.ONE).setStartDate(notBefore.getTime()).setEndDate(notAfter.getTime())
                .build();

        KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
        kpGenerator.initialize(spec);
        KeyPair kp = kpGenerator.generateKeyPair();

        return kp;
    }
}