Encrypting files with AES on Android

前端 未结 1 1334
死守一世寂寞
死守一世寂寞 2020-12-04 18:55

So I am working on a personal project for myself, and I am trying to encrypt files on my phone. These files can be anything i.e Documents, photos, etc. Right now I am trying

相关标签:
1条回答
  • 2020-12-04 19:17

    maybe I'm missing something but on my side it works without a problem. Can you try the following class simply changing the fileToBeCrypted, fileToBeDecrypted and fileDecryptedOutput variables?

    package test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.security.InvalidKeyException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.Arrays;
    
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.SecretKeySpec;
    
    public class TestCrypt{
    
        private static final String salt = "t784";
        private static final String cryptPassword = "873147cbn9x5'2 79'79314";
        private static final String fileToBeCrypted = "c:\\Temp\\sampleFile.conf";
        private static final String fileToBeDecrypted = "c:\\Temp\\sampleFile.conf.crypt";
        private static final String fileDecryptedOutput = "c:\\Temp\\sampleFile.conf.decrypted";
    
        public static void main(String[] args) throws Exception
        {
            for (int i=0; i<100; i++)
            {
                encryptfile(fileToBeCrypted, cryptPassword);
                decrypt(fileToBeDecrypted, cryptPassword, fileDecryptedOutput);
                System.out.println(i);
            }
        }
    
        public static void encryptfile(String path,String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
            FileInputStream fis = new FileInputStream(path);
            FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
            byte[] key = (salt + password).getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key,16);
            SecretKeySpec sks = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            int b;
            byte[] d = new byte[8];
            while((b = fis.read(d)) != -1) {
                cos.write(d, 0, b);
            }
            cos.flush();
            cos.close();
            fis.close();
        }
    
        public static void decrypt(String path,String password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
            FileInputStream fis = new FileInputStream(path);
            FileOutputStream fos = new FileOutputStream(outPath);
            byte[] key = (salt + password).getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key,16);
            SecretKeySpec sks = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, sks);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            int b;
            byte[] d = new byte[8];
            while((b = cis.read(d)) != -1) {
                fos.write(d, 0, b);
            }
            fos.flush();
            fos.close();
            cis.close();
        }
    
    }
    

    I can iterate many times without an error! I'm using Oracle JDK 1.8 but running in 1.7 compatibility mode.

    Hope this helps you out.

    Bye Piero

    0 讨论(0)
提交回复
热议问题