definition
Every class inherits from the Object class. The writeObject and readObject functions can be serialized and deserialized. If rewritten, the data can be encrypted.
Example
package JavaNote_103;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.*;
import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class JavaNote_114_mySerializable implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
private boolean male;
private static final String ALGORITHM = "AES";
public static byte[] encryptInt(int value, SecretKey encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
byte[] encryptedBytes = cipher.doFinal(ByteBuffer.allocate(4).putInt(value).array());
return encryptedBytes;
}
public static int decryptInt(byte[] encryptedBytes, SecretKey encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, encryptionKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return ByteBuffer.wrap(decryptedBytes).getInt();
}
public void print(){
System.out.println(name);
System.out.println(age);
System.out.println(male);
}
// 自定义的 writeObject 方法,在序列化对象时调用
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject(); // 默认序列化其他字段
// 加密 age 字段
byte[] encryptedAge = encryptInt(age);
out.writeObject(encryptedAge);
}
// 自定义的 readObject 方法,在反序列化对象时调用
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject(); // 默认反序列化其他字段
// 解密 age 字段
byte[] encryptedAge = (byte[]) in.readObject();
this.age = decryptInt(encryptedAge);
}
// 加密 int 数据
private byte[] encryptInt(int value) {
try {
String password = "encryption_password";
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec parameterSpec = new PBEParameterSpec("saltsalt".getBytes(), 100);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] valueBytes = Integer.toString(value).getBytes();
return cipher.doFinal(valueBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 解密 int 数据
private int decryptInt(byte[] encryptedValue) {
try {
String password = "encryption_password";
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec parameterSpec = new PBEParameterSpec("saltsalt".getBytes(), 100);
cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
byte[] decryptedBytes = cipher.doFinal(encryptedValue);
return Integer.parseInt(new String(decryptedBytes));
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
File f = new File("d:/temp/JavaNote_114_mySerializable.bin");
if(f.exists()){
System.out.println("Read from file");
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new FileInputStream(f));
JavaNote_114_mySerializable a = (JavaNote_114_mySerializable) ois.readObject();
a.print();
}finally {
ois.close();
System.out.println("close");
}
}
JavaNote_114_mySerializable o1 = new JavaNote_114_mySerializable();
o1.name = "Xiqing Hu";
o1.age = 38;
o1.male = true;
System.out.println("Write to file");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(o1);
}finally {
oos.close();
System.out.println("close");
}
System.out.println("done");
}
}