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

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

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 21567
공지 [계속 추가중] Keycloak 용어 및 설정 옵션 정의 황제낙엽 2024.02.02 20407
146 SPDX 2.3, 계속 써도 되나요? — 3.0으로 넘어갈 때를 판단하는 실무 기준 황제낙엽 2026.08.11 55
145 같은 OSS 지만 여러 버전을 등록해야 하는 경우 황제낙엽 2026.04.24 1972
144 [FOSSLight Dependency Scanner] Android Project 의 Dependency Scan 을 위한 Android SDK 설치와 설정 황제낙엽 2026.04.23 1843
143 FOSSLight Hub 의 REST API 이용 (v2) 황제낙엽 2026.03.19 2500
142 [FOSSLight/검토용] 개발그룹의 sbom-info.yaml 분석 및 피드백 절차 황제낙엽 2026.03.16 1983
141 [FOSSLight/관리용] Project Identification 진행에서 최종 SBOM SPDX 문서 추출 황제낙엽 2026.02.09 1917
140 [FOSSLight] 'Download Location' 컬럼 값에 'Different from DB' 라는 푸른색 메세지가 출력 file 황제낙엽 2026.02.06 3613
139 [FOSSLight] Project Identification 진행시의 신규 OSS 등록 처리 방법 황제낙엽 2026.02.06 3750
138 [FOSSLight Hub] License 정보를 직접 등록시 필요한 항목 목록 황제낙엽 2026.02.06 3579
137 [FOSSLight Hub] Open Source 정보를 직접 등록시 필요한 항목 목록 황제낙엽 2026.02.06 3352
136 [OSS] Microsoft.Web.WebView2 황제낙엽 2026.02.06 3352
135 [FOSSLight] fosslight 프로젝트의 'Pre-Review' > 'Open Source' 팝업창의 세 버튼에 대한 용도 황제낙엽 2026.02.06 3573
134 [FOSSLight/검토용] prechecker convert 결과 Hub 등록후 Identification 처리 file 황제낙엽 2026.02.05 3289
133 python venv 설치, fosslight source scanner , binary scanner 설치, 버전 확인 황제낙엽 2026.02.02 3508
132 SPDX 템플릿 다운로드 및 온라인 변환기 황제낙엽 2026.01.22 3870
131 SBOM(Software Bill of Materials) 및 소프트웨어 공급망 보안 관련 주요 정부 기관 및 단체 황제낙엽 2026.01.22 3606
130 [FOSSLight] online guide link 황제낙엽 2026.01.14 1844
129 identification 에서 출력하는 warning, error 메세지 황제낙엽 2025.12.29 1428
128 sbom-info.yaml 을 이용한 dependency scan excel 작성 file 황제낙엽 2025.12.29 3578
127 binary scan 결과물에 대해 취약점이 없는데 왜 SBOM에 넣어야 하나? 황제낙엽 2025.12.17 3484