Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将IvParameterSpec写入文件?_Java_Cryptography_Aes_Secret Key - Fatal编程技术网

Java 如何将IvParameterSpec写入文件?

Java 如何将IvParameterSpec写入文件?,java,cryptography,aes,secret-key,Java,Cryptography,Aes,Secret Key,我使用以下代码将SecretKey写入一个文件。同样,我必须将ivParameterSpec写入另一个文件。我该怎么做 SecretKey key = KeyGenerator.getInstance("AES").generateKey(); ObjectOutputStream secretkeyOS = new ObjectOutputStream(new FileOutputStream("publicKeyFile")); secretkeyOS.writeObject(key); s

我使用以下代码将SecretKey写入一个文件。同样,我必须将ivParameterSpec写入另一个文件。我该怎么做

SecretKey key = KeyGenerator.getInstance("AES").generateKey();
ObjectOutputStream secretkeyOS = new ObjectOutputStream(new FileOutputStream("publicKeyFile"));
secretkeyOS.writeObject(key);
secretkeyOS.close();

AlgorithmParameterSpec paramSpec1 = new IvParameterSpec(iv);
session.setAttribute("secParam", paramSpec1);
ObjectOutputStream paramOS = new ObjectOutputStream(new FileOutputStream("paramFile"));
paramOS.writeObject(paramSpec1);
paramOS.close();

不要尝试存储IvParameterSpec对象。它不可序列化,因为它不打算存储。 第四部分是本文的重要组成部分。存储这个并从IV创建一个新的IvSpec。我已经将用于AES加密的示例代码更改为存储IV,并使用加载的IV解密密文,以便您可以查看可能的工作流

请注意,这是一个最小的示例。在实际用例中,您还需要存储和加载密钥,并且还应该重新考虑异常处理:-D

public class Test {
    public static void main(String[] args) throws Exception {
        String message = "This string contains a secret message.";

        // generate a key
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        keygen.init(128);
        byte[] key = keygen.generateKey().getEncoded();
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

        byte[] iv = { 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        // initialize the cipher for encrypt mode
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);

        // encrypt the message
        byte[] encrypted = cipher.doFinal(message.getBytes());
        System.out.println("Ciphertext: " + hexEncode(encrypted) + "\n");

        // Write IV
        FileOutputStream fs = new FileOutputStream(new File("paramFile"));
        BufferedOutputStream bos = new BufferedOutputStream(fs);
        bos.write(iv);
        bos.close();

        // Read IV
        byte[] fileData = new byte[16];
        DataInputStream dis = null;

        dis = new DataInputStream(new FileInputStream(new File("paramFile")));
        dis.readFully(fileData);
        if (dis != null) {
            dis.close();
        }

        // reinitialize the cipher for decryption
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(fileData));

        // decrypt the message
        byte[] decrypted = cipher.doFinal(encrypted);
        System.out.println("Plaintext: " + new String(decrypted) + "\n");
    }

    [...]
}

iv是16位字节数组我想,这个答案写一个字节数组到一个文件应该对你有帮助。不,不可能。paramspec1不是字节数组为什么要保存对象?保存iv并在此基础上创建一个新规范(当需要解密时),或者iv是定义IvParameterSpec的部分。如果你储存静脉输液,然后再装一次。新的IvParameterSpec(loaded_iv)此新参数规范与另一个参数规范相同。您的代码不工作,因为IvParameterSpec不可序列化,因为它不打算存储