static byte[] buildRSA(RSAPublicKey key) { DataByteOutputStream out = new DataByteOutputStream(); BigInteger exponent = key.getPublicExponent(); BigInteger modulus = key.getModulus(); int exponentLength = BigIntegerLength(exponent); if (exponentLength < 256) out.writeByte(exponentLength); else { out.writeByte(0); out.writeShort(exponentLength); } out.writeBigInteger(exponent); out.writeBigInteger(modulus); return out.toByteArray(); }