Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

cryptography - Java Retrieve the actual value of the public key from the KeyPair object

I wanted to ask how to retrieve the actual values of the private and public keys from the KeyPair object because i need to export them and save in a database.

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();

System.out.println("Public key " + keyPair.getPublic());
System.out.println("Private key " + keyPair.getPrivate());

The output is:

Public key Sun RSA public key, 1024 bits
  modulus: 105712092415375085805423498639048173422142354311030811647243014925610093650322108853068042919471115278002432342007597147610508132502035047888382465733153739247741208519707861808073276783311634229563965825609200080862631487160732889423591650215084096832366499080850540875321197564283324922935557797293830551071
  public exponent: 65537
Private key sun.security.rsa.RSAPrivateCrtKeyImpl@35e71
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply downcast to RSAPrivateKey and RSAPublicKey. Once you've done that you can access the methods of these classes.

RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic());
BigInteger modulus = pubKey.getModulus();

etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...