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. }  

 

번호 제목 sitelink1 글쓴이 날짜 조회 수
공지 [계속 추가중] SBOM 용어 정의   황제낙엽 2025.04.10 59
공지 [계속 추가중] Keycloak 용어 및 설정 옵션 정의   황제낙엽 2024.02.02 635
13 [Copilot] javax.crypto 패키지를 사용하여 암호화, 복호화 하는 방법   황제낙엽 2024.06.07 97
12 Apache Log4j 2 보안 업데이트 권고 https://www.boho.or.kr/data/secNoticeVie...ence=36389  황제낙엽 2021.12.13 135
11 OpenSSL사용방법 메모, RSA암호의 최대 사이즈, JCA/JCE가이드   황제낙엽 2007.09.27 174
10 Java Cryptography Extension (JCE) 개요   황제낙엽 2007.09.27 347
9 Java에서 암호화하고 C++에서 복호화하는 방법   황제낙엽 2007.09.27 379
» 비밀키를 Keytool에서 취급할 수 있는 형식으로 변환방법 http://java-house.jp/ml/archive/j-h-b/051472.html  황제낙엽 2007.09.27 230
7 공개키 암호화의 수학적 알고리즘과 자바 구현   황제낙엽 2007.09.22 208
6 RSA 암호화 알고리즘을 구현한 자바예제 (산술계산)   황제낙엽 2007.09.17 327
5 RSA 암호화 프로그램 예제 (BigInteger 이용) file   황제낙엽 2007.09.08 257
4 해쉬를 이용한 패스워드 로그인   황제낙엽 2007.09.05 111
3 Java 보안과 암호화 (개론) file   황제낙엽 2007.09.05 117
2 RSA 공개키 암호화 방식 (java.security, javax.crypto, au.net.aba.crypto.provider 패키지 이용)   황제낙엽 2007.09.05 279
1 자바 암호화 기법 - MD5를 이용한 해쉬키 생성 (Hash)   황제낙엽 2007.09.01 316