JAVA RSA 암호화 프로그램 예제 (BigInteger 이용)

황제낙엽 2007.09.08 01:26 조회 수 : 3191 추천:105

sitelink3 http://1 
sitelink4 http://ko 
extra_vars5 http://blog.naver.com/safinfo?Redirect=Log&logNo=10018946130 
extra_vars6 sitelink1 
자바의 BigInteger 클래스를 이용하여 RSA 로직을 구현하였다.
클래스를 생성하여 콘솔에 출려하면 Public키와 Private키가 출력되고
이를 이용하여 encrypt, decrypt 를 수행 가능하다.
·미리보기 | 소스복사·
  1. import java.math.BigInteger;   
  2. import java.security.SecureRandom;   
  3.        
  4.   
  5. public class RSA {   
  6.    private final static BigInteger one      = new BigInteger("1");   
  7.    private final static SecureRandom random = new SecureRandom();   
  8.   
  9.    private BigInteger privateKey;   
  10.    private BigInteger publicKey;   
  11.    private BigInteger modulus;   
  12.   
  13.    // generate an N-bit (roughly) public and private key   
  14.    RSA(int N) {   
  15.       BigInteger p = BigInteger.probablePrime(N/2, random);   
  16.       BigInteger q = BigInteger.probablePrime(N/2, random);   
  17.       BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));   
  18.   
  19.       modulus    = p.multiply(q);                                     
  20.       publicKey  = new BigInteger("65537");     // common value in practice = 2^16 + 1   
  21.       privateKey = publicKey.modInverse(phi);   
  22.    }   
  23.   
  24.   
  25.    BigInteger encrypt(BigInteger message) {   
  26.       return message.modPow(publicKey, modulus);   
  27.    }   
  28.   
  29.    BigInteger decrypt(BigInteger encrypted) {   
  30.       return encrypted.modPow(privateKey, modulus);   
  31.    }   
  32.   
  33.    public String toString() {   
  34.       String s = "";   
  35.       s += "public  = " + publicKey  + "n";   
  36.       s += "private = " + privateKey + "n";   
  37.       s += "modulus = " + modulus;   
  38.       return s;   
  39.    }   
  40.     
  41.    public static void main(String[] args) {   
  42.       int N = Integer.parseInt(args[0]);   
  43.       RSA key = new RSA(N);   
  44.       System.out.println(key);   
  45.     
  46.       // create random message, encrypt and decrypt   
  47.       BigInteger message = new BigInteger(N-1, random);   
  48.   
  49.       //// create message by converting string to integer   
  50.       // String s = "test";   
  51.       // byte[] bytes = s.getBytes();   
  52.       // BigInteger message = new BigInteger(s);   
  53.   
  54.       BigInteger encrypt = key.encrypt(message);   
  55.       BigInteger decrypt = key.decrypt(encrypt);   
  56.       System.out.println("message   = " + message);   
  57.       System.out.println("encrpyted = " + encrypt);   
  58.       System.out.println("decrypted = " + decrypt);   
  59.    }   
  60. }  
번호 제목 글쓴이 날짜 조회 수
공지 [계속 추가중] 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 1973
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 3580
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 3509
132 SPDX 템플릿 다운로드 및 온라인 변환기 황제낙엽 2026.01.22 3870
131 SBOM(Software Bill of Materials) 및 소프트웨어 공급망 보안 관련 주요 정부 기관 및 단체 황제낙엽 2026.01.22 3606
130 [FOSSLight] online guide link 황제낙엽 2026.01.14 1845
129 identification 에서 출력하는 warning, error 메세지 황제낙엽 2025.12.29 1428
128 sbom-info.yaml 을 이용한 dependency scan excel 작성 file 황제낙엽 2025.12.29 3579
127 binary scan 결과물에 대해 취약점이 없는데 왜 SBOM에 넣어야 하나? 황제낙엽 2025.12.17 3485