隨筆-69  評論-0  文章-5  trackbacks-0

          一,將pem格式的key文件導(dǎo)入keystore

          摘抄備用:http://www.agentbob.info/agentbob/79-AB.html

          說明: 經(jīng)試驗(yàn)證書的pem文件可能通過keytool直接導(dǎo)入keystore中的

          Apache Tomcat and many other Java applications expect to retrieve SSL/TLS certificates from a Java Key Store (JKS). Jave Virtual Machines usually come with keytool  to help you create a new key store.

          Keytool helps you to:

          • create a new JKS with a new private key
          • generate a Certificate Signung Request (CSR) for the private key in this JKS
          • import a certificate that you received for this CSR into your JKS

          Keytool does not let you import an existing private key for which you already have a certificate. So you need to do this yourself, here's how:

          Let's assume you have a private key (key.pem) and a certificate (cert.pem), both in PEM format as the file names suggest.

          PEM format is 'kind-of-human-readable' and looks like e.g.

          -----BEGIN CERTIFICATE-----
          Ulv6GtdFbjzLeqlkelqwewlq822OrEPdH+zxKUkKGX/eN
          .
          . (snip)
          .
          9801asds3BCfu52dm7JHzPAOqWKaEwIgymlk=
          ----END CERTIFICATE-----

          Convert both, the key and the certificate into DER format using openssl :

          openssl pkcs8 -topk8 -nocrypt -in key.pem -inform PEM -out key.der -outform DER
          openssl x509 -in cert.pem -inform PEM -out cert.der -outform DER

          Now comes the tricky bit, you need something to import these files into the JKS. ImportKey will do this for you, get the ImportKey.java (text/x-java-source, 6.6 kB, info) source or the compiled (Java 1.5 !) ImportKey.class (application/octet-stream, 3.3 kB, info) and run it like

          user@host:~$ java ImportKey key.der cert.der
          Using keystore-file : /home/user/keystore.ImportKey
          One certificate, no chain.
          Key and certificate stored.
          Alias:importkey Password:importkey

          Now we have a proper JKS containing our private key and certificate in a file called keystore.ImportKey, using 'importkey' as alias and also as password. For any further changes, like changing the password we can use keytool.



          二、將私鑰導(dǎo)出成pem文件(默認(rèn)keytool是不能導(dǎo)出私鑰的)
          import sun.misc.BASE64Encoder;
          import java.security.cert.Certificate;
          import java.security.*;
          import java.io.File;
          import java.io.FileInputStream;
           
          class ExportPriv {
              
          public static void main(String args[]) throws Exception{
              ExportPriv myep 
          = new ExportPriv();
              myep.doit();
              }
           
              
          public void doit() throws Exception{
           
              KeyStore ks 
          = KeyStore.getInstance("JKS");
              String fileName 
          = "store.jks";
           
              
          char[] passPhrase = "password".toCharArray();
              BASE64Encoder myB64 
          = new BASE64Encoder();
              
           
              File certificateFile 
          = new File(fileName);
              ks.load(
          new FileInputStream(certificateFile), passPhrase);
           
              KeyPair kp 
          = getPrivateKey(ks, "alias", passPhrase);
                  
              PrivateKey privKey 
          = kp.getPrivate();
              
           
              String b64 
          = myB64.encode(privKey.getEncoded());
           
              System.out.println(
          "-----BEGIN PRIVATE KEY-----");
              System.out.println(b64);
              System.out.println(
          "-----END PRIVATE KEY-----");
           
              }
           
          // From http://javaalmanac.com/egs/java.security/GetKeyFromKs.html
           
             
          public KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
                  
          try {
                      
          // Get private key
                      Key key = keystore.getKey(alias, password);
                      
          if (key instanceof PrivateKey) {
                          
          // Get certificate of public key
                          Certificate cert = keystore.getCertificate(alias);
              
                          
          // Get public key
                          PublicKey publicKey = cert.getPublicKey();
              
                          
          // Return a key pair
                          return new KeyPair(publicKey, (PrivateKey)key);
                      }
                  } 
          catch (UnrecoverableKeyException e) {
                  } 
          catch (NoSuchAlgorithmException e) {
                  } 
          catch (KeyStoreException e) {
                  }
                  
          return null;
              }
           
          }
           

          posted on 2008-04-17 16:27 liunix 閱讀(8006) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 和田市| 犍为县| 大名县| 沽源县| 泸水县| 永州市| 康乐县| 天峻县| 福建省| 即墨市| 应城市| 惠州市| 曲麻莱县| 布拖县| 昌宁县| 钟山县| 诸城市| 稷山县| 丹棱县| 苍溪县| 定南县| 庆阳市| 普宁市| 梅河口市| 阜平县| 德庆县| 临城县| 晴隆县| 行唐县| 常宁市| 巩留县| 濉溪县| 东方市| 隆尧县| 乌拉特后旗| 临泽县| 镇远县| 从江县| 德庆县| 石门县| 临沭县|