JAVA Java에서 암호화하고 C++에서 복호화하는 방법

황제낙엽 2007.09.27 13:40 조회 수 : 2588 추천:107

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

주의 사항 : 개인적으로도 검증하지 못한 예제이므로 충분히 검토후에 사용할 것

1. 대상환경
    - 웹서버 : AIX WebSpehere어플리케이션서버 4.0
    - 클라이언트 : WindowsNT VisualC++6.0

2. Java で暗?化してC++ で復?する方法

·미리보기 | 소스복사·
  1. import java.io.File;   
  2. import java.io.FileOutputStream;   
  3. import java.io.FileInputStream;   
  4. import java.security.KeyFactory;   
  5. import java.security.Key;   
  6. import java.security.Security;   
  7. import java.security.spec.PKCS8EncodedKeySpec;   
  8. import java.security.spec.X509EncodedKeySpec;   
  9. import javax.crypto.Cipher;   
  10. import cryptix.jce.provider.CryptixCrypto;   
  11.   
  12. /*  
  13.  * encrypt stuff with RSA (cryptix)  
  14.  *  
  15.  * javac -classpath .:cryptix-jce-api.jar:cryptix-jce-provider.jar C.java  
  16.  * java -classpath .:cryptix-jce-api.jar:cryptix-jce-provider.jar C  
  17.  */  
  18. public class C {   
  19.   public static void main(String[] args) throws Exception {   
  20.     File f;   
  21.     FileInputStream i;   
  22.     FileOutputStream o;   
  23.     Runtime r = Runtime.getRuntime();   
  24.     byte[] plain = "howdy!n".getBytes();   
  25.     Security.addProvider(new CryptixCrypto());   
  26.     /* generate keys */  
  27.     r.exec("openssl genrsa -out pri.pem").waitFor();   
  28.     r.exec("openssl rsa -in pri.pem -pubout -outform DER -out pub.der").waitFor();   
  29.     r.exec("openssl rsa -in pri.pem -outform DER -out pri-rsa.der").waitFor();   
  30.     r.exec("openssl pkcs8 -nocrypt -topk8 -inform DER -in pri-rsa.der -outform DER -out pri.der").waitFor();   
  31.     /* read in keys */  
  32.     f = new File("pub.der");   
  33.     byte[] pub_e = new byte[(int) f.length()];   
  34.     i = new FileInputStream(f);   
  35.     i.read(pub_e);   
  36.     i.close();   
  37.     f = new File("pri.der"); //(not used)   
  38.     byte[] pri_e = new byte[(int) f.length()];   
  39.     i = new FileInputStream(f);   
  40.     i.read(pri_e);   
  41.     i.close();   
  42.     KeyFactory kf = KeyFactory.getInstance("RSA");   
  43.     Key pub = kf.generatePublic(new X509EncodedKeySpec(pub_e));   
  44.     Key pri = kf.generatePrivate(new PKCS8EncodedKeySpec(pri_e));   
  45.     /* encrypt */  
  46.     Cipher c = Cipher.getInstance("RSA/ECB/PKCS#1");// provider="CryptixCrypto"   
  47.     c.init(Cipher.ENCRYPT_MODE, pub);   
  48.     o = new FileOutputStream("secret");   
  49.     o.write(c.doFinal(plain));   
  50.     o.close();   
  51.     /* decrypt */  
  52.     r.exec("openssl pkcs8 -inform DER -in pri -nocrypt -out pri.pem -outform PEM").waitFor();   
  53.     r.exec("openssl rsautl -inkey pri.pem -in secret -decrypt -out plain").waitFor();   
  54.     /* display */  
  55.     i = new FileInputStream("plain");   
  56.     int d;   
  57.     while ((d = i.read()) >= 0) {   
  58.       System.out.write(d);   
  59.     }   
  60.   }   
  61. }  

번호 제목 글쓴이 날짜 조회 수
공지 [계속 추가중] 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
» Java에서 암호화하고 C++에서 복호화하는 방법 황제낙엽 2007.09.27 2588
8 비밀키를 Keytool에서 취급할 수 있는 형식으로 변환방법 황제낙엽 2007.09.27 2802