JAVA RSA 키 발급(기본)

기본 RSA 키 발급 코드

클래스로

– KeyGenTest

– 메인 어플리케이션(인쇄 실행용)

import java.security.*;

@Getter
public class KeyGenTest {

    private final PrivateKey privateKey;
    private final PublicKey publicKey;

    public KeyGenTest() throws NoSuchAlgorithmException {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.generateKeyPair();

        this.privateKey = kp.getPrivate();
        this.publicKey =  kp.getPublic();
    }
}

1. KeyPairGenerator를 가져오고 RSA 알고리즘 사용을 지정합니다.

2. KeyPairGenerator에 대한 initialize()로 키 크기를 결정합니다. (가능하면 ..1024, 2048 이상 사용 권장)

3. generateKeyPair를 통해 KeyPair에서 privateKey 및 publicKey 수신

4. getPrivate() 및 getPublic()으로 가져올 수 있습니다.

@SpringBootApplication
public class SpringPracticeApplication {

    public static void main(String() args) throws NoSuchAlgorithmException {

        KeyGenTest keyGen = new KeyGenTest();
        System.out.println(keyGen.getPrivateKey());
        System.out.println(keyGen.getPublicKey());
        SpringApplication.run(SpringPracticeApplication.class, args);
    }

}

위에서 생성한 클래스를 생성하여 getter를 통해 출력합니다.

SunRsaSign RSA private CRT key, 2048 bits
  params: null
  modulus: 17931226813357465914662673339345013703445698367640038387553815604888637963814399948671949026661360725059715390818671097849355906906276997613145951774569985594152218411632051624366230006524797395605635028135083209444838735618577854839090168429278166140062207835559187024270496467525902784883531697977508726393005174085298962611242312005798372771505569278107255753083313619083074422189992623751519552876799992428586857415549332204291694412509737343395041225683751259834100092050140570392675207168099151100824613647535583019887488124868180517733563637732897052639718396456541755584782220891345150241833721583571408208787
  private exponent: 5287135922323567928573805630552253609493639856207578952364159676959092421269656295041529868489618607062482875508186219919144201673205924926017858188378937113712825863044963388456185492867924758116534038538266138811847715444609891617110615602291396958825733619395848605474816878076056966524091223762414798187485782020366546127671164869932727451122864007819204944283322097627352910862090639217520088369861274195944720678465392230517420772439566669798482055053135078076758943912364334146812517696109362637990769624559547835427354813319676331453679035339641057501219392860179502649935334419321316524856755580040432624993


Sun RSA public key, 2048 bits
  params: null
  modulus: 17931226813357465914662673339345013703445698367640038387553815604888637963814399948671949026661360725059715390818671097849355906906276997613145951774569985594152218411632051624366230006524797395605635028135083209444838735618577854839090168429278166140062207835559187024270496467525902784883531697977508726393005174085298962611242312005798372771505569278107255753083313619083074422189992623751519552876799992428586857415549332204291694412509737343395041225683751259834100092050140570392675207168099151100824613647535583019887488124868180517733563637732897052639718396456541755584782220891345150241833721583571408208787
  public exponent: 65537

이러한 방식으로 개인 CRT 키와 공개 키(+ 2048비트)를 발급할 수 있습니다.