/* * [MakeKeys.java] * * Summary: Create a public and private RSA encryption key pair. * * Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.4 2006-03-05 reformat with IntelliJ and add Javadoc. */ package com.mindprod.transporter; import java.io.FileOutputStream; import java.io.IOException; import static java.lang.System.*; /** * Create a public and private RSA encryption key pair. * * @author Roedy Green, Canadian Mind Products * @version 1.4 2006-03-05 reformat with IntelliJ and add Javadoc. * @since 2004-06-08 */ public final class MakeKeys { /** * invoke with: java.exe com.mindprod.transporter.MakeKeys name 512 results to files nameprivatekey.ser and * namepublickey.ser Generated keys must be tested before deployment. If they don't work, regenerate the keys. * Output goes to files current directory namepublickey.ser and nameprivatekey.ser These files are binary serialised * objects, not human-readable. * * @param args first arg is name for the key pair, e.g. jimmy. second arg is keysize 256 .. 4096. Larger values * offer more security but are much slower to generate keys and encrypt/decrypt. 4096 will take almost * forever. */ public static void main( String[] args ) { if ( args.length != 2 ) { err.println( "You must specify a name for the key, and keysize in bits on the command line, e.g. jimmy 1024" ); System.exit( 1 ); } String keyname = args[ 0 ]; // how many bits long the key is. int keySize = Integer.parseInt( args[ 1 ] ); if ( keySize < Tools.SMALLEST_KEYSIZE ) { err.println( "KeySize " + keySize + " too small." ); keySize = Tools.SMALLEST_KEYSIZE; } else if ( keySize > Tools.BIGGEST_KEYSIZE ) { err.println( "KeySize " + keySize + " too big." ); keySize = Tools.BIGGEST_KEYSIZE; } if ( keySize % 8 != 0 ) { err.println( "KeySize " + keySize + " not a multiple of 8." ); keySize = keySize / 8 * 8; } int blockSize = keySize / 8; out.println( "generating keys for: " + keyname + "publickey.ser and " + keyname + "privatekey.ser\n" + keySize + " bits. blockSize: " + blockSize + " bytes." ); out.println( "Please be patient. You only need to do this once." ); long start = System.currentTimeMillis(); PrivateKey p = new PrivateKey( keySize ); long stop = System.currentTimeMillis(); out.println( "elapsed " + ( stop - start ) + " milliseconds" ); try { p .savePrivateKey( new FileOutputStream( keyname + "privatekey.ser" ) ); /* PrivateKey also contains the public key. */ p.savePublicKey( new FileOutputStream( keyname + "publickey.ser" ) ); out.println( "Make backups of the generated keys. If you lose them there is no way" ); out.println( "to read your encryted information. " ); } catch ( IOException e ) { err.println( "key generation failed" ); System.exit( 1 ); } } }