Java AES文件加密/解密

Java AES文件加密/解密,java,file,encryption,aes,dropbox,Java,File,Encryption,Aes,Dropbox,目标:要使用JFileChooser拾取文件,请将该文件传递到加密方法,加密文件获取加密文件,然后通过将文件上载到服务器。我拥有的上载方法,或者更具体地说,是特定的代码行'uploadedFile=client.uploadFile(“/”+nameOf,DbxWriteMode.add(),inputFile.length(),his);' 问题:在尝试加密文件时,如果不进行加密,则正常上载可以正常工作。无法将“密钥”放入输入文件,服务器也不会接受该文件。请参阅代码 问题:如何将加密集成到其中

目标:要使用JFileChooser拾取文件,请将该文件传递到加密方法,加密文件获取加密文件,然后通过将文件上载到服务器。我拥有的上载方法,或者更具体地说,是特定的代码行'uploadedFile=client.uploadFile(“/”+nameOf,DbxWriteMode.add(),inputFile.length(),his);'

问题:在尝试加密文件时,如果不进行加密,则正常上载可以正常工作。无法将“密钥”放入输入文件,服务器也不会接受该文件。请参阅代码

问题:如何将加密集成到其中,使代码能够正确有效地结合我的uploadFile方法和我现有的加密方法

下面的工作代码没有加密

public void uploadFile() throws DbxException, FileLoadException, 

IOException, Exception {

    try{

        //auth method

        phoneHome();

    }catch(IOException e){

        System.out.println("not saving accessToken");

        JOptionPane.showMessageDialog(null, "Your Access Information Does 
Not Exist,\n Please Login"+
                "Please Login By Clicking 'OK'");
        drop(); // will run auth method for user to login
    }
    fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int dialog = fc.showSaveDialog(this);
    if (dialog == JFileChooser.APPROVE_OPTION) {
        inputFile = fc.getSelectedFile();
        inputFile.getName();
        inputFile.getAbsoluteFile();
        String nameOf = inputFile.getName();
        System.out.println(" File: " + inputFile);


        fis = new FileInputStream(inputFile);
// where the file is being sent to upload to dropbox
        uploadedFile = client.uploadFile( "/" + nameOf ,DbxWriteMode.add(), inputFile.length(),fis);
            }

    communication.append("");
    System.out.println("Uploaded: " + uploadedFile.toString());
    communication.append("Uploaded: " + uploadedFile.toString());


    JOptionPane.showMessageDialog(null,"File Upload:" + uploadedFile.toString(),
                   "Success!", JOptionPane.WARNING_MESSAGE);



        } 
我拥有的加密方法,需要在上面实现

private byte[] getKeyBytes(final byte[] key) throws Exception {
        byte[] keyBytes = new byte[16];
        System.arraycopy(key, 0, keyBytes, 0, Math.min(key.length, keyBytes.length));
        return keyBytes;
    }

    public Cipher getCipherEncrypt(final byte[] key) throws Exception {
        byte[] keyBytes = getKeyBytes(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        return cipher;
    }
public void encrypt() throws Exception {
        Cipher cipher = getCipherEncrypt(key);
        FileOutputStream fos = null;
        CipherOutputStream cos = null;
        FileInputStream fis = null;
        try {
           fis = new FileInputStream(inputFile);
         fos = new FileOutputStream(outputFile);
            cos = new CipherOutputStream(fos, cipher);
            byte[] data = new byte[1024];
            int read = fis.read(data);
            while (read != -1) {
                cos.write(data, 0, read);
                read = fis.read(data);
                System.out.println(new String(data, "UTF-8").trim());
            }
            cos.flush();
        } finally {
            cos.close();
            fos.close();
            fis.close();
        }
    }
把这些家伙放在一起的想法?

首先检查:将原始文件制作成zip并上传

如果有效,则将加密文件放入zip,然后通过dropbox发送。首先选中:将原始文件放入zip并上传


如果有效,请先将加密文件压缩成zip格式,然后通过dropbox发送

请先将代码格式化。有了所有这些空行,阅读起来就没有乐趣了。如果您要立即修复它,请先将代码格式化好。有这么多空行,读起来很不愉快。我会马上修复它,抱歉,不管我想上传什么文件扩展名,这与问题无关。他们使用JFileChooser选择文件,然后inputFile就是他们刚刚选择并上传到服务器的文件。我想做的是做同样的事情,但是在文件被拾取并传递给上传人之后,我会运行加密。我想上传的文件扩展名与问题无关。他们使用JFileChooser选择文件,然后inputFile就是他们刚刚选择并上传到服务器的文件。我正试图做的是做同样的事情,而是在文件被拾取并传递到上传后运行加密