【问题标题】:Java File Encryption AESJava 文件加密 AES
【发布时间】:2016-02-08 21:31:23
【问题描述】:

问题:上传方法有效我提出的问题是**encrypt(inputFile, new File("Users/myname/Desktop/example.txt"),key);**,而不是写入我的桌面我希望将其存储为我可以使用的文件或变量,以便我可以上传到保管箱服务器。我只是不知道如何编写这部分代码,也不知道该放什么,这样才能正确执行。

    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(File in, File output, byte[] key) throws Exception {

            Cipher cipher = getCipherEncrypt(key);
            FileOutputStream fos = null;
            CipherOutputStream cos = null;
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(in);
                fos = new FileOutputStream(output);
                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();
                System.out.println("name"+ output);


            } finally {
                System.out.println("performed encrypt method now closing streams");
                cos.close();
                fos.close();
                fis.close();
            }
        }




    // Working Upload Method To DropBox Cloud
    public void uploadFile () throws DbxException, IOException, FileLoadException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidParameterSpecException, ClassNotFoundException, NoSuchProviderException, InvalidAlgorithmParameterException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException, ShortBufferException, Exception {

    // autheticate if there is a accessToken here if not prompt to login by activating the drop method re-auth..
    try{
      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
    }

    // user picks file to upload with JFileChooser
     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);


    /* calling out inputFIle from out JFileChooser and now passing it into
        our method and encrytping and outputing it.
        */


    encrypt(inputFile, new  File("Users/myname/Desktop/example.txt"),key);



    File selected = new File ("?");
    inputStream = new FileInputStream (selected);
    uploadedFile = client.uploadFile( "/" +selected ,DbxWriteMode.add(), inputFile.length(), inputStream);

    /*
Original works without encryption 
    File selectedFile = new File(nameOf+inputFile);
    inputStream = new FileInputStream(inputFile);

    */


    // Original  uploadedFile = client.uploadFile( "/" +selectedFile ,DbxWriteMode.add(), inputFile.length(), inputStream);



    setTitle("Uploading File..");
    System.out.println("Uploaded: " + uploadedFile.toString());
    JOptionPane.showMessageDialog(null,"File Upload:" + uploadedFile.toString(),
            "Success!", JOptionPane.WARNING_MESSAGE);
    }
    }

【问题讨论】:

  • 您的问题似乎是“如何将文件上传到保管箱?”,但您的标题与该问题无关,标签也没有帮助。这真的是你的问题吗?如果是这样,那么请删除对加密的不必要提及,因为您的问题似乎与加密无关。
  • 同意。直接将流写入 Dropbox 可能会很有趣,但移动预先生成的文件与加密无关。
  • 方法uploadFile会自行完成上传任务。我的问题是;如何将 ("users/myname/Desktop/exampme.txt") 替换为可用于上传的 var 或文件?加密方法本身工作正常,我只是不希望它写入我的桌面我希望它存储为文件或在这种情况下我可以用来上传等的东西,

标签: java file cryptography aes


【解决方案1】:

解决了...只是把一些不需要再复杂化的东西复杂化了,而是选择了一种更简单的方法。

ORIGINAL:    encrypt(inputFile, new  File("Users/myname/Desktop/example.txt"),key);


   WORKING:    encrypt(inputFile, new  File(inputFile.getName),key);

Eazy Peezy...如果有人有任何其他建议,我想听听他们,谢谢。

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2020-01-20
    • 2011-06-05
    • 1970-01-01
    • 2020-04-01
    • 2012-03-28
    • 2011-08-03
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多