package com.oracle.test.serialize;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class PasswordFormat implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private transient String password;
public PasswordFormat(String name, String password) {
this.name = name;
this.password = password;
}
public PasswordFormat() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private void writeObject(ObjectOutputStream oos) throws IOException {//重写以下两个方法,完成序列化时password的加密和解密
oos.defaultWriteObject();
char a[]=this.password.toCharArray();
String str="";
for(int i=0;i<a.length;i++){
a[i]+=i;
str+=a[i];
}//各项加i再逆置;
StringBuilder sb = new StringBuilder(str);
oos.writeObject(sb.reverse().toString());
}
private void readObject(ObjectInputStream ois)
throws ClassNotFoundException, IOException {
ois.defaultReadObject();
String str = (String) ois.readObject();
StringBuilder sb = new StringBuilder(str);
sb.reverse();
char a[]=sb.toString().toCharArray();
str="";
for(int i=0;i<a.length;i++){
a[i]-=i;
str+=a[i];
}
this.password = str;
}
@Override
public String toString() {
return "PasswordFormat [name=" + name + ", password=" + password + "]";
}
public static void serializeTemp(PasswordFormat ob,File file){
try(
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
){
oos.writeObject(ob);
oos.flush();
System.out.println((PasswordFormat)ois.readObject());
}catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
PasswordFormat pf=new PasswordFormat("xcw","xcw12345678");
File file=new File("F://a//first.xiaowu");
PasswordFormat.serializeTemp(pf,file);
}
}
java 序列化serialize的小运用,密码加密存取
最新推荐文章于 2023-03-10 14:15:53 发布