To extend Uwe's answer, the reason you see different values is your strange handling of the public key data:
RSAPublicKey pubkey = (RSAPublicKey) cert.getPublicKey();
tempPub = pubkey.getEncoded();
sPub = new String( tempPub );
field=hex(sPub);
System.out.println("Public key : \n" + field );
Here you are creating a string from a stream of bytes (never a good idea) and then hex-encoding the result. No wonder it looks weird! In fact, your hex
method looks very strange and probably should be thrown away.
Instead, just try this:
RSAPublicKey pubkey = (RSAPublicKey) cert.getPublicKey();
field = DatatypeConverter.printHexBinary(pubkey.getEncoded());
System.out.println("Public key : \n" + field );
If you are using Java 5 or below, Google for a decent byte-to-hex function as DatatypeConverter
is not available.