Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. public static X509Certificate GetCertificate_v3(KeyPair keygen, Date startDate, Date expiryDate,
  2. String serial, String Certification_Aut_Id) throws InvalidKeyException, SecurityException, SignatureException{
  3.  
  4. X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
  5. v3CertGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
  6. v3CertGen.setIssuerDN(new X509Principal("CN=" + Certification_Aut_Id + ", O=o, L=L, ST=il, C= c"));
  7. v3CertGen.setNotBefore(startDate);
  8. v3CertGen.setNotAfter(expiryDate);
  9. v3CertGen.setSubjectDN(new X509Principal("CN=" + Certification_Aut_Id + ", O=o, L=L, ST=il, C= c"));
  10. v3CertGen.setPublicKey(keygen.getPublic());
  11. v3CertGen.setSignatureAlgorithm("SHA256withECDSA");
  12. X509Certificate cert = v3CertGen.generateX509Certificate(keygen.getPrivate());
  13.  
  14. return cert;
  15.  
  16. }
  17.  
  18. public static void storeKeypair(String KSpwd, String PKpwd, String KSname, X509Certificate certificate,
  19. KeyPair keygen, String alias, String temp_local) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException{
  20.  
  21. //Before a keystore can be accessed, it must be loaded.
  22. KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  23. // get user password and file input stream
  24. char[] KSpassword = KSpwd.toCharArray();
  25. FileInputStream fis = new java.io.FileInputStream(KSname);
  26. ks.load(fis, KSpassword);
  27. fis.close();
  28.  
  29. //writing the X509Certificate in a .cer file
  30. FileOutputStream fos1 = new FileOutputStream(temp_local + alias + ".cer");
  31. fos1.write( certificate.getEncoded() );
  32. fos1.flush();
  33. fos1.close();
  34.  
  35. // Load the certificate chain (in X.509 DER encoding).
  36. FileInputStream certificateStream = new FileInputStream(temp_local + alias + ".cer");
  37. CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
  38. Certificate[] chain = {};
  39. chain = certificateFactory.generateCertificates(certificateStream).toArray(chain);
  40.  
  41. // save my private key & certificate chain
  42. char[] PKpassword = PKpwd.toCharArray();
  43. ks.setEntry(alias, new KeyStore.PrivateKeyEntry(keygen.getPrivate(), chain),
  44. new KeyStore.PasswordProtection(PKpassword)
  45. );
  46.  
  47. //Store the KeyStore
  48. // Write out the keystore
  49. FileOutputStream fos = new FileOutputStream(KSname);
  50. ks.store(fos, KSpassword);
  51. fos.close();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement