Package realcix20.cixfiles

Source Code of realcix20.cixfiles.KeyManager

/*
* KeyManager.java
*
* Created on 2006��11��20��, ����11:32
*
* RealCix2.0
*/

package realcix20.cixfiles;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.sql.ResultSet;

import realcix20.utils.DAO;
import realcix20.utils.Resources;

/**
*
* @author JerryChen
*/
public class KeyManager {
   
    public final static String KEY_GENERATE_ALGORITHM = "RSA";
    public final static String ENCRYPT_MODE_OR_DECRYPT_MODE ="RSA/ECB/PKCS1Padding";
       
    //generate keyPair & save the keyPair to database
    public static void generateKeyPair() {
       
        try {
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_GENERATE_ALGORITHM);
            keyGen.initialize(1024);
            KeyPair key = keyGen.generateKeyPair();
            DAO dao = DAO.getInstance();
            dao.update(Resources.GENERATE_KEY_SQL);
            dao.setObject(1, (Object)key.getPublic());
            dao.setObject(2, (Object)key.getPrivate());
            dao.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }              
       
    }
   
    public static void updateKeyPair(PublicKey publicKey, PrivateKey privateKey) {
       
        DAO dao = DAO.getInstance();
        dao.update(Resources.GENERATE_KEY_SQL);
        dao.setObject(1, (Object)publicKey);
        dao.setObject(2, (Object)privateKey);
        dao.executeUpdate();
       
    }      
   
    public static PublicKey getPublicKey() {    
       
        PublicKey key = null;
       
        DAO dao = DAO.getInstance();
        dao.query(Resources.SELECT_PUB_AND_PRI_KEY_SQL);
        ResultSet rs = dao.executeQuery();
        try {
           
            if (rs.next())
                key = (PublicKey)rs.getObject("PUBKEY");
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return key;
       
    }
   
    public static PrivateKey getPrivateKey() {
               
        PrivateKey key = null;
       
        DAO dao = DAO.getInstance();
        dao.query(Resources.SELECT_PUB_AND_PRI_KEY_SQL);
        ResultSet rs = dao.executeQuery();
        try {
           
            if (rs.next())
                key = (PrivateKey)rs.getObject("PRIKEY");
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return key;
       
    }
   
   
}
TOP

Related Classes of realcix20.cixfiles.KeyManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.