JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

RSA Private Key and Public Key Pair Sample

This section provides a tutorial example on how to run JcaKeyPair.java to generate a RSA private key and public key pair sample. Keys are stored PKCS#8 and X.509 encoding formats.

Now let's see the private key and public key generated by the RSA algorithm:

java -cp . JcaKeyPair 512 rsa rsa

KeyPairGenerator Object Info:
Algorithm = rsa
Provider = SunRsaSign version 1.0
Key Size = 512
toString = java.security.KeyPairGenerator$Delegate@7172ea

Private Key Info:
Algorithm = RSA
Saved File = rsa.pri
Size = 346
Format = PKCS8
toString = com.sun.rsajca.JSA_RSAPrivateKey@45a877

Public Key Info:
Algorithm = RSA
Saved File = rsa.pub
Size = 94
Format = X509
toString = com.sun.rsajca.JSA_RSAPublicKey@2f0db

java -cp . HexWriter rsa.pri rsa_pri.hex

type rsa_pri.hex
30820156020100300D06092A864886F7
0D0101010500048201403082013C0201
00024100D1565D698FA30419CED84049
7E346F00F3F451D6C43315850B13C639
AE25EEE009FD9A574251127A2F9A14A0
47ADBB2DC39253D2858E8F4A0BC6DDF7
D09DEE7F020301000102410094CAA43E
C1BF86CCA93F0002BCD79C06BB5F6F44
0DD4997925347AF2C89B61BB470DB3D6
3B53B03AF3F8D0B09F1451974E0D7283
C2A742956B18CC4F53AE8541022100F4
52141F336B9FB350523381E0F65D437B
52C15F39CE8A32C4FC41B47527870F02
2100DB582BFDBE19628FA0E250C2DDCA
A72CBA1D62C35F62D4085FBD9C622216
A191022100DAAC0FE8711C759E87585B
5376A6EA88787B7429A7D28D6CAD545D
F68718EECF022068670D79577489BF47
9AB14A2F7B781B07F358C119B3D6ACAD
B578AC1501A4A1022100C7BEBD09E4D6
C020542841FF966FF3646CDB8F5C62E2
1E7E29E367C69DF70FD4

java -cp . HexWriter rsa.pub rsa_pub.hex

type rsa_pub.hex
305C300D06092A864886F70D01010105
00034B003048024100D1565D698FA304
19CED840497E346F00F3F451D6C43315
850B13C639AE25EEE009FD9A57425112
7A2F9A14A047ADBB2DC39253D2858E8F
4A0BC6DDF7D09DEE7F0203010001

Comparing the default implementation of DSA algorithm with RSA algorithm, the DSA implementation returns all the internal parameters of the keys in the toString() method.

Last update: 2006.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.3.1 on Windows

 Downloading and Installing JDK 1.4.1 on Windows

 Downloading and Installing JDK 1.5.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

Private key and Public Key Pair Generation

 Private and Public Keys and Related Interfaces

 KeyPair and KeyPairGenerator Classes

 Key Pair Sample Program - JcaKeyPair.java

 DSA Private Key and Public Key Pair Sample

RSA Private Key and Public Key Pair Sample

 DiffieHellman Private Key and Public Key Pair Sample

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
RSA Private Key and Public Key Pair Sample