AES文件加密/解密

时间:2016-02-06 05:54:21

标签: java file encryption aes dropbox

目标:要使用JFileChooser选择文件,请将该文件传递给加密方法,加密文件获取加密文件,然后通过最终将文件上传到服务器。我有上传方法或更具体的特定代码行' uploadedFile = client.uploadFile(" /" + nameOf,DbxWriteMode.add(),inputFile.length(),他的);'

问题:正常上传工作正常,如果尝试加密文件时不涉及加密,则不会与" KEY"无法放入inputFile,服务器将不接受该文件,请参阅代码。

问题:如何将加密集成到此代码中,以使代码能够正确有效地结合我的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();
        }
    }

将这些人放在一起的想法

1 个答案:

答案 0 :(得分:0)

首先检查:将原始文件设为zip并上传

如果有效,则将加密文件转换为zip,然后通过dropbox发送