Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. DefaultBootstrap.bootstrap();
  2. BasicParserPool ppMgr = new BasicParserPool();
  3. ppMgr.setNamespaceAware(true);
  4.  
  5. //Read file from the filesystem
  6.  
  7. File file1=new File("F:/Softwares/Assertion.xml");
  8. InputStream inCommonSaml=new FileInputStream(file1);
  9.  
  10. // Parse file
  11. Document inCommonSamlDoc = ppMgr.parse(inCommonSaml);
  12. Element metadataRoot = inCommonSamlDoc.getDocumentElement();
  13. UnmarshallerFactory unmarshallerFactory=configuration.getUnmarshallerFactory();
  14. Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);
  15. Response inCommonSamlRes = (Response) unmarshaller.unmarshall(metadataRoot);
  16.  
  17. //Get certificate
  18. SignatureValidator signatureValidator = new SignatureValidator(cert);
  19. Signature signature=inCommonSamlRes.getSignature();
  20. signatureValidator.validate(signature);
  21.  
  22.  
  23. try {
  24. BasicX509Credential credential = new BasicX509Credential();
  25.  
  26. File file2=new File("F:/Softwares/publicKey.crt");
  27. InputStream samlCertificate=new FileInputStream(file2);
  28. CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  29. //
  30. @SuppressWarnings("deprecation")
  31. java.security.cert.X509Certificate certificate = (java.security.cert.X509Certificate) certificateFactory.generateCertificate(samlCertificate);
  32. //
  33.  
  34. X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec((certificate).getPublicKey().getEncoded());
  35. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  36. PublicKey key = keyFactory.generatePublic(publicKeySpec);
  37.  
  38.  
  39. credential.setPublicKey(key);
  40. Object obj = (credential).getPublicKey();
  41. if (obj instanceof RSAPublicKey) {
  42. BigInteger modulus = ((RSAPublicKey) obj).getModulus();
  43. BigInteger exponent = ((RSAPublicKey) obj).getPublicExponent();
  44. System.out.println("modulus");
  45. System.out.println (org.apache.commons.codec.binary.Base64.encodeBase64String(modulus.toByteArray()));
  46. System.out.println("public exponent:");
  47. System.out.println (org.apache.commons.codec.binary.Base64.encodeBase64String(exponent.toByteArray()));
  48.  
  49. }
  50. // System.out.println ("public key is: //n//r"+ credential.getPublicKey());
  51. return credential;
  52.  
  53.  
  54.  
  55. } catch (Exception e) {
  56. throw e; //Throws a 'Signature did not validate against the credential's key' exception
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement