Package realcix20.classes.plugins

Source Code of realcix20.classes.plugins.CixFile

package realcix20.classes.plugins;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.PublicKey;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;

import javax.crypto.Cipher;
import javax.crypto.SealedObject;

import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.ClassManager;
import realcix20.classes.basic.Row;
import realcix20.utils.DAO;
import realcix20.utils.ObjectUtil;
import realcix20.utils.Resources;

public class CixFile {
   
        //stat : 1, an invite; 2, confirm an invite; 3, identify with an invite
        private Integer stat;
        private Vector noEncryData;
        private PublicKey publicKey;
        private byte[] wrappedKey;
        private SealedObject encryData;

        public static CixFile importFile(File file) {
           
                CixFile cixFile = null;
                try {
                    ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
                    Integer stat = (Integer)is.readObject();
                    Vector noEncryData = (Vector)is.readObject();
                    PublicKey publicKey = (PublicKey)is.readObject();
                    byte[] wrappedKey = (byte[])is.readObject();
                    SealedObject encryData = (SealedObject)is.readObject();
                    cixFile = new CixFile(stat, noEncryData, publicKey, wrappedKey, encryData);
                    is.close();
                } catch (Exception e) {
                    return null;
                }
               
                return cixFile;
           
        }               
       
        public static File exportFile(CixFile cixFile, String fileName) {
           
                File file = null;
                try {
                    file = new File(fileName);
                    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
                    os.writeObject(cixFile.getStat());
                    os.writeObject(cixFile.getNoEncryData());
                    os.writeObject(cixFile.getPublicKey());
                    os.writeObject(cixFile.getWrappedKey());
                    os.writeObject(cixFile.getEncryData());
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
               
                return file;
           
        }
       
        public Vector decryData(PublicKey pubKey) {
           
                Vector decryData = null;
                try {
                    Cipher cipher = Cipher.getInstance("RSA");
                    cipher.init(Cipher.UNWRAP_MODE, pubKey);
                    Key key = cipher.unwrap(wrappedKey, "DES", Cipher.SECRET_KEY);
                    decryData = (Vector)encryData.getObject(key);
                } catch (Exception e) {
                    decryData = null;
                    e.printStackTrace();
                }
               
                return decryData;
           
        }
       
        public int analyCixFile() {
                           
                int result = -1;
           
                if (stat == 1) {//An invite
                    Vector decryData = decryData(publicKey);
                    result = receiveAnInvite(decryData);
                } else if (stat == 2) {//confirm invite
                    Vector decryData = decryData(publicKey);
                    result = confirmTheInvite(decryData);
                } else if (stat == 3) {//modified partner
                    Vector decryData = decryData(publicKey);
                } else if (stat == 4) {//correspondence transfer                       
                    result = transferCorrespondence();
                } else if (stat == 5) {//correspondence transfer confirm
                    result = confirmTransferCorrespondence(null);//will be modified
                }
               
                return result;
           
        }
       
        //501:success; 502:not found correspondence;       
        public int confirmTransferCorrespondence(Vector data) {
           
                int result = 501;
               
               
               
                return result;
           
        }
       
        //401:success; 402:not found pubKey
        public int transferCorrespondence()  {
           
                int result = 402;
               
                String ns = (String)noEncryData.get(0);
                String p = (String)noEncryData.get(1);
                String pa = (String)noEncryData.get(2);
               
                DAO dao = DAO.getInstance();
                dao.query(Resources.SELECT_PUBKEY_PRIKEY_FROM_PA_SQL);
                dao.setObject(1, ns);
                dao.setObject(2, p);
                dao.setObject(3, pa);
                ResultSet rs = dao.executeQuery();
                try {
                    if (rs.next()) {
                        publicKey = (PublicKey)rs.getObject("PUBKEY");
                        result = 401;
                    } else {
                        result = 402;
                    }
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
               
                return result;
           
        }
       
        //101:success;
        public int receiveAnInvite(Vector data) {
           
                int result = 101;
                               
                return result;
           
        }
       
        //201:success; 202:not found equal row;
        public int confirmTheInvite(Vector data) {
           
                int result = 202;
                Row partnerRow = (Row)data.get(0);
                Vector pubKeys = (Vector)data.get(1);
                Cell urnsCell = ObjectUtil.findNewCell(partnerRow, "P", "URNS");
                Cell urpCell = ObjectUtil.findNewCell(partnerRow, "P", "URP");
                String urns = (String)urnsCell.getColumnValue();
                String urp = (String)urpCell.getColumnValue();
                BaseClass object = ClassManager.createClass(250);
                Iterator rowsIter = object.getRows().iterator();
                while (rowsIter.hasNext()) {
                    Row row = (Row)rowsIter.next();
                    Cell nsCell = ObjectUtil.findNewCell(row, "P", "NS");
                    Cell pCell = ObjectUtil.findNewCell(row, "P", "P");
                    String ns = (String)nsCell.getColumnValue();
                    String p = (String)pCell.getColumnValue();
                    if ( (urns.equals(ns)) && (urp.equals(p)) ) {
                        result = 201;
                        break;
                    }
                }               
               
                return result;
           
        }
       
        public CixFile(Integer stat, Vector noEncryData, PublicKey publicKey, byte[] wrappedKey, SealedObject encryData) {

                this.stat = stat;
                this.noEncryData = noEncryData;
                this.publicKey = publicKey;
                this.wrappedKey = wrappedKey;
                this.encryData = encryData;

        }
       
        public SealedObject getEncryData() {
                return encryData;
        }

        public PublicKey getPublicKey() {
                return publicKey;
        }

        public Integer getStat() {
                return stat;
        }

        public Vector getNoEncryData() {
                return noEncryData;
        }

        public byte[] getWrappedKey() {
                return wrappedKey;
        }
   
}
TOP

Related Classes of realcix20.classes.plugins.CixFile

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.