First, use the keytool to generate the private key Library:
Keytool-genkey-alias privatekey-keystore privatekeys. Store
Then, export the public key in the private key library to a file:
Keytool-export-alias privatekey-file certfile. cer-keystore privatekeys. Store
Then import the Certificate file to the public key database:
Keytool-import-alias publiccert-file certfile. cer-keystore publiccerts. Store
Import de. schlichtherle. license. licensemanager; import de. schlichtherle. license. licenseparam;/*** licensemanager container class * @ author song Sany */public class internal {Private Static licensemanager; public static synchronized licensemanager getlicensemanager (licenseparam licenseparams) {If (licensemanager = NULL) {licensemanager = new licensemanager (licenseparams);} return licensemanager ;}}
Import Java. io. file; import Java. util. date; import Java. util. prefs. preferences; import javax. security. auth. x500.x500principal; import de. schlichtherle. license. cipherparam; import de. schlichtherle. license. defaultcipherparam; import de. schlichtherle. license. defaultkeystoreparam; import de. schlichtherle. license. defaultlicenseparam; import de. schlichtherle. license. keystoreparam; import de. schlichtherle. license. Licensecontent; import de. schlichtherle. license. licenseparam; import de. schlichtherle. license. licensemanager; public class testtruelicense {public final static string privatealias = "privatekey"; public final static string publicalias = "publiccert"; public final static string keypwd = "111aaa "; public final static string storepwd = "111aaa"; public final static string subject = "truelicense test"; // for ease of use The example in the API // x500princal is an inherent format of the Certificate file, for details, see apipublic final static x500principal defaultholderandissuer = new x500principal ("cn = Duke, ou = foreign oft, O = Sun Microsystems, c = us"); public static void main (string [] ARGs) throws exception ****************** /licensemanager = licensemanagerholder. getlicensemanager (initlicenseparams0 (); string storepath = testtruelicense. class. Getresource (". "). getFile () + file. separator + "test. LIC "; licensemanager. store (createlicensecontent (), new file (storepath); system. out. println ("certificate generated on the server side! "); ******************/// Because a licensemanager is used, you need to reset the licenseparam licensemanager for verification. setlicenseparam (initlicenseparams1 (); // install the certificate licensemanager. install (new file (storepath); system. out. println ("the client has successfully installed the certificate! "); // Verify the certificate licensemanager. Verify (); system. Out. println (" the client has successfully verified the certificate! ");} // Return the Private Static licenseparam initlicenseparams0 () {preferences preference = preferences. usernodeforpackage (testtruelicense. class); // set the symmetric password for certificate content encryption cipherparam = new defaultcipherparam ("111aaa"); // The class from which the parameter 1 and 2 are located. getresource () obtains the keystore alias // parameter 4 keystore storage password // parameter 5 keystore password keystoreparam privatestoreparam = new defaultkeystoreparam (testtruelicense. class, "privatekeys. store ", privatealias, storepwd, keypwd); licenseparam licenseparams = new defaultlicenseparam (subject, preference, privatestoreparam, cipherparam); Return licenseparams ;} // return the Private Static licenseparam initlicenseparams1 () {preferences preference = preferences. usernodeforpackage (testtruelicense. class); cipherparam = new defaultcipherparam ("111aaa"); keystoreparam privatestoreparam = new defaultkeystoreparam (testtruelicense. class, "publiccerts. store ", publicalias, storepwd, null); licenseparam licenseparams = new defaultlicenseparam (subject, preference, privatestoreparam, cipherparam); Return licenseparams ;} // generally, you can obtain the certificate content from an external form. // for demonstration, the certificate content is written to public final static licensecontent createlicensecontent () {licensecontent content = NULL; content = new licensecontent (); content. setsubject (subject); content. setholder (defaultholderandissuer); content. setissuer (defaultholderandissuer); content. setissued (new date (); content. setnotbefore (new date (); content. setnotafter (new date (). gettime () + 1000); content. setconsumertype ("user"); content. setconsumeramount (1); content. setinfo ("descriptive information not empty"); // extended content. setextra (new object (); Return content ;}}