Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:JAVA如何从一个.p12或.pfx文件中获取公钥和私钥? [Re:njord]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-10-17 02:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Be careful about the password. It can *not* be empty

/**
* Read a p12 format digital certificate. Be careful about the file format.
* Sometimes, it might be incompatible. If it happens, import/export again
* using netscape(p12) or IE(pfx).
*/
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Key;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import java.io.*;
import java.util.*;

public class ReadP12Cert
{
public static void main(String[] args)
{
final String KEYSTORE_FILE = "cert/dev_coo1.p12";
     final String KEYSTORE_PASSWORD = "123";
     final String KEYSTORE_ALIAS = "alias";

try
{
       KeyStore ks = KeyStore.getInstance("PKCS12");
       FileInputStream fis = new FileInputStream(KEYSTORE_FILE);

// If the keystore password is empty(""), then we have to set
// to null, otherwise it won't work!!!
char[] nPassword = null;
if ((KEYSTORE_PASSWORD == null) || KEYSTORE_PASSWORD.trim().equals(""))
{
nPassword = null;
}
else
{
nPassword = KEYSTORE_PASSWORD.toCharArray();
}
ks.load(fis, nPassword);
fis.close();

System.out.println("keystore type=" + ks.getType());

// Now we loop all the aliases, we need the alias to get keys.
// It seems that this value is the "Friendly name" field in the
// detals tab <-- Certificate window <-- view <-- Certificate
// Button <-- Content tab <-- Internet Options <-- Tools menu
// In MS IE 6.
       Enumeration enum = ks.aliases();
String keyAlias = null;
       if (enum.hasMoreElements()) // we are readin just one certificate.
       {
keyAlias = (String)enum.nextElement();
         System.out.println("alias=[" + keyAlias + "]");
       }

// Now once we know the alias, we could get the keys.
System.out.println("is key entry=" + ks.isKeyEntry(keyAlias));
PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);
Certificate cert = ks.getCertificate(keyAlias);
PublicKey pubkey = cert.getPublicKey();

System.out.println("cert class = " + cert.getClass().getName());
System.out.println("cert = " + cert);
System.out.println("public key = " + pubkey);
System.out.println("private key = " + prikey);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code

Hibernate开发指南

话题树型展开
人气 标题 作者 字数 发贴时间
18267 JAVA如何从一个.p12或.pfx文件中获取公钥和私钥? njord 10 2003-09-10 15:16
16162 Re:JAVA如何从一个.p12或.pfx文件中获取公钥和私钥? njord 74 2003-09-11 11:16
16356 Re:JAVA如何从一个.p12或.pfx文件中获取公钥和私钥? floater 282 2003-09-11 22:16
16426 Re:JAVA如何从一个.p12或.pfx文件中获取公钥和私钥? njord 800 2003-09-12 08:48
21229 Re:JAVA如何从一个.p12或.pfx文件中获取公钥和私钥? floater 3080 2003-10-17 02:23

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923