久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在openssl生成的java中使用.key和.crt文件?

How to use .key and .crt file in java that generated by openssl?(如何在openssl生成的java中使用.key和.crt文件?)
本文介紹了如何在openssl生成的java中使用.key和.crt文件?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

我需要java中的非對(duì)稱加密.我使用 http://www.imacat.idv.tw/tech/sslcerts.html .
如何使用這些 .key 和 .crt 文件提取 Java 中的公鑰和私鑰?

I need asymmetric encryption in java. I generate .key and .crt files with own password and .crt file by openssl that said in http://www.imacat.idv.tw/tech/sslcerts.html .
How to use these .key and .crt file to extract publickey and private key in Java?

推薦答案

您的 .key.crt 文件可能是 PEM 格式.要檢查這一點(diǎn),請(qǐng)使用文本編輯器打開它們并檢查內(nèi)容是否類似于 -----BEGIN CERTIFICATE----- (或開始 RSA 私鑰"...).這通常是 OpenSSL 使用的默認(rèn)格式,除非您明確指定 DER.

Your .key and .crt files may be in PEM format. To check this open them with a text editor and check whether the content looks like ------BEGIN CERTIFICATE------ (or "begin RSA private key"...). This is generally the default format used by OpenSSL, unless you've explicitly specified DER.

這可能不是必需的(見下文),但如果您的證書是 DER 格式(二進(jìn)制格式),您可以使用以下方法將它們轉(zhuǎn)換為 PEM 格式:

It's probably not required (see below), but if your certificate is in DER format (a binary format), you can convert them in PEM format using:

openssl x509 -inform DER -in cert.crt -outform PEM -out cert.pem

(如果需要,請(qǐng)查看 openssl rsa 的幫助以使用私鑰執(zhí)行類似操作.)

(Check the help for openssl rsa for doing something similar with the private key if needed.)

然后你有兩個(gè)選擇:

  • 構(gòu)建一個(gè) PKCS#12 文件

  • Build a PKCS#12 file

openssl pkcs12 -export -in myhost.crt -inkey myhost.key -out myhost.p12

然后,您可以直接從 Java 中將其用作PKCS12"類型的密鑰庫(kù).除文件位置外,大多數(shù) Java 應(yīng)用程序都應(yīng)允許您指定密鑰庫(kù)類型.對(duì)于默認(rèn)系統(tǒng)屬性,這是通過 javax.net.ssl.keyStoreType 完成的(但您正在使用的應(yīng)用程序可能不使用它).否則,如果您想顯式加載它,請(qǐng)使用以下內(nèi)容:

You can then use it directly from Java as a keystore of type "PKCS12". Most Java applications should allow you to specify a keystore type in addition to the file location. For the default system properties, this is done with javax.net.ssl.keyStoreType (but the application you're using might not be using this). Otherwise, if you want to load it explicitly, use something like this:

KeyStore ks = KeyStore.getInstance("PKCS12");
FileInputStream fis =
    new FileInputStream("/path/to/myhost.p12");
ks.load(fis, "password".toCharArray()); // There are other ways to read the password.
fis.close();

(然后,您應(yīng)該能夠遍歷 aliases()/security/KeyStore.html" rel="noreferrer">KeyStore 并使用 getCertificate (然后使用 getPublicKey()公鑰)和 getKey().

(Then, you should be able to iterate through the aliases() of the KeyStore and use getCertificate (and then getPublicKey() for the public key) and getKey().

  • 使用 BouncyCastle 的 PEMReader.

 FileReader fr = ... // Create a FileReader for myhost.crt
 PEMReader pemReader = new PEMReader(fr);
 X509Certificate cert = (X509Certificate)pemReader.readObject();
 PublicKey pk = cert.getPublicKey();
 // Close reader...

對(duì)于私鑰,如果私鑰受密碼保護(hù),則需要實(shí)現(xiàn) PasswordFinder(請(qǐng)參閱 PEMReader 文檔中的鏈接)來(lái)構(gòu)建 PEMReader.(您需要將 readObject() 的結(jié)果轉(zhuǎn)換為 KeyPrivateKey.)

For the private key, you'll need to implement a PasswordFinder (see link from PEMReader doc) for constructing the PEMReader if the private key is password-protected. (You'll need to cast the result of readObject() into a Key or PrivateKey.)

這篇關(guān)于如何在openssl生成的java中使用.key和.crt文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 成人免费视频网站在线看 | 国产高清视频一区 | 最新黄色在线观看 | 一本色道精品久久一区二区三区 | 久久久综合色 | 欧美亚洲国产日韩 | 午夜三级在线观看 | 成人在线视频一区 | 亚洲精品视频在线看 | 91麻豆精品国产91久久久久久久久 | 国产色在线 | 午夜久久久久久久久久一区二区 | 精品国产乱码久久久久久图片 | 97久久久久久久久 | 中文字幕在线视频精品 | 毛片一区二区三区 | 国产精品久久国产精品 | 成人日韩| 免费毛片网 | 色播视频在线观看 | 欧美激情综合色综合啪啪五月 | 久久美女网 | 啪啪免费网站 | 国产午夜精品视频 | 午夜影院在线观看免费 | 久久6视频 | 欧美精品久久久 | 国产成人高清在线观看 | 国产一区二区三区四区 | 欧美一区二区在线观看 | 91毛片在线看 | 中文字幕av网 | 天堂资源最新在线 | 91av久久久 | 三a毛片 | 亚洲国产黄 | 九九热免费在线观看 | 中国黄色在线视频 | 日韩在线免费 | 国产超碰人人爽人人做人人爱 | 亚洲区一区二区 |