-
Notifications
You must be signed in to change notification settings - Fork 3
Usage
Additional to this Wiki entry, please consult also the project's JavaDoc.
liJense uses currently RSA keys with the size of 4096 bit. Neither the key algorithm nor the key size can currently be changed via the API.
Generating a new pair of private and public keys is quite easy. You can use KeyUtil.generateNewKeyPair() to do this. The currently used random generator to create the key pair is SHA1PRNG.
KeyPair keyPair = KeyUtil.generateNewKeyPair( );
PrivateKey privateKey = keyPair.getPrivate( );
PublicKey publicKey = keyPair.getPublic( );
If you want to generate the keys yourself (for instance, if you want to use an own random generator), you can do so and still use them with liJense, as long as the resulting keys are 4096 Bit RSA keys.
The KeyUtil class provides various methods to store and retrieve keys. In order to save a private or public key to a file, you can use KeyUtil.saveKeyToFile(Key,File).
KeyUtil.saveKeyToFile( privateKey, new File( "key.private" ) );
KeyUtil.saveKeyToFile( publicKey, new File( "key.public" ) );
If you want to store the keys in a database, you might want to consider to use the getEncoded() methods of the keys instead.
byte[] binaryPrivateKey = privateKey.getEncoded();
In order to load keys, liJense provides three methods for the private and the public key respectively. Keys can be loaded from a file, an input stream (useful if the public key is within your JAR file) or from an array containing binary data (useful if the key is loaded from a database blob).
PrivateKey privateKey = KeyUtil.loadPrivateKeyFromFile( new File( "key.private" ) );
PublicKey publicKey = KeyUtil.loadPublicKeyFromArray( binaryPublicKey );
Visit the JavaDocs for more detailed information on the methods.