/*
* CIXFileData.java
*
* Created on 2006��11��22��, ����9:50
*
* RealCix2.0
*/
package realcix20.cixfiles;
import java.io.Serializable;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import realcix20.classes.basic.Row;
/**
*
* @author JerryChen
*/
public class CIXFileData implements Serializable {
private String type;
private SealedObject encryedData;
private CIXKey cixKey;
public CIXFileData(String type, PrivateKey privateKey, Row row) {
this.setType(type);
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);
encryedData = new SealedObject(row, cipher);
cixKey = new CIXKey();
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, privateKey);
getCixKey().wrappedKey = cipher.wrap(key);
} catch (Exception e) {
e.printStackTrace();
}
}
public Row getRow(PublicKey publicKey) {
Row row = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, publicKey);
Key key = cipher.unwrap(getCixKey().wrappedKey, "DES", Cipher.SECRET_KEY);
row = (Row)getEncryedData().getObject(key);
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public SealedObject getEncryedData() {
return encryedData;
}
public CIXKey getCixKey() {
return cixKey;
}
}