Java tutorial
//package com.java2s; import java.security.PublicKey; import java.security.interfaces.DSAPublicKey; import java.security.interfaces.RSAPublicKey; public class Main { private static byte[] getPKBytes(PublicKey pk) { if (pk instanceof RSAPublicKey) { RSAPublicKey k = (RSAPublicKey) pk; return k.getModulus().toByteArray(); } else if (pk instanceof DSAPublicKey) { DSAPublicKey k = (DSAPublicKey) pk; return k.getY().toByteArray(); } return null; } }