반응형

Download Apache Commons Codec 다운로드 :

commons.apache.org/proper/commons-codec/download_codec.cgi

 

라이브러리 폴더에 복사

 

RSA key 파일 생성 클래스 추가 

package com.rsatest.keypair;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;


//
// RSA Key 파일 생성
//
// Created by netcanis on 2019/04/29.
// Copyright © 2019 netcanis. All rights reserved.
//


public class GenKeys
{
	private KeyPairGenerator keyGen;
	private KeyPair pair;
	private PrivateKey privateKey;
	private PublicKey publicKey;

	// RSA 초기화 및 키 생성 
	public GenKeys(int keysize) throws NoSuchAlgorithmException, NoSuchProviderException {
		this.keyGen = KeyPairGenerator.getInstance("RSA");
		this.keyGen.initialize(keysize);
		this.pair = this.keyGen.generateKeyPair();
		this.publicKey = pair.getPublic();
		this.privateKey = pair.getPrivate();
	}

	public PublicKey getPublicKey() {
		return this.publicKey;
	}
	
	public PrivateKey getPrivateKey() {
		return this.privateKey;
	}
	
	public void writeToFile(String path, byte[] key) throws IOException {
		File f = new File(path);
		f.getParentFile().mkdirs();

		FileOutputStream fos = new FileOutputStream(f);
		fos.write(key);
		fos.flush();
		fos.close();
	}

	
	
	public static void main(String[] args) {
		GenKeys genKeys;
		try {
			// 1024bit key pair 생성  
			genKeys = new GenKeys(1024);
			
			System.out.println(genKeys.publicKey);
			System.out.println(genKeys.privateKey);
			
			// 바이너리 파일로 저장 
			genKeys.writeToFile("KeyPair/public.key", genKeys.getPublicKey().getEncoded());
			genKeys.writeToFile("KeyPair/private.key", genKeys.getPrivateKey().getEncoded());
			
		} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
			System.err.println(e.getMessage());
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
}

 

결과 로그

 

결과물 확인

 

2021/02/05 - [개발노트] - 용어 정리

2021/02/05 - [iOS/Objective-C] - ARC or Non-ARC Compile Flag 설정

2021/02/05 - [개발노트] - Korea Bank Codes

2021/02/05 - [OS/Mac OS X] - NVRAM / PRAM 재설정

2021/02/05 - [OS/Mac OS X] - Mac OS 재설치

2021/01/06 - [iOS/Swift] - String to CGFloat

2021/01/05 - [iOS/Tips] - SceneDelegate 포인터 구하기

2021/01/05 - [iOS/Tips] - 앱 호출 (URL scheme)

2020/12/24 - [개발노트] - 라이선스 종류

2020/12/24 - [OS/Mac OS X] - MacBook을 AP로 설정하는 방법

2020/12/18 - [Arduino] - Bit Rate, Baud Rate

2020/12/18 - [Arduino] - RS232 Serial 통신 불량체크

2020/12/17 - [프로그래밍/Java] - Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

2020/12/17 - [프로그래밍/Java] - RSA 암복호화

반응형

'개발 > Note' 카테고리의 다른 글

20진수 변환  (0) 2021.09.16
RSA 암복호화 테스트  (0) 2021.02.05
용어 정리  (0) 2021.02.05
Korea Bank Codes  (0) 2021.02.05
라이선스 종류  (0) 2020.12.24
블로그 이미지

SKY STORY

,