简体   繁体   中英

What I am getting when i am trying to print Public and private key generated for RSA in code below?

What I am getting when i am trying to print Public and private key generated for RSA in code below?

public void generateKeyPair()throws Exception{
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(1024);
    KeyPair key = keyGen.generateKeyPair(); 
    System.out.println("Private Key --> "+key.getPrivate());
    System.out.println("Public Key --> "+ key.getPublic());

}

Output:

Private Key --> sun.security.rsa.RSAPrivateCrtKeyImpl@3201e
Public Key --> Sun RSA public key, 1024 bits
modulus:     91070638360884489717846387624081331865380920530817450364127225655147956614794217905486935019170980669357616099533814798328666299188081771295145969332740783420682208946757921176081598083665454855067910689297215183406707874995244612816580868221470575486438389243678546960355939828269782848832295142018678264741
public exponent: 65537

For the public key , modulus and exponent are 2 components of the RSA public key, so this is interesting information expressed in a readable manner rather than base64 encoded (like it would be if you had the key in a file for instance). The 1024 bits part is the length of the key, which gives a sense of its strength.

For the private key , it doesn't make sense to let any information about the key leak in toString. This is just the plain default implementation of Object.toString() .

you can encode private key and public key to base64 so you can print it.

        import org.apache.commons.codec.binary.Base64;

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);
        KeyPair key = keyGen.generateKeyPair();
        byte[] privateKey = key.getPrivate().getEncoded();
        byte[] publicKey = key.getPublic().getEncoded();
        String encodedPrivateKey = Base64.encodeBase64String(publicKey);
        String encodedPublicKey = Base64.encodeBase64String(privateKey);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM