当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java对象序列化加密解密
发布时间:2010/7/11 13:31:36 来源:城市学习网 编辑:ziteng
  示例只是简单说明序列化反序列化过程中属性值是可以改变的
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  public class Person implements Serializable {
  private String firstName;
  private String lastName;
  private int age;
  private Person spouse;
  public Person(String fn, String ln, int a) {
  this.firstName = fn;
  this.lastName = ln;
  this.age = a;
  }
  public String getFirstName() {
  return firstName;
  }
  public String getLastName() {
  return lastName;
  }
  public int getAge() {
  return age;
  }[NextPage]   public Person getSpouse() {
  return spouse;
  }
  public void setFirstName(String value) {
  firstName = value;
  }
  public void setLastName(String value) {
  lastName = value;
  }
  public void setAge(int value) {
  age = value;
  }
  public void setSpouse(Person value) {
  spouse = value;
  }
  private void writeObject(ObjectOutputStream stream) throws IOException {
  age = age << 2;   //序列化的时候加密
  stream.defaultWriteObject();
  }
  private void readObject(ObjectInputStream stream) throws IOException,
  ClassNotFoundException {
  stream.defaultReadObject(); //反序列化的时候解密
  age = age >> 2;
  }
  public String toString() {
  return "[Person:firstName=" + firstName + "lastName=" + lastName
  + "age=" + age + "spouse="
  + (spouse != null ? spouse.getFirstName() : "[null]") + "]";
  }
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved