CodeNote_1.1.2_自定义序列化中,实现对int类型数据的加密和解密操作

定义

每一个类都继承自Object类。其中的writeObject和readObject函数可以进行序列化和反序列化,重写的话,可以对数据进行加密。

实例

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");

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值