import RSA Public Key - Android java.security

Android examples for java.security:RSA

Description

import RSA Public Key

Demo Code


//package com.java2s;

import android.util.Base64;
import android.util.Log;

import java.security.*;
import java.security.spec.InvalidKeySpecException;

import java.security.spec.X509EncodedKeySpec;

public class Main {
    public static PublicKey importPubKey(String publicKey) {
        PublicKey pubKey = null;//  www  .  jav  a  2 s.c o  m
        if (publicKey != null) {
            try {
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                X509EncodedKeySpec keySpec = new X509EncodedKeySpec(
                        Base64.decode(publicKey.getBytes(), Base64.NO_WRAP));
                Log.d("keySpec", "" + (keySpec == null));
                pubKey = keyFactory.generatePublic(keySpec);
                Log.d("pubKey", "" + (pubKey == null));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            }

        }
        return pubKey;
    }
}

Related Tutorials