RSA Public Key To String - Android java.security

Android examples for java.security:RSA

Description

RSA Public Key To String

Demo Code


//package com.java2s;

import java.security.KeyFactory;

import java.security.NoSuchAlgorithmException;

import java.security.PublicKey;

import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

import android.util.Base64;

public class Main {
    private static final String ASYMMETRIC_ALGORITHM = "RSA";

    public static String PublicKeyToString(PublicKey key) {
        try {// w w  w  . j av a 2s .  co m
            KeyFactory fact = KeyFactory.getInstance(ASYMMETRIC_ALGORITHM);
            X509EncodedKeySpec spec;
            spec = fact.getKeySpec(key, X509EncodedKeySpec.class);
            return Base64.encodeToString(spec.getEncoded(), Base64.DEFAULT);
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials