當前位置:首頁 » 編程語言 » java密鑰生成

java密鑰生成

發布時間: 2025-09-24 23:01:25

A. java鐢熸垚RSA闈炲圭О鍨嬪姞瀵嗙殑鍏閽ュ拰縐侀掗

銆銆闈炲圭О鍨嬪姞瀵嗛潪甯擱傚悎澶氫釜瀹㈡埛絝鍜屾湇鍔″櫒涔嬮棿鐨勭樺瘑閫氳 瀹㈡埛絝浣跨敤鍚屼竴涓鍏閽ュ皢鏄庢枃鍔犲瘑 鑰岃繖涓鍏閽ヤ笉鑳介嗗悜鐨勮В瀵 瀵嗘枃鍙戦佸埌鏈嶅姟鍣ㄥ悗鏈夋湇鍔″櫒絝鐢ㄧ侀掗瑙e瘑 榪欐牱灝卞仛鍒頒簡鏄庢枃鐨勫姞瀵嗕紶閫

銆銆闈炲圭О鍨嬪姞瀵嗕篃鏈夊畠鍏堝ぉ鐨勭己鐐 鍔犲瘑 瑙e瘑閫熷害鎱㈠埗綰︿簡瀹冪殑鍙戞尌 濡傛灉浣犳湁澶ч噺鐨勬枃瀛楅渶瑕佸姞瀵嗕紶閫 寤鴻浣犻氳繃闈炲圭О鍨嬪姞瀵嗘潵鎶婂圭О鍨 瀵嗛掗 鍒嗗彂鍒板㈡埛絝 鍙婃椂鏇存柊瀵圭О鍨 瀵嗛掗

銆銆import java io *;

銆銆import java security *;

銆銆import javax crypto *;

銆銆import javax crypto spec *;

銆銆/**

銆銆* <p>Title: RSA闈炲圭О鍨嬪姞瀵嗙殑鍏閽ュ拰縐侀掗</p>

銆銆* <p>Description: </p>

銆銆* <p>Copyright: Copyright (c) </p>

銆銆* <p>Company: </p>

銆銆* @author not attributable

銆銆* @version

銆銆*/

銆銆public class KeyRSA {

銆銆private KeyPairGenerator kpg = null;

銆銆private KeyPair kp = null;

銆銆private PublicKey public_key = null;

銆銆private PrivateKey private_key = null;

銆銆private FileOutputStream public_file_out = null;

銆銆private ObjectOutputStream public_object_out = null;

銆銆private FileOutputStream private_file_out = null;

銆銆private ObjectOutputStream private_object_out = null;

銆銆/**

銆銆* 鏋勯犲嚱鏁

銆銆* @param in 鎸囧畾瀵嗗寵闀垮害錛堝彇鍊艱寖鍥 鍀 錛

銆銆* @throws NoSuchAlgorithmException 寮傚父

銆銆*/

銆銆public KeyRSA(int in String address) throws NoSuchAlgorithmException FileNotFoundException IOException

銆銆{

銆銆kpg = KeyPairGenerator getInstance( RSA ); //鍒涘緩 瀵嗗寵瀵 鐢熸垚鍣

銆銆kpg initialize(in); //鎸囧畾瀵嗗寵闀垮害錛堝彇鍊艱寖鍥 鍀 錛

銆銆kp = kpg genKeyPair(); //鐢熸垚 瀵嗗寵瀵 鍏朵腑鍖呭惈鐫涓涓鍏鍖欏拰涓涓縐佸寵鐨勪俊鎮

銆銆public_key = kp getPublic(); //鑾峰緱鍏鍖

銆銆private_key = kp getPrivate(); //鑾峰緱縐佸寵

銆銆//淇濆瓨鍏鍖

銆銆public_file_out = new FileOutputStream(address + /public_key dat );

銆銆public_object_out = new ObjectOutputStream(public_file_out);

銆銆public_object_out writeObject(public_key);

銆銆//淇濆瓨縐佸寵

銆銆private_file_out = new FileOutputStream(address + /private_key dat );

銆銆private_object_out = new ObjectOutputStream(private_file_out);

銆銆private_object_out writeObject(private_key);

銆銆}

銆銆public static void main(String[] args) {

銆銆try {

銆銆System out println( 縐佸寵鍜屽叕鍖欎繚瀛樺埌C鐩樹笅鐨勬枃浠朵腑 );

銆銆new KeyRSA( c:/ );

銆銆}

銆銆catch (IOException ex) {

銆銆}

銆銆catch (NoSuchAlgorithmException ex) {

銆銆}

銆銆}

lishixin/Article/program/Java/hx/201311/26592

B. 用Java編寫一個程序,生成公鑰和私鑰對

一:需要包含的包
import java.security.*;
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.cert.*;
import sun.security.x509.*
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

二:從文件中讀取證書
用keytool將.keystore中的證書寫入文件中,然後從該文件中讀取證書信息
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in=new FileInputStream("out.csr");
Certificate c=cf.generateCertificate(in);

String s=c.toString();
三:從密鑰庫中直接讀取證書
String pass="123456";
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,pass.toCharArray());
java.security.cert.Certificate c=ks.getCertificate(alias);//alias為條目的別名

四:JAVA程序中顯示證書指定信息
System.out.println("輸出證書信息:\n"+c.toString());
System.out.println("版本號:"+t.getVersion());
System.out.println("序列號:"+t.getSerialNumber().toString(16));
System.out.println("主體名:"+t.getSubjectDN());
System.out.println("簽發者:"+t.getIssuerDN());
System.out.println("有效期:"+t.getNotBefore());
System.out.println("簽名演算法:"+t.getSigAlgName());
byte [] sig=t.getSignature();//簽名值
PublicKey pk=t.getPublicKey();
byte [] pkenc=pk.getEncoded();
System.out.println("公鑰");
for(int i=0;i<pkenc.length;i++)System.out.print(pkenc[i]+",");

五:JAVA程序列出密鑰庫所有條目
String pass="123456";
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,pass.toCharArray());
Enumeration e=ks.aliases();
while(e.hasMoreElements())
java.security.cert.Certificate c=ks.getCertificate((String)e.nextElement());

六:JAVA程序修改密鑰庫口令
String oldpass="123456";
String newpass="654321";
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,oldpass.toCharArray());
in.close();
FileOutputStream output=new FileOutputStream(".keystore");
ks.store(output,newpass.toCharArray());
output.close();

七:JAVA程序修改密鑰庫條目的口令及添加條目
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,storepass.toCharArray());
Certificate [] cchain=ks.getCertificate(alias);獲取別名對應條目的證書鏈
PrivateKey pk=(PrivateKey)ks.getKey(alias,oldkeypass.toCharArray());獲取別名對應條目的私鑰
ks.setKeyEntry(alias,pk,newkeypass.toCharArray(),cchain);向密鑰庫中添加條目
第一個參數指定所添加條目的別名,假如使用已存在別名將覆蓋已存在條目,使用新別名將增加一個新條目,第二個參數為條目的私鑰,第三個為設置的新口令,第四個為該私鑰的公鑰的證書鏈
FileOutputStream output=new FileOutputStream("another");
ks.store(output,storepass.toCharArray())將keystore對象內容寫入新文件

八:JAVA程序檢驗別名和刪除條目
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,storepass.toCharArray());
ks.containsAlias("sage");檢驗條目是否在密鑰庫中,存在返回true
ks.deleteEntry("sage");刪除別名對應的條目
FileOutputStream output=new FileOutputStream(".keystore");
ks.store(output,storepass.toCharArray())將keystore對象內容寫入文件,條目刪除成功

九:JAVA程序簽發數字證書
(1)從密鑰庫中讀取CA的證書
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,storepass.toCharArray());
java.security.cert.Certificate c1=ks.getCertificate("caroot");
(2)從密鑰庫中讀取CA的私鑰
PrivateKey caprk=(PrivateKey)ks.getKey(alias,cakeypass.toCharArray());
(3)從CA的證書中提取簽發者的信息
byte[] encod1=c1.getEncoded(); 提取CA證書的編碼
X509CertImpl cimp1=new X509CertImpl(encod1); 用該編碼創建X509CertImpl類型對象
X509CertInfo cinfo1=(X509CertInfo)cimp1.get(X509CertImpl.NAME+"."+X509CertImpl.INFO); 獲取X509CertInfo對象
X500Name issuer=(X500Name)cinfo1.get(X509CertInfo.SUBJECT+"."+CertificateIssuerName.DN_NAME); 獲取X509Name類型的簽發者信息
(4)獲取待簽發的證書
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in2=new FileInputStream("user.csr");
java.security.cert.Certificate c2=cf.generateCertificate(in);
(5)從待簽發的證書中提取證書信息
byte [] encod2=c2.getEncoded();
X509CertImpl cimp2=new X509CertImpl(encod2); 用該編碼創建X509CertImpl類型對象
X509CertInfo cinfo2=(X509CertInfo)cimp2.get(X509CertImpl.NAME+"."+X509CertImpl.INFO); 獲取X509CertInfo對象
(6)設置新證書有效期
Date begindate=new Date(); 獲取當前時間
Date enddate=new Date(begindate.getTime()+3000*24*60*60*1000L); 有效期為3000天
CertificateValidity cv=new CertificateValidity(begindate,enddate); 創建對象
cinfo2.set(X509CertInfo.VALIDITY,cv); 設置有效期
(7)設置新證書序列號
int sn=(int)(begindate.getTime()/1000); 以當前時間為序列號
CertificateSerialNumber csn=new CertificateSerialNumber(sn);
cinfo2.set(X509CertInfo.SERIAL_NUMBER,csn);
(8)設置新證書簽發者
cinfo2.set(X509CertInfo.ISSUER+"."+CertificateIssuerName.DN_NAME,issuer);應用第三步的結果
(9)設置新證書簽名演算法信息
AlgorithmId algorithm=new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
cinfo2.set(CertificateAlgorithmId.NAME+"."+CertificateAlgorithmId.ALGORITHM,algorithm);
(10)創建證書並使用CA的私鑰對其簽名
X509CertImpl newcert=new X509CertImpl(cinfo2);
newcert.sign(caprk,"MD5WithRSA"); 使用CA私鑰對其簽名
(11)將新證書寫入密鑰庫
ks.setCertificateEntry("lf_signed",newcert);
FileOutputStream out=new FileOutputStream("newstore");
ks.store(out,"newpass".toCharArray()); 這里是寫入了新的密鑰庫,也可以使用第七條來增加條目

十:數字證書的檢驗
(1)驗證證書的有效期
(a)獲取X509Certificate類型對象
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in1=new FileInputStream("aa.crt");
java.security.cert.Certificate c1=cf.generateCertificate(in1);
X509Certificate t=(X509Certificate)c1;
in2.close();
(b)獲取日期
Date TimeNow=new Date();
(c)檢驗有效性
try{
t.checkValidity(TimeNow);
System.out.println("OK");
}catch(CertificateExpiredException e){ //過期
System.out.println("Expired");
System.out.println(e.getMessage());
}catch(( e){ //尚未生效
System.out.println("Too early");
System.out.println(e.getMessage());}
(2)驗證證書簽名的有效性
(a)獲取CA證書
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in2=new FileInputStream("caroot.crt");
java.security.cert.Certificate cac=cf.generateCertificate(in2);
in2.close();
(c)獲取CA的公鑰
PublicKey pbk=cac.getPublicKey();
(b)獲取待檢驗的證書(上步已經獲取了,就是C1)
(c)檢驗證書
boolean pass=false;
try{
c1.verify(pbk);
pass=true;
}catch(Exception e){
pass=false;
System.out.println(e);
}

熱點內容
class反編譯為java 發布:2025-09-25 00:25:58 瀏覽:54
盤JAVA 發布:2025-09-25 00:22:50 瀏覽:810
java我的世界怎麼免費開伺服器 發布:2025-09-25 00:00:52 瀏覽:941
婦癌資料庫 發布:2025-09-24 23:53:53 瀏覽:785
fcsan存儲 發布:2025-09-24 23:52:19 瀏覽:936
androidjson數組 發布:2025-09-24 23:41:32 瀏覽:849
攝影腳本模板 發布:2025-09-24 23:38:13 瀏覽:447
更換伺服器和更換ip區別 發布:2025-09-24 23:36:11 瀏覽:893
android與或非 發布:2025-09-24 23:30:59 瀏覽:312
微信商店源碼 發布:2025-09-24 23:23:37 瀏覽:704