RSAPrivateCrtKeySpec privKeySpec = null;
ASN1InputStream asn1InputStream = new ASN1InputStream(privateKey);
//parsiranje ASN1 strukture
ArrayList<BigInteger> privateKeyParts = new ArrayList<BigInteger>();
try {
DERObject derObject = asn1InputStream.readObject();
ASN1Sequence asn1Seq = (ASN1Sequence) derObject;
Enumeration<DERObject> en = asn1Seq.getObjects();
while(en.hasMoreElements()){
DERObject obj = en.nextElement();
DERBitString bitString = DERBitString.getInstance(obj);
byte[] prependZero = new byte[bitString.getBytes().length+1];
// zbog "cudnog" BigInteger konstruktora mora se dodati 0x0 na pocetak,
// u suprotnom u pojedinim slucajevima pogresno se protumaci znak i brojevi ispadnu negativni
prependZero[0] = 0x0;
System.arraycopy(bitString.getBytes(), 0, prependZero, 1, bitString.getBytes().length);
privateKeyParts.add(new BigInteger(prependZero));
}
// citamo sertifikat zbog podataka o javnom kljucu
byte[] standardFileBytes = readElementaryFile(STANDARD_CERTIFICATE);
byte[] certificateBytes = new byte[standardFileBytes.length];
System.arraycopy(standardFileBytes, 4, certificateBytes, 0, standardFileBytes.length-4);
ByteArrayInputStream certificateInputStream = new ByteArrayInputStream(certificateBytes);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate x509Cert = (X509Certificate) cf.generateCertificate(certificateInputStream);
// javni kljuc se sastoji od modula i javnog eksponenta u odgovarajucoj ASN1 strukturi
ASN1InputStream is = new ASN1InputStream(x509Cert.getPublicKey().getEncoded());
DERObject obj = is.readObject();
ASN1Sequence seq = (ASN1Sequence)obj;
SubjectPublicKeyInfo pkInfo = SubjectPublicKeyInfo.getInstance(seq);
RSAPublicKeyStructure pubk = RSAPublicKeyStructure.getInstance(pkInfo.getPublicKey());
BigInteger expo = pubk.getPublicExponent();
BigInteger mod = pubk.getModulus();