JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

JCA - Key Factory - Encoding Tool

Part:   1  2  3  

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

Here is the result of my first test. It is done with JDK 1.3.1.

javac -classpath . JcaKeyFactoryTest.java

java -cp . JcaKeyFactoryTest 512 dsa DSA

KeyPairGenerator Object Info:
Algorithm = DSA
Provider = SUN version 1.2
Key Size = 512
toString = sun.security.provider.DSAKeyPairGenerator@738798

Private Key Info:
Algorithm = DSA
Saved File = dsa.pri
Length = 202
Format = PKCS#8
toString = Sun DSA Private Key
parameters:
p:
fca682ce 8e12caba 26efccf7 110e526d b078b05e decbcd1e b4a208f3 ae1617ae
01f35b91 a47e6df6 3413c5e1 2ed0899b cd132acd 50d99151 bdc43ee7 37592e17
q:
962eddcc 369cba8e bb260ee6 b6a126d9 346e38c5
g:
678471b2 7a9cf44e e91a49c5 147db1a9 aaf244f0 5a434d64 86931d2d 14271b9e
35030b71 fd73da17 9069b32e 2935630e 1c206235 4d0da20a 6c416e50 be794ca4

x: 91a8c9462732fa91aa4cbc97ca7ae43c9bcac41b

Public Key Info:
Algorithm = DSA
Saved File = dsa.pub
Length = 244
Format = X.509
toString = Sun DSA Public Key
Parameters:
p:
fca682ce 8e12caba 26efccf7 110e526d b078b05e decbcd1e b4a208f3 ae1617ae
01f35b91 a47e6df6 3413c5e1 2ed0899b cd132acd 50d99151 bdc43ee7 37592e17
q:
962eddcc 369cba8e bb260ee6 b6a126d9 346e38c5
g:
678471b2 7a9cf44e e91a49c5 147db1a9 aaf244f0 5a434d64 86931d2d 14271b9e
35030b71 fd73da17 9069b32e 2935630e 1c206235 4d0da20a 6c416e50 be794ca4

y:
8f799b3a eead766a 163c603d e1008521 4c946a94 98abb83e e5cbd6c0 1c38a0a6
3a23dac4 69065303 22c6fde7 10d3b567 b4c0b89f df6971e7 30178015 2a23d77f

KeyFactory Object Info:
Algorithm = DSA
Provider = SUN version 1.2
toString = java.security.KeyFactory@26e431

Private Key Info:
Algorithm = DSA
Saved File = dsa.pri
Length = 202
toString = Sun DSA Private Key
parameters:DSA
p:    
fca682ce 8e12caba 26efccf7 110e526d b078b05e decbcd1e b4a208f3 ae1617ae
01f35b91 a47e6df6 3413c5e1 2ed0899b cd132acd 50d99151 bdc43ee7 37592e17
q:     
962eddcc 369cba8e bb260ee6 b6a126d9 346e38c5
g:     
678471b2 7a9cf44e e91a49c5 147db1a9 aaf244f0 5a434d64 86931d2d 14271b9e
35030b71 fd73da17 9069b32e 2935630e 1c206235 4d0da20a 6c416e50 be794ca4

x: 91a8c9462732fa91aa4cbc97ca7ae43c9bcac41b

Public Key Info:
Algorithm = DSA
Saved File = dsa.pub
Length = 244
toString = Sun DSA Public Key
Parameters:DSA
p:     
fca682ce 8e12caba 26efccf7 110e526d b078b05e decbcd1e b4a208f3 ae1617ae
01f35b91 a47e6df6 3413c5e1 2ed0899b cd132acd 50d99151 bdc43ee7 37592e17
q:     
962eddcc 369cba8e bb260ee6 b6a126d9 346e38c5
g:     
678471b2 7a9cf44e e91a49c5 147db1a9 aaf244f0 5a434d64 86931d2d 14271b9e
35030b71 fd73da17 9069b32e 2935630e 1c206235 4d0da20a 6c416e50 be794ca4

y:
8f799b3a eead766a 163c603d e1008521 4c946a94 98abb83e e5cbd6c0 1c38a0a6
3a23dac4 69065303 22c6fde7 10d3b567 b4c0b89f df6971e7 30178015 2a23d77f

The program seems to be working:

  • A pair of keys is generated using the DSA algorithm first.
  • The private key is then stored in a file using the default encoding format.
  • The public key is also stored in a file using the default encoding format.
  • A KeyFactory object is then created with DSA algorithm using the default security package provider.
  • The private key is then read into the program and decoded into PrivateKey object with the help of PKCS8EncodedKeySpec class.
  • The public key is also read into the program and decoded into PublicKey object with the help of X509EncodedKeySpec class.

Here is the result of my second test with RSA algorithm.

java -cp . JcaKeyFactoryTest 512 rsa RSA

KeyPairGenerator Object Info:
Algorithm = RSA
Provider = SunRsaSign version 1.0
Key Size = 512
toString = java.security.KeyPairGenerator$Delegate@2f6684

Private Key Info:
Algorithm = RSA
Saved File = rsa.pri
Length = 343
Format = PKCS8
toString = com.sun.rsajca.JSA_RSAPrivateKey@372a1a

Public Key Info:
Algorithm = RSA
Saved File = rsa.pub
Length = 94
Format = X509
toString = com.sun.rsajca.JSA_RSAPublicKey@12d342

KeyFactory Object Info:
Algorithm = RSA
Provider = SunRsaSign version 1.0
toString = java.security.KeyFactory@5ff48b

Private Key Info:
Algorithm = RSA
Saved File = rsa.pri
Length = 343
toString = com.sun.rsajca.JSA_RSAPrivateKey@fe7c3

Public Key Info:
Algorithm = RSA
Saved File = rsa.pub
Length = 94
toString = com.sun.rsajca.JSA_RSAPublicKey@3e2d65

Part:   1  2  3  

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - JCA - Key Factory - Encoding Tool