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.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Vector;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
public class RealCIXFile implements java.io.Serializable {
private String type;//type: Partner / Correspondence
private PublicKey publicKey;
private byte[] wrappedKey;
private SealedObject encryedDatas;
private Vector notEncryedDatas;//used by Correspondence
//Constructor for Partner
/* datas struct:
* 1.PartnerRow
* 2.PartnerAccountRows
* 3.PublicKeys
* 4.Partner Banks
* 5.Partner Addresses
* 6.FamilyRow
* 7.mePartnerAccountRows NS='ME'
*/
public RealCIXFile(String type, PrivateKey privateKey, PublicKey publicKey, Vector datas) {
this.type = type;
this.publicKey = publicKey;
this.encryedDatas = encryDatas(datas, privateKey);
}
//Constuctor for Correspondence
/* notEncryedDatas struct:
* 1.URNS
* 2.URP
* 3.URPA
*
* datas struct:
* 1.CorrespondenceRow
*/
public RealCIXFile(String type, PrivateKey privateKey, Vector notEncryedDatas, Vector datas) {
this.type = type;
this.notEncryedDatas = notEncryedDatas;
this.encryedDatas = encryDatas(datas, privateKey);
}
public Vector getDatas(PublicKey publicKey) {
Vector datas = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, publicKey);
Key key = cipher.unwrap(wrappedKey, "DES", Cipher.SECRET_KEY);
datas = (Vector)encryedDatas.getObject(key);
} catch (Exception e) {
datas = null;
return datas;
}
return datas;
}
private SealedObject encryDatas(Vector datas, PrivateKey privateKey) {
SealedObject encryedDatas = null;
try {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keyGen.init(random);
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
encryedDatas = new SealedObject(datas, cipher);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, privateKey);
this.wrappedKey = cipher.wrap(key);
} catch (Exception e) {
e.printStackTrace();
}
return encryedDatas;
}
public File exportFile(String fileName) {
File file = null;
try {
file = new File("" + fileName + ".cix");
file.createNewFile();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(this);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
public static RealCIXFile importFile(File cixFile) {
RealCIXFile realCIXFile = null;
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream(cixFile));
realCIXFile = (RealCIXFile)is.readObject();
is.close();
} catch (Exception e) {
realCIXFile = null;
}
return realCIXFile;
}
public String getType() {
return type;
}
public PublicKey getPublicKey() {
return publicKey;
}
public Vector getNotEncryedDatas() {
return notEncryedDatas;
}
}