sitelink1 http://java-house.jp/ml/archive/j-h-b/051472.html 
sitelink2  
sitelink3  
sitelink4  
extra_vars5  
extra_vars6  

秘密鍵を keytool で扱える形式への?換方法

 

·미리보기 | 소스복사·
 
  1. ::::::::::::::::::::::::::::   
  2. :: doit.sh   
  3. ::::::::::::::::::::::::::::   
  4. #!/bin/sh   
  5. #   
  6. # JavaKeyStore test program -- import privatekey&cert from openssl   
  7. #   
  8.   
  9. set -x   
  10.   
  11. -- force cleanup   
  12. rm -f newkeystore *.class *.der *.pem *~   
  13.   
  14. -- fetch openssl prepared privatekey & cert   
  15. SOMEWHERE=$1   
  16. cp $SOMEWHERE/client-private.pem  .   
  17. cp $SOMEWHERE/client-cert.pem  .   
  18.   
  19. -- pem -> der conversion   
  20. openssl x509 -in client-cert.pem -out client-cert.der -outform der   
  21. openssl rsa -in client-private.pem -out client-private-rsa.der -inform pem -outform der   
  22. openssl pkcs8 -topk8 -in client-private-rsa.der -inform der -out client-private.der -outform der -nocrypt   
  23.   
  24. -- compile && exec   
  25. javac -classpath . CreateKeyStore.java &&   
  26. java -classpath . CreateKeyStore paSsword pasSword newkeystore thatsme client-private.der client-cert.der    
  27.   
  28. -- show it (cert part)   
  29. keytool -v -list -storepass paSsword -keypass pasSword -keystore newkeystore   
  30.   
  31. -- show it (key part)   
  32. javac -classpath . GetPrivateKey.java &&   
  33. java -classpath . GetPrivateKey paSsword pasSword newkeystore thatsme |   
  34. openssl pkcs8 -inform der -nocrypt -outform pem |   
  35. openssl rsa -text   
  36.   
  37. -- delete intermediate files   
  38. rm -f *.class *.der *.pem *~  

 

·미리보기 | 소스복사·
 
  1. ::::::::::::::::::::::::::::   
  2. :: CreateKeyStore.java   
  3. ::::::::::::::::::::::::::::   
  4. /*  
  5.  * $ javac -classpath . CreateKeyStore.java  
  6.  * $ java -classpath . CreateKeyStore storepass keypass newkeystorename alias keyin certin  
  7.  *  
  8.  * ref:  
  9.  * j2sdk1_4_0/j2se/src/share/classes/sun/security/tools/KeyTool.java  
  10.  * j2sdk1_4_0/j2se/src/share/classes/sun/security/provider/JavaKeyStore.java  
  11.  * j2sdk1_4_0/j2se/src/share/classes/java/security/KeyStore.java  
  12.  */  
  13. import java.io.File;   
  14. import java.io.FileInputStream;   
  15. import java.io.DataInputStream;   
  16. import java.io.FileOutputStream;   
  17. import java.security.Key;   
  18. import java.security.KeyStore;   
  19. import java.security.PrivateKey;   
  20. import java.security.KeyFactory;   
  21. import java.security.cert.Certificate;   
  22. import java.security.cert.CertificateFactory;   
  23. import java.security.cert.X509Certificate;   
  24. import java.security.interfaces.RSAPrivateKey;   
  25. import java.security.spec.PKCS8EncodedKeySpec;   
  26. public class CreateKeyStore {   
  27.   public static void main(String[] args) throws Exception {   
  28.     /*   
  29.      * variables  
  30.      */  
  31.     String spass = args[0];   
  32.     String kpass = args[1];   
  33.     String keystore = args[2];   
  34.     String alias = args[3];   
  35.     String keyin = args[4];   
  36.     String certin = args[5];   
  37.     /*  
  38.      * load given private key  
  39.      */  
  40.     KeyFactory kf = KeyFactory.getInstance("RSA");   
  41.     File f = new File(keyin);   
  42.     byte[] b = new byte[(int) f.length()];   
  43.     DataInputStream i = new DataInputStream(new FileInputStream(f));   
  44.     i.readFully(b);   
  45.     i.close();   
  46.     Key key = kf.generatePrivate(new PKCS8EncodedKeySpec(b));   
  47.     /*  
  48.      * load given certificate  
  49.      */  
  50.     FileInputStream in = new FileInputStream(certin);   
  51.     CertificateFactory cf = CertificateFactory.getInstance("X509");   
  52.     X509Certificate cert = (X509Certificate) cf.generateCertificate(in);   
  53.     in.close();   
  54.     /*  
  55.      * create a fresh keystore with given privatekey and certificate   
  56.      */  
  57.     KeyStore ks = KeyStore.getInstance("JKS");   
  58.     ks.load(nullnull);        // <= means ``inititlize''   
  59.     ks.setKeyEntry(alias, key, kpass.toCharArray(), new Certificate[] { cert, });   
  60.     FileOutputStream os = new FileOutputStream(keystore);   
  61.     ks.store(os, spass.toCharArray());   
  62.     os.close();   
  63.   }   
  64. }  
·미리보기 | 소스복사·
 
  1. ::::::::::::::::::::::::::::   
  2. :: GetPrivateKey.java   
  3. ::::::::::::::::::::::::::::   
  4. /*  
  5.  * $ javac GetPrivateKey.java  
  6.  * $ java GetPrivateKey storepass keypass keystore alias >key.out  
  7.  */  
  8. import java.security.KeyStore;   
  9. import java.security.Key;   
  10. import java.io.FileInputStream;   
  11. public class GetPrivateKey {   
  12.   public static void main(String[] args) throws Exception {   
  13.     String spass = args[0];   
  14.     String kpass = args[1];   
  15.     String keystore = args[2];   
  16.     String alias = args[3];   
  17.     KeyStore ks = KeyStore.getInstance("JKS");   
  18.     FileInputStream fs = new FileInputStream(keystore);   
  19.     ks.load(fs, spass.toCharArray());   
  20.     Key key = ks.getKey(alias, kpass.toCharArray());   
  21.     System.out.write(key.getEncoded());   
  22.   }   
  23. }  

 

번호 제목 글쓴이 날짜 조회 수
공지 [계속 추가중] SBOM 용어 정의 황제낙엽 2025.04.10 9310
공지 [계속 추가중] Keycloak 용어 및 설정 옵션 정의 황제낙엽 2024.02.02 9392
27 해쉬 알고리즘의 종류 황제낙엽 2009.12.01 1611
26 Ajax 보안 관련 문서 (Attacking AJAX Web Applications) file 황제낙엽 2009.02.12 1507
25 blowfish 알고리즘 file 황제낙엽 2008.01.22 1478
24 OpenSSL을 이용한 보안 통신 API의 설계 및 구현 file 황제낙엽 2007.10.01 1474
23 Windows환경에서의 OpenSSL설치 file 황제낙엽 2007.09.28 1387
22 OpenSSL Command-Line HOWTO 황제낙엽 2007.09.27 2221
21 Certificate Server의 설치 와 Client인증 황제낙엽 2007.09.27 1457
20 OpenSSL의 설치 및 운영 황제낙엽 2007.09.27 1511
19 OpenSSL 프로그래밍 황제낙엽 2007.09.27 3403
18 OpenSSL 을 통한 파일 암호화 황제낙엽 2007.09.27 1855
17 OpenSSL 과 OpenSSH 소스 파일 (Language : C) 황제낙엽 2007.09.24 2277
16 RSA 암호화 알고리즘을 구현한 C++ 예제 file 황제낙엽 2007.09.17 3469
15 RSA암호화를 이용한 로그인 ID/패스워드 정보 관리 황제낙엽 2007.09.05 1639
14 PHP와 OpenSSL 황제낙엽 2007.09.27 2383
13 RSA 공개키 암호화 알고리즘 - PHP 구현[2] file 황제낙엽 2007.09.27 3230
12 RSA 공개키 암호화 알고리즘 - PHP 구현[1] file 황제낙엽 2007.09.05 2410
11 OpenSSL사용방법 메모, RSA암호의 최대 사이즈, JCA/JCE가이드 황제낙엽 2007.09.27 2260
10 Java Cryptography Extension (JCE) 개요 황제낙엽 2007.09.27 2875
9 Java에서 암호화하고 C++에서 복호화하는 방법 황제낙엽 2007.09.27 2588
» 비밀키를 Keytool에서 취급할 수 있는 형식으로 변환방법 황제낙엽 2007.09.27 2802