String To DSA Public Key - Android java.security

Android examples for java.security:Key

Description

String To DSA Public Key

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 {
    public static PublicKey StringToPublicKey(String keyData) {

        KeyFactory fact;/* w  w  w  .  j a  v  a2s .  c om*/
        try {
            byte[] data = Base64.decode(keyData, Base64.DEFAULT);
            X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
            fact = KeyFactory.getInstance("DSA");
            return fact.generatePublic(spec);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials