【问题标题】:encrypt and serialize by one --- deserialize and decrypt by another由一个加密和序列化 --- 由另一个反序列化和解密
【发布时间】:2017-11-11 14:04:11
【问题描述】:

我有可序列化的对象:

import java.io.Serializable;

public class ConfigObject implements Serializable{

private String url;
private String user;
private String pass;

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getPass() {
    return pass;
}

public void setPass(String pass) {
    this.pass = pass;
}        
}

SerializableEncryptDecrypt 类中的 2 个方法:

public static void encrypt(Serializable object, OutputStream ostream, byte[] keyy, String transformationnn) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        try {
            // Length is 16 byte
            SecretKeySpec sks = new SecretKeySpec(keyy, transformationnn);

            // Create cipher
            Cipher cipher = Cipher.getInstance(transformationnn);
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            SealedObject sealedObject = new SealedObject(object, cipher);

            // Wrap the output stream
            CipherOutputStream cos = new CipherOutputStream(ostream, cipher);
            ObjectOutputStream outputStream = new ObjectOutputStream(cos);
            outputStream.writeObject(sealedObject);
            outputStream.close();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }

    public static Object decrypt(InputStream istream, byte[] keyy, String transformationnn) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        SecretKeySpec sks = new SecretKeySpec(keyy, transformationnn);
        Cipher cipher = Cipher.getInstance(transformationnn);
        cipher.init(Cipher.DECRYPT_MODE, sks);

        CipherInputStream cipherInputStream = new CipherInputStream(istream, cipher);
        ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
        SealedObject sealedObject;
        try {
            sealedObject = (SealedObject) inputStream.readObject();
            return sealedObject.getObject(cipher);
        } catch (ClassNotFoundException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
            return null;
        }
    }

我制作了 2 个使用此类 (SerializableEncryptDecrypt) 的软件(soft1 和 soft2)。该软件对输入数据(相同的输入数据)进行加密和序列化。当我将输出数据与我给出的输入进行比较时,是完全不同的数据。但我需要相同的输出数据。

提前感谢您的帮助。

【问题讨论】:

  • 请正确格式化您的代码。

标签: java encryption serializable


【解决方案1】:

使用一些盐(nonce、IV、...)进行随机加密是一种很好的做法,因此即使您使用相同的密钥使用相同的值进行加密,您也可能(并且应该)得到不同的输出。在某些情况下,相同的输出会降低安全性。

我不能确定,但​​我敢打赌“SealedObject”就是这样做的。如果你真的需要“相同的输出”,你可以直接序列化对象(不使用 SealedObject)。但是然后-您负责存储盐,身份验证标签等。

请注意 - 如果您在某些实际项目中使用此代码,则不应存储密码(即使是加密的),如果需要,仅存储它们的加盐加密哈希。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2012-09-29
    • 2017-12-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多