sitelink1 | |
---|---|
sitelink2 | |
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 를 수행 가능하다.
클래스를 생성하여 콘솔에 출려하면 Public키와 Private키가 출력되고
이를 이용하여 encrypt, decrypt 를 수행 가능하다.
·미리보기 | 소스복사·
- import java.math.BigInteger;
- import java.security.SecureRandom;
- public class RSA {
- private final static BigInteger one = new BigInteger("1");
- private final static SecureRandom random = new SecureRandom();
- private BigInteger privateKey;
- private BigInteger publicKey;
- private BigInteger modulus;
- // generate an N-bit (roughly) public and private key
- RSA(int N) {
- BigInteger p = BigInteger.probablePrime(N/2, random);
- BigInteger q = BigInteger.probablePrime(N/2, random);
- BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));
- modulus = p.multiply(q);
- publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1
- privateKey = publicKey.modInverse(phi);
- }
- BigInteger encrypt(BigInteger message) {
- return message.modPow(publicKey, modulus);
- }
- BigInteger decrypt(BigInteger encrypted) {
- return encrypted.modPow(privateKey, modulus);
- }
- public String toString() {
- String s = "";
- s += "public = " + publicKey + "n";
- s += "private = " + privateKey + "n";
- s += "modulus = " + modulus;
- return s;
- }
- public static void main(String[] args) {
- int N = Integer.parseInt(args[0]);
- RSA key = new RSA(N);
- System.out.println(key);
- // create random message, encrypt and decrypt
- BigInteger message = new BigInteger(N-1, random);
- //// create message by converting string to integer
- // String s = "test";
- // byte[] bytes = s.getBytes();
- // BigInteger message = new BigInteger(s);
- BigInteger encrypt = key.encrypt(message);
- BigInteger decrypt = key.decrypt(encrypt);
- System.out.println("message = " + message);
- System.out.println("encrpyted = " + encrypt);
- System.out.println("decrypted = " + decrypt);
- }
- }
댓글 0
번호 | 제목 | sitelink1 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
공지 | [계속 추가중] SBOM 용어 정의 | 황제낙엽 | 2025.04.10 | 52 | |
공지 | [계속 추가중] Keycloak 용어 및 설정 옵션 정의 | 황제낙엽 | 2024.02.02 | 631 | |
13 | [Copilot] javax.crypto 패키지를 사용하여 암호화, 복호화 하는 방법 | 황제낙엽 | 2024.06.07 | 96 | |
12 | Apache Log4j 2 보안 업데이트 권고 | https://www.boho.or.kr/data/secNoticeVie...ence=36389 | 황제낙엽 | 2021.12.13 | 134 |
11 | OpenSSL사용방법 메모, RSA암호의 최대 사이즈, JCA/JCE가이드 | 황제낙엽 | 2007.09.27 | 173 | |
10 | Java Cryptography Extension (JCE) 개요 | 황제낙엽 | 2007.09.27 | 345 | |
9 | Java에서 암호화하고 C++에서 복호화하는 방법 | 황제낙엽 | 2007.09.27 | 378 | |
8 | 비밀키를 Keytool에서 취급할 수 있는 형식으로 변환방법 | http://java-house.jp/ml/archive/j-h-b/051472.html | 황제낙엽 | 2007.09.27 | 229 |
7 | 공개키 암호화의 수학적 알고리즘과 자바 구현 | 황제낙엽 | 2007.09.22 | 207 | |
6 | RSA 암호화 알고리즘을 구현한 자바예제 (산술계산) | 황제낙엽 | 2007.09.17 | 326 | |
» |
RSA 암호화 프로그램 예제 (BigInteger 이용)
![]() | 황제낙엽 | 2007.09.08 | 257 | |
4 | 해쉬를 이용한 패스워드 로그인 | 황제낙엽 | 2007.09.05 | 111 | |
3 |
Java 보안과 암호화 (개론)
![]() | 황제낙엽 | 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 |