Generate key which contains a pair of RSA private and public key using 1024 bytes. - Java Security

Java examples for Security:RSA

Description

Generate key which contains a pair of RSA private and public key using 1024 bytes.

Demo Code

/**/*from w  w w  .j av a2 s  .  c  o  m*/
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 * You may obtain a copy of the License at

 *  http://www.zhuangxulin.com/licenses/LICENSE-1.0
          
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

import sun.misc.BASE64Encoder;

public class Main {
    /**
     * String to hold name of the encryption algorithm.
     */
    public static final String ALGORITHM = "RSA";

    /**
     * Generate key which contains a pair of private and public key using 1024
     * bytes. Store the set of keys in Prvate.key and Public.key files.
     * 
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void generateKey(String PRIVATE_KEY_FILE,
            String PUBLIC_KEY_FILE) {
        try {
            final KeyPairGenerator keyGen = KeyPairGenerator
                    .getInstance(ALGORITHM);
            keyGen.initialize(1024);
            final KeyPair key = keyGen.generateKeyPair();

            File privateKeyFile = new File(PRIVATE_KEY_FILE);
            File publicKeyFile = new File(PUBLIC_KEY_FILE);

            // Create files to store public and private key
            if (privateKeyFile.getParentFile() != null) {
                privateKeyFile.getParentFile().mkdirs();
            }
            privateKeyFile.createNewFile();

            if (publicKeyFile.getParentFile() != null) {
                publicKeyFile.getParentFile().mkdirs();
            }
            publicKeyFile.createNewFile();
            String publicKey = (new BASE64Encoder()).encodeBuffer((key
                    .getPublic().getEncoded()));
            System.out.println("PUBLIC KEY:" + publicKey);
            // Saving the Public key in a file
            // FileOutputStream publicKeyOS = new
            // FileOutputStream(publicKeyFile);
            ObjectOutputStream publicKeyOS = new ObjectOutputStream(
                    new FileOutputStream(publicKeyFile));
            publicKeyOS.write(publicKey.getBytes());
            publicKeyOS.close();

            // Saving the Private key in a file
            String privateKey = (new BASE64Encoder()).encodeBuffer((key
                    .getPrivate().getEncoded()));
            System.out.println("PRIVATE KEY:" + privateKey);
            FileOutputStream privateKeyOS = new FileOutputStream(
                    privateKeyFile);
            privateKeyOS.write(privateKey.getBytes("utf-8"));
            privateKeyOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Related Tutorials