Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.math.BigInteger;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.RSAPublicKeySpec;

public class Main {
    public static final String KEY_ALGORITHM = "RSA";

    public static PublicKey getPublicKey(String n, String publicExponent) {
        KeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(n, 16), new BigInteger(publicExponent, 16));

        KeyFactory factory = null;
        PublicKey publicKey = null;
        try {
            factory = KeyFactory.getInstance(KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        try {
            publicKey = factory.generatePublic(publicKeySpec);
        } catch (InvalidKeySpecException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        return publicKey;
    }
}