如何使用.p12证书使用FTPClient连接到FTP?

时间:2017-09-18 16:30:09

标签: java ssl ftp p12

我有 .p12 文件的证书可以连接到FTP服务器。我以前从未使用过这种证书进行连接。

所以我普通的FTP连接是:

FTPClient ftpClient = new FTPClient();

try {
            ftpClient.connect(ftpServer, ftpPort);
            ftpClient.login(ftpUser, ftpPassword);

            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            FTPFile[] ftpFiles = ftpClient.listFiles();
...

我也可以从.p12文件中检索一些信息:

FileInputStream is = new FileInputStream("d:\\temp\\cert\\$126805.p12");

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(is, passwordCertificate.toCharArray());

String alias = "my_alias";

Key key = keystore.getKey(alias, passwordCertificate.toCharArray());
if (key instanceof PrivateKey) {
    // Get certificate of public key
    Certificate cert = keystore.getCertificate(alias);

    // Get public key
    PublicKey publicKey = cert.getPublicKey();

    System.out.println("Public key: " + publicKey);
    System.out.println("Key: " + key);

    // Return a key pair
    new KeyPair(publicKey, (PrivateKey) key);
}

如何使用.p12文件中的信息连接到FTP?

增添。 我已完成以下步骤:

  1. 将PKCS12文件导入密钥库:
  2.   

    keytool -importkeystore -srckeystore $ 126805.p12 -destkeystore   keystore.jks -srcstoretype pkcs12

    1. 应用代码:

          String keystoreFile = "D:\\temp\\cert\\keystore.jks";
          String passwordKeyStore = "123456";
          File storeFile = new File(keystoreFile);
      
          KeyStore keyStore = loadStore("JKS", storeFile, passwordKeyStore);
      
          X509TrustManager defaultTrustManager = TrustManagerUtils.getDefaultTrustManager(keyStore);
      
          String protocol = "SSL";
          FTPSClient client = new FTPSClient(protocol, true);
          //also I tried FTPSClient client = new FTPSClient(protocol, false);
      
          client.setTrustManager(defaultTrustManager);
          client.addProtocolCommandListener(new PrintCommandListener(new  PrintWriter(System.out)));
      
          String host = "ftp_host_name";
      
          System.out.println("**** Connect to host ****");
          client.connect(host);//Here I get an Exception
          ...
      
    2. 哪里

      private KeyStore loadStore(String storeType, File storePath, String storePass)
                  throws KeyStoreException,  IOException, GeneralSecurityException {
      
              KeyStore ks = KeyStore.getInstance(storeType);
              FileInputStream stream = null;
      
              try {
                  stream = new FileInputStream(storePath);
                  ks.load(stream, storePass.toCharArray());
              } finally {
                  Util.closeQuietly(stream);
              }
      
              return ks;
      }
      

      在连接期间,我收到以下错误:

        

      无法连接到服务器。 java.net.ConnectException:连接   超时:连接

      当我不使用任何trustManager时,我得到的行为相同。似乎由于某些原因证书未正确应用。

      有人能给我任何建议吗?

1 个答案:

答案 0 :(得分:0)

因此。代码是正确的。我没有使用FTP URL,而是使用HTTPS。

唯一的纠正是可以直接从.p12文件创建KeyStore:

none()