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

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Java中能自己生成证书吗?
lvjing79





发贴: 18
积分: 0
于 2004-09-12 15:01 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
比如一张自签名的CA证书。可以吗?


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
linux_china



版主


发贴: 752
积分: 240
于 2004-09-12 15:04 user profilesend a private message to usersend email to linux_chinasearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以的,没有问题。以前用cryptix就生成过的。


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
lvjing79





发贴: 18
积分: 0
于 2004-09-12 17:10 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
我在《Java 安全》中好像没有看到啊?不知道是在哪个库里面?版主能告诉我吗?多谢了!


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
emarket

亢龙有诲

CJSDN高级会员


发贴: 460
积分: 82
于 2004-09-12 22:00 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
用OpenSSL是可以的
http://www-900.ibm.com/developerWorks/cn/java/j-certgen/

http://www-900.ibm.com/developerWorks/cn/java/j-certgen/index_eng.shtml

windows下如何用可以参见
http://www.dylanbeattie.net/docs/openssl_iis_ssl_howto.html



个人Blog http://agilespeaking.blogspot.com/

作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-09-13 01:25 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
use keytool in java bin dir


"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
作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
lvjing79





发贴: 18
积分: 0
于 2004-09-13 17:40 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
但keytool只能操作已有的证书啊,并不能用一个根证书给另一个证书请求进行签名啊?


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
menzy



版主


发贴: 754
积分: 113
于 2004-09-16 08:25 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
你是不时向做一个CA系统阿?


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
lvjing79





发贴: 18
积分: 0
于 2004-09-16 14:08 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
呵呵,是。现在是用keytool生成csr,然后用openssl颁发。


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
NUAA_SG





发贴: 8
积分: 0
于 2004-09-20 15:55 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
绝对可以,做过!


作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
menzy



版主


发贴: 754
积分: 113
于 2004-09-21 10:47 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
需要调用接口库吧!
编程调用System做当然可以,但是不管用什么,都应当公布java API,如果有API就好办



作者 Re:Java中能自己生成证书吗? [Re:lvjing79]
v_gyc





发贴: 2
积分: 10
于 2004-11-07 12:29 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
用 BC, 自己来 ,下面 是 V1 X509的 , V3的 还要麻烦点。

/*
* Created on 2004-10-22
* by v_gyc
*/

package test.certificate.generate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Date;

import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jce.X509V1CertificateGenerator;

/**
* @author Administrator
*/
public class V1Generator {

private static final String sigalg_DSAWITHSHA1 = "DSAWithSHA1";

public static void main(String[] args) throws NoSuchAlgorithmException,
NoSuchProviderException, InvalidKeyException, SecurityException,
SignatureException, IOException, CertificateEncodingException {

String attrs = "C=cn" + ", O=www.neuq.edu.cn" + ",L=QinHuangDao"
+ ",ST=HeBei" + ",E=****@mail.***.edu.cn"
+ ",OU=software center" + ",CN=Guanchun";

//first generate a certificate

X509Name subjectDN = new X509Name(attrs);
X509Name issuerDN = new X509Name(attrs);
Date from = new Date();
Date to = new Date(from.getTime() + 86400 * 1000 * 100);
BigInteger certSerial = BigInteger.valueOf(123456);

//这里 应该 使用 SecureRandom
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", "BC");
KeyPair kp = kpg.generateKeyPair();
PublicKey pubk = kp.getPublic();
PrivateKey prik = kp.getPrivate();

X509Certificate result = generateCertificate(issuerDN, certSerial,
from, to, subjectDN, pubk, prik, sigalg_DSAWITHSHA1);

//output certificate
new File("tmp.cer").createNewFile();
java.io.File file = new File("tmp.cer");
FileOutputStream fos = new FileOutputStream(file);
byte[] certbytes = result.getEncoded();
fos.write(certbytes);
fos.close();

System.out.println("OK!!KO");
System.out.println("OK!!KO");

}

/**
* @param attrs
* /**
* @return X509Certificate /**
* @throws NoSuchAlgorithmException
* /**
* @throws NoSuchProviderException
* /**
* @throws SignatureException
* /**
* @throws InvalidKeyException
* @param issuerDN
* @param serial
* @param from
* @param to
* @param subjectDN
* @param publicKey
* TODO
* @param privateKey
* TODO
* @param algorithm
* TODO
*/
public static X509Certificate generateCertificate(X509Name issuerDN,
BigInteger serial, Date from, Date to, X509Name subjectDN,
PublicKey publicKey, PrivateKey privateKey, String algorithm)

throws NoSuchAlgorithmException, NoSuchProviderException,
SignatureException, InvalidKeyException {

X509V1CertificateGenerator certGenerator = new X509V1CertificateGenerator();
certGenerator.setIssuerDN(issuerDN);
//issue serial
certGenerator.setSerialNumber(serial);
certGenerator.setSignatureAlgorithm(algorithm);
//date
certGenerator.setNotBefore(from);
certGenerator.setNotAfter(to);

//subject DN
certGenerator.setSubjectDN(subjectDN);

//public key
certGenerator.setPublicKey(publicKey);

//generate certificate using private key
X509Certificate result = certGenerator
.generateX509Certificate(privateKey);
return result;
}

}




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