Java Keystore를 PEM 형식으로 변환
keytool 및 openssl 응용 프로그램을 사용하여 Java 키 저장소 파일에서 PEM 파일로 변환하려고합니다. 그러나 나는 전환을 수행하는 좋은 방법을 찾지 못했습니다. 어떤 아이디어?
키 저장소를 PEM으로 직접 변환하는 대신 PKCS12 파일을 먼저 작성한 다음 관련 PEM 파일 및 키 저장소로 변환하려고했습니다. 그러나 그것들을 사용하여 연결을 설정할 수 없습니다. (보안 연결을 구현하려면 PEM 파일과 키 저장소 파일 만 있으면됩니다. "java 키 저장소 파일에서 시작"과 같은 제한은 없습니다. :) 따라서 다른 형식에서 시작해도 괜찮습니다.
그러나 jks에서 pem으로 직접 변환하는 방법이 바람직합니다.
적어도 jdk6을 사용하는 것은 매우 간단합니다 ...
bash $ keytool -keystore foo.jks -genkeypair-별명 foo \ -dname 'CN = foo.example.com, L = 멜버른, ST = 빅토리아, C = AU' 키 저장소 비밀번호를 입력하십시오. 새 비밀번호를 다시 입력하십시오 : 에 대한 키 비밀번호를 입력하십시오 (키 저장소 비밀번호와 동일한 경우 리턴) : bash $ keytool -keystore foo.jks -exportcert-별칭 foo | \ openssl x509-정보 der- 텍스트 키 저장소 비밀번호를 입력하십시오. asdasd 증명서: 데이터: 버전 : 3 (0x2) 일련 번호 : 1237334757 (0x49c03ae5) 서명 알고리즘 : dsaWithSHA1 발급자 : C = AU, ST = 빅토리아, L = 멜버른, CN = foo.example.com 타당성 비정기 : 3 월 18 일 00:05:57 2009 GMT 이후 : Jun 16 00:05:57 2009 GMT 제목 : C = AU, ST = 빅토리아, L = 멜버른, CN = foo.example.com 주제 공개 키 정보 : 공개 키 알고리즘 : dsaEncryption DSA 공개 키 : 선술집: 00 : e2 : 66 : 5c : e0 : 2e : da : e0 : 6b : a6 : aa : 97 : 64 : 59 : 14 : 7e : a6 : 2e : 5a : 45 : f9 : 2f : b5 : 2d : f4 : 34 : 27 : e6 : 53 : c7 : bash $ keytool -importkeystore -srckeystore foo.jks \ -destkeystore foo.p12 \ -srcstoretype jks \ -deststoretype pkcs12 대상 키 저장소 비밀번호를 입력하십시오. 새 비밀번호를 다시 입력하십시오 : 소스 키 저장소 비밀번호를 입력하십시오. 별명 foo에 대한 항목을 가져 왔습니다. 가져 오기 명령 완료 : 1 개의 항목을 성공적으로 가져 왔으며 0 개의 항목이 실패했거나 취소되었습니다. bash $ openssl pkcs12 -in foo.p12 -out foo.pem 비밀번호 가져 오기를 입력하십시오. MAC 확인 OK PEM 암호 문구를 입력하십시오 : 확인-PEM 암호 문구를 입력하십시오. bash $ openssl x509-텍스트-foo.pem 증명서: 데이터: 버전 : 3 (0x2) 일련 번호 : 1237334757 (0x49c03ae5) 서명 알고리즘 : dsaWithSHA1 발급자 : C = AU, ST = 빅토리아, L = 멜버른, CN = foo.example.com 타당성 비정기 : 3 월 18 일 00:05:57 2009 GMT 이후 : Jun 16 00:05:57 2009 GMT 제목 : C = AU, ST = 빅토리아, L = 멜버른, CN = foo.example.com 주제 공개 키 정보 : 공개 키 알고리즘 : dsaEncryption DSA 공개 키 : 선술집: 00 : e2 : 66 : 5c : e0 : 2e : da : e0 : 6b : a6 : aa : 97 : 64 : 59 : 14 : 7e : a6 : 2e : 5a : 45 : f9 : 2f : b5 : 2d : f4 : 34 : 27 : e6 : 53 : c7 : bash $ openssl dsa -text -in foo.pem DSA 키 읽기 PEM 암호 문구를 입력하십시오 : 개인 키 : (1024 비트) 개인 : 00 : 8f : b1 : af : 55 : 63 : 92 : 7c : d2 : 0f : e6 : f3 : a2 : f5 : ff : 1a : 7a : fe : 8c : 39 : dd 선술집: 00 : e2 : 66 : 5c : e0 : 2e : da : e0 : 6b : a6 : aa : 97 : 64 : 59 : 14 : 7e : a6 : 2e : 5a : 45 : f9 : 2f : b5 : 2d : f4 : 34 : 27 : e6 : 53 : c7 :
당신은 결국 :
- foo.jks-Java 형식의 키 저장소
- foo.p12-PKCS # 12 형식의 키 저장소
- foo.pem-키 저장소의 모든 키 및 인증서 (PEM 형식)
(이 마지막 파일은 원하는 경우 키와 인증서로 나눌 수 있습니다.)
명령 요약-JKS 키 저장소를 작성하려면 다음을 수행하십시오.
keytool -keystore foo.jks -genkeypair -alias foo \
-dname 'CN=foo.example.com,L=Melbourne,ST=Victoria,C=AU'
명령 요약-JKS 키 저장소를 PKCS # 12 키 저장소로 변환 한 다음 PEM 파일로 변환
keytool -importkeystore -srckeystore foo.jks \
-destkeystore foo.p12 \
-srcstoretype jks \
-deststoretype pkcs12
openssl pkcs12 -in foo.p12 -out foo.pem
JKS 키 저장소에 둘 이상의 인증서가 있고 별명 중 하나와 연관된 인증서 및 키만 내보내려면 다음 변형을 사용할 수 있습니다.
keytool -importkeystore -srckeystore foo.jks \
-destkeystore foo.p12 \
-srcalias foo \
-srcstoretype jks \
-deststoretype pkcs12
openssl pkcs12 -in foo.p12 -out foo.pem
명령 요약-JKS 키 저장소를 PEM 파일과 비교하려면 다음을 수행하십시오.
keytool -keystore foo.jks -exportcert -alias foo | \
openssl x509 -inform der -text
openssl x509 -text -in foo.pem
openssl dsa -text -in foo.pem
openssl
StoBor의 명령을 사용할 때 계속 오류가 발생합니다 .
MAC verified OK
Error outputting keys and certificates
139940235364168:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:535:
139940235364168:error:23077074:PKCS12 routines:PKCS12_pbe_crypt:pkcs12 cipherfinal error:p12_decr.c:97:
139940235364168:error:2306A075:PKCS12 routines:PKCS12_item_decrypt_d2i:pkcs12 pbe crypt error:p12_decr.c:123:
어떤 이유로 든이 스타일의 명령 만 내 JKS 파일에서 작동합니다.
keytool -importkeystore -srckeystore foo.jks \
-destkeystore foo.p12 \
-srcstoretype jks \
-srcalias mykey \
-deststoretype pkcs12 \
-destkeypass DUMMY123
열쇠는 설정했다 destkeypass
, 인수의 가치는 중요하지 않았다.
The keytool
command will not allow you to export the private key from a key store. You have to write some Java code to do this. Open the key store, get the key you need, and save it to a file in PKCS #8 format. Save the associated certificate too.
KeyStore ks = KeyStore.getInstance("jks");
/* Load the key store. */
...
char[] password = ...;
/* Save the private key. */
FileOutputStream kos = new FileOutputStream("tmpkey.der");
Key pvt = ks.getKey("your_alias", password);
kos.write(pvt.getEncoded());
kos.flush();
kos.close();
/* Save the certificate. */
FileOutputStream cos = new FileOutputStream("tmpcert.der");
Certificate pub = ks.getCertificate("your_alias");
cos.write(pub.getEncoded());
cos.flush();
cos.close();
Use OpenSSL utilities to convert these files (which are in binary format) to PEM format.
openssl pkcs8 -inform der -nocrypt < tmpkey.der > tmpkey.pem
openssl x509 -inform der < tmpcert.der > tmpcert.pem
Direct conversion from jks to pem file using the keytool
keytool -exportcert -alias selfsigned -keypass password -keystore test-user.jks -rfc -file test-user.pem
Simplified instructions to converts a JKS file to PEM and KEY format (.crt & .key):
keytool -importkeystore -srckeystore <Source-Java-Key-Store-File> -destkeystore <Destination-Pkcs12-File> -srcstoretype jks -deststoretype pkcs12 -destkeypass <Destination-Key-Password>
openssl pkcs12 -in <Destination-Pkcs12-File> -out <Destination-Pem-File>
openssl x509 -outform der -in <Destination-Pem-File> -out <Destination-Crt-File>
openssl rsa -in <Destination-Pem-File> -out <Destination-Key-File>
I found a very interesting solution:
http://www.swview.org/node/191
Then, I divided the pair public/private key into two files private.key publi.pem and it works!
Well, OpenSSL should do it handily from a #12 file:
openssl pkcs12 -in pkcs-12-certificate-file -out pem-certificate-file
openssl pkcs12 -in pkcs-12-certificate-and-key-file -out pem-certificate-and-key-file
Maybe more details on what the error/failure is?
Converting a JKS KeyStore to a single PEM file can easily be accomplished using the following command:
keytool -list -rfc -keystore "myKeystore.jks" | sed -e "/-*BEGIN [A-Z]*-*/,/-*END [A-Z]-*/!d" >> "myKeystore.pem"
Explanation:
keytool -list -rfc -keystore "myKeystore.jks"
lists everything in the 'myKeyStore.jks' KeyStore in PEM format. However, it also prints extra information.| sed -e "/-*BEGIN [A-Z]*-*/,/-*END [A-Z]-*/!d"
filters out everything we don't need. We are left with only the PEMs of everything in the KeyStore.>> "myKeystore.pem"
write the PEMs to the file 'myKeyStore.pem'.
In case you don't have openssl installed and you are looking for a quick solution, there is software called portcle which is very useful and small to download.
The disadvantage is that there is no command line as far as I know. But from the GUI, it is pretty straight forward to export a PEM private key:
- Open you JKS key store
- Right click over your private key entry and select export
Select Private Key and certificates and PEM format
Try Keystore Explorer http://keystore-explorer.org/
KeyStore Explorer is an open source GUI replacement for the Java command-line utilities keytool and jarsigner. It does openssl/pkcs12 as well.
first create keystore file as
C:\Program Files\Android\Android Studio\jre\bin>keytool -keystore androidkey.jks -genkeypair -alias androidkey
Enter keystore password:
Re-enter new password:
What is your first and last name? Unknown: FirstName LastName
What is the name of your organizational unit? Unknown: Mobile Development
What is the name of your organization? Unknown: your company name
What is the name of your City or Locality? What is the name of your State or Province?
What is the two-letter country code for this unit? Unknown: IN //press enter
Now it will ask to confirm
Is CN=FirstName LastName, OU=Mobile Development, O=your company name, L=CityName, ST=StateName, C=IN correct? [no]: yes
Enter key password for (RETURN if same as keystore password): press enter if you want same password
key has been generated, now you can simply get pem file using following command
C:\Program Files\Android\Android Studio\jre\bin>keytool -export -rfc -alias androidkey -file android_certificate.pem -keystore androidkey.jks
Enter keystore password:
Certificate stored in file
First dump the keystore from JKS to PKCS12
1. keytool -importkeystore -srckeystore ~/.android/debug.keystore -destkeystore intermediate.p12 -srcstoretype JKS -deststoretype PKCS12
Dump the new pkcs12 file into pem
- openssl pkcs12 -in intermediate.p12 -nodes -out intermediate.rsa.pem
You should have both the cert and private key in pem format. Split them up. Put the part between “BEGIN CERTIFICATE” and “END CERTIFICATE” into cert.x509.pem Put the part between “BEGIN RSA PRIVATE KEY” and “END RSA PRIVATE KEY” into private.rsa.pem Convert the private key into pk8 format as expected by signapk
3. openssl pkcs8 -topk8 -outform DER -in private.rsa.pem -inform PEM -out private.pk8 -nocrypt
Converting a Java Keystore into PEM Format
The most precise answer of all must be that this is NOT possible.
A Java keystore is merely a storage facility for cryptographic keys and certificates while PEM is a file format for X.509 certificates only.
참고URL : https://stackoverflow.com/questions/652916/converting-a-java-keystore-into-pem-format
'Programing' 카테고리의 다른 글
Eclipse의 패키지 탐색기에서 트리 항목의 글꼴 크기를 어떻게 구성 할 수 있습니까? (0) | 2020.07.11 |
---|---|
서로 다른 간격으로 작업을 수행 할 수있는 Nodejs 스케줄러가 필요합니다. (0) | 2020.07.11 |
Angular에서 angular.copy의 대안 (0) | 2020.07.11 |
현재 도메인 받기 (0) | 2020.07.11 |
Xcode6 : 시뮬레이터의 두 인스턴스 실행 (0) | 2020.07.11 |