import RSA Private Key - Android java.security

Android examples for java.security:RSA

Description

import RSA Private 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.PKCS8EncodedKeySpec;

public class Main {
    public static PrivateKey importPriKey(String privateKey) {
        PrivateKey priKey = null;
        if (privateKey != null) {
            try {
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                Log.d("keyFactory", "" + (keyFactory == null));
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
                        Base64.decode(privateKey.getBytes(), Base64.NO_WRAP));
                Log.d("keyFactory", "" + (keySpec == null));
                priKey = keyFactory.generatePrivate(keySpec);
                Log.d("priKey", "" + (priKey == null));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();/*from  w  ww  .j  a  v  a2 s .  c  o  m*/
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            }

        }
        return priKey;
    }
}

Related Tutorials