TravJav92 February 2016

Java File Encryption AES

PROBLEM: Upload method is working the problem I'm presenting is **encrypt(inputFile, new File("Users/myname/Desktop/example.txt"),key);** instead of writing to my desktop I want it to be stored as a file or var I can use so I can upload to dropbox server. I just do not know how to code this part and do not know what to put so this will execute appropriately.

    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.printl        

Answers


TravJav92 February 2016

Solved... all it was is simply over complicating something that didn't need to be complicated furthermore and have settled for a more simplicit way.

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


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

Eazy Peezy... if anyone has any other suggestions I would like to hear them, thanks.

Post Status

Asked in February 2016
Viewed 2,235 times
Voted 9
Answered 1 times

Search




Leave an answer