Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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读取所有类型的extesion以进行加密/解密_Java_Encryption_Cryptography - Fatal编程技术网

使用Java读取所有类型的extesion以进行加密/解密

使用Java读取所有类型的extesion以进行加密/解密,java,encryption,cryptography,Java,Encryption,Cryptography,我正在开发一个程序,允许加载任何扩展名的文件,并使用一些算法对其进行加密 我已经完成了加密部分,我正在使用javaFile类来加载文件 但问题是,我无法使用file对象加载PDF文件 这是我加载文件的代码 /** * Read bytes from a File into a byte[]. * * @param file The File to read. * @return A byte[] containing the contents of

我正在开发一个程序,允许加载任何扩展名的文件,并使用一些算法对其进行加密

我已经完成了加密部分,我正在使用java
File
类来加载文件

但问题是,我无法使用
file
对象加载PDF文件

这是我加载文件的代码

 /** 
     * Read bytes from a File into a byte[].
     * 
     * @param file The File to read.
     * @return A byte[] containing the contents of the File.
     * @throws IOException Thrown if the File is too long to read or couldn't be
     * read fully.
     */
    @SuppressWarnings("resource")
    public static byte[] readBytesFromFile(File file) throws IOException {
      InputStream is = new FileInputStream(file);

      // Get the size of the file
      long length = file.length();

      // You cannot create an array using a long type.
      // It needs to be an int type.
      // Before converting to an int type, check
      // to ensure that file is not larger than Integer.MAX_VALUE.
      if (length > Integer.MAX_VALUE) {
        throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")");
      }

      // Create the byte array to hold the data
      byte[] bytes = new byte[(int)length];

      // Read in the bytes
      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
          offset += numRead;
      }

      // Ensure all the bytes have been read in
      if (offset < bytes.length) {
          throw new IOException("Could not completely read file " + file.getName());
      }

      // Close the input stream and return bytes
      is.close();
      return bytes;
  }
当我加载文本文件并打印它时,我得到十六进制输出,当我将十六进制输出转换为ASCII字符时,我得到文本文件的实际数据,但在
PDF
文件的情况下不会发生这种情况

这里值得一提的是,在加载数据之后,我与数据无关,我所需要做的就是对其进行加密,当我解密它时,我应该能够获得实际的数据。 这只适用于文本文件,而不适用于任何其他扩展,如apk、pdf、doc等

我在Play Store中看到过一个应用程序,它能够输入任何文件,然后使用AES-256对其进行加密并保存,然后当我们解密它时,我们会得到原始文件

有谁能给我提供一些线索,告诉我如何加密所有类型的文件,而不管它们的扩展名是什么

下面是我用来先将pdf文件加载为字节,然后将其另存为另一个pdf文件的测试代码

public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException {
      BufferedOutputStream bos = null;

    try {
      FileOutputStream fos = new FileOutputStream(theFile);
      bos = new BufferedOutputStream(fos); 
    }finally {
      if(bos != null) {
        try  {
          //flush and close the BufferedOutputStream
          bos.flush();
          bos.close();
        } catch(Exception e){}
      }
    }
    }
写作

File inputfile = new File("F:/perfect-secrecy.pdf");
        File outputfile = new File("F:/p.pdf");


    LoadFile.writeBytesToFile(output, LoadFile.readBytesFromFile(inputfile));

这里的生成输出文件是0kb,我不知道为什么。

您没有向
bos
写入任何内容。更不用说空的catch块是永远不知道出了什么问题的好方法。你可以读/写二进制(或者真正的字节/八位字节)。十六进制是一种表示二进制数据的方法,它不是十六进制数据的值。因此,您通常不会读/写十六进制,这就是字节的显示方式。
File inputfile = new File("F:/perfect-secrecy.pdf");
        File outputfile = new File("F:/p.pdf");


    LoadFile.writeBytesToFile(output, LoadFile.readBytesFromFile(inputfile));