对象的序列化----对对象数据加密的实现(1)

来源:互联网 发布:淘宝最大的零食店 编辑:程序博客网 时间:2024/05/02 02:35

package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class user implements Serializable{
 private static int count;
 private String name;
 private transient String password;
 
 public user(String name,String password){
  this.name = name;
  this.password = password;
 }
 //数据加密函数
 private byte[] change(byte[] buff){
  for(int i=0; i<buff.length;i++){
   int b = 0;
   for(int j=0; j<8;j++){
    int bit = (buff[i] >> j & 1) == 1?0:1;
    b += (1<<j)*bit;
   }
   buff[i] = (byte)b;
  }
  return buff;
 }
 
 private void writeObject(ObjectOutputStream stream)throws
              IOException{
  stream.defaultWriteObject();
  stream.writeObject(change(password.getBytes()));
  stream.writeInt(count);
 }
 
 private void readObject(ObjectInputStream stream)throws
         IOException,ClassNotFoundException{
  stream.defaultReadObject();
  byte[] buff = (byte[])stream.readObject();
  password = new String(change(buff));
  count = stream.readInt();
 }
 
 public String toString(){
  return name + "    " +password;
 }
}
 

原创粉丝点击