Improve SSL
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / PkiUtils.java
index 050b7d4d7b3cdcf123cee983452404e4f8b0c876..031515caadbcd0705ec08fd9cc13fe2429224ef4 100644 (file)
@@ -41,7 +41,7 @@ class PkiUtils {
                        kpGen.initialize(keySize, new SecureRandom());
                        KeyPair pair = kpGen.generateKeyPair();
                        Date notBefore = new Date(System.currentTimeMillis() - 10000);
-                       Date notAfter = new Date(System.currentTimeMillis() + 24L * 3600 * 1000);
+                       Date notAfter = new Date(System.currentTimeMillis() + 365 * 24L * 3600 * 1000);
                        BigInteger serial = BigInteger.valueOf(System.currentTimeMillis());
                        X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(x500Principal, serial, notBefore,
                                        notAfter, x500Principal, pair.getPublic());
@@ -92,67 +92,71 @@ class PkiUtils {
                long begin = System.currentTimeMillis();
                for (int i = 512; i < 1024; i = i + 2) {
                        try {
-                               KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM,provider);
+                               KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM, provider);
                                keyGen.initialize(i, secureRandom);
                                keyGen.generateKeyPair();
                        } catch (Exception e) {
                                System.err.println(i + " : " + e.getMessage());
                        }
                }
-               System.out.println( (System.currentTimeMillis() - begin) + " ms");
+               System.out.println((System.currentTimeMillis() - begin) + " ms");
 
-//             // String text = "a";
-//             String text = "testtesttesttesttesttesttesttesttesttesttesttesttesttesttest";
-//             try {
-//                     System.out.println(text);
-//                     PrivateKey privateKey;
-//                     PublicKey publicKey;
-//                     char[] password = "changeit".toCharArray();
-//                     String alias = "CN=test";
-//                     KeyStore keyStore = KeyStore.getInstance("pkcs12");
-//                     File p12file = new File("test.p12");
-//                     p12file.delete();
-//                     if (!p12file.exists()) {
-//                             keyStore.load(null);
-//                             generateSelfSignedCertificate(keyStore, new X500Principal(alias), 513, password);
-//                             try (OutputStream out = new FileOutputStream(p12file)) {
-//                                     keyStore.store(out, password);
-//                             }
-//                     }
-//                     try (InputStream in = new FileInputStream(p12file)) {
-//                             keyStore.load(in, password);
-//                             privateKey = (PrivateKey) keyStore.getKey(alias, password);
-//                             publicKey = keyStore.getCertificateChain(alias)[0].getPublicKey();
-//                     }
-//                     // KeyPair key;
-//                     // final KeyPairGenerator keyGen =
-//                     // KeyPairGenerator.getInstance(ALGORITHM);
-//                     // keyGen.initialize(4096, new SecureRandom());
-//                     // long begin = System.currentTimeMillis();
-//                     // key = keyGen.generateKeyPair();
-//                     // System.out.println((System.currentTimeMillis() - begin) + " ms");
-//                     // keyStore.load(null);
-//                     // keyStore.setKeyEntry("test", key.getPrivate(), password, null);
-//                     // try(OutputStream out=new FileOutputStream(p12file)) {
-//                     // keyStore.store(out, password);
-//                     // }
-//                     // privateKey = key.getPrivate();
-//                     // publicKey = key.getPublic();
-//
-//                     Cipher encrypt = Cipher.getInstance(ALGORITHM);
-//                     encrypt.init(Cipher.ENCRYPT_MODE, publicKey);
-//                     byte[] encrypted = encrypt.doFinal(text.getBytes());
-//                     String encryptedBase64 = Base64.getEncoder().encodeToString(encrypted);
-//                     System.out.println(encryptedBase64);
-//                     byte[] encryptedFromBase64 = Base64.getDecoder().decode(encryptedBase64);
-//
-//                     Cipher decrypt = Cipher.getInstance(ALGORITHM);
-//                     decrypt.init(Cipher.DECRYPT_MODE, privateKey);
-//                     byte[] decrypted = decrypt.doFinal(encryptedFromBase64);
-//                     System.out.println(new String(decrypted));
-//             } catch (Exception e) {
-//                     e.printStackTrace();
-//             }
+               // // String text = "a";
+               // String text =
+               // "testtesttesttesttesttesttesttesttesttesttesttesttesttesttest";
+               // try {
+               // System.out.println(text);
+               // PrivateKey privateKey;
+               // PublicKey publicKey;
+               // char[] password = "changeit".toCharArray();
+               // String alias = "CN=test";
+               // KeyStore keyStore = KeyStore.getInstance("pkcs12");
+               // File p12file = new File("test.p12");
+               // p12file.delete();
+               // if (!p12file.exists()) {
+               // keyStore.load(null);
+               // generateSelfSignedCertificate(keyStore, new X500Principal(alias),
+               // 513, password);
+               // try (OutputStream out = new FileOutputStream(p12file)) {
+               // keyStore.store(out, password);
+               // }
+               // }
+               // try (InputStream in = new FileInputStream(p12file)) {
+               // keyStore.load(in, password);
+               // privateKey = (PrivateKey) keyStore.getKey(alias, password);
+               // publicKey = keyStore.getCertificateChain(alias)[0].getPublicKey();
+               // }
+               // // KeyPair key;
+               // // final KeyPairGenerator keyGen =
+               // // KeyPairGenerator.getInstance(ALGORITHM);
+               // // keyGen.initialize(4096, new SecureRandom());
+               // // long begin = System.currentTimeMillis();
+               // // key = keyGen.generateKeyPair();
+               // // System.out.println((System.currentTimeMillis() - begin) + " ms");
+               // // keyStore.load(null);
+               // // keyStore.setKeyEntry("test", key.getPrivate(), password, null);
+               // // try(OutputStream out=new FileOutputStream(p12file)) {
+               // // keyStore.store(out, password);
+               // // }
+               // // privateKey = key.getPrivate();
+               // // publicKey = key.getPublic();
+               //
+               // Cipher encrypt = Cipher.getInstance(ALGORITHM);
+               // encrypt.init(Cipher.ENCRYPT_MODE, publicKey);
+               // byte[] encrypted = encrypt.doFinal(text.getBytes());
+               // String encryptedBase64 =
+               // Base64.getEncoder().encodeToString(encrypted);
+               // System.out.println(encryptedBase64);
+               // byte[] encryptedFromBase64 =
+               // Base64.getDecoder().decode(encryptedBase64);
+               //
+               // Cipher decrypt = Cipher.getInstance(ALGORITHM);
+               // decrypt.init(Cipher.DECRYPT_MODE, privateKey);
+               // byte[] decrypted = decrypt.doFinal(encryptedFromBase64);
+               // System.out.println(new String(decrypted));
+               // } catch (Exception e) {
+               // e.printStackTrace();
+               // }
 
        }