Why is desktop AES file IO incompatible with Android AES file IO?

前端 未结 1 449
醉酒成梦
醉酒成梦 2021-02-10 16:16

I have ported an application from Android to desktop that uses AES to encrypt some private data. Both applications are able to encrypt and decrypt the data for their own use but

相关标签:
1条回答
  • 2021-02-10 17:06

    Solved by

    1. Adding proper constructors to initialize SecretKeySpec and IvParameterSpec.
    2. Getting rid of ObjectOutputStream and ObjectInputStream in desktop app.

    Android app:

    public class AesFileIo {
        private static final String EOL = "\n";
        private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
        private SecretKeySpec secretKeySpec;
        private IvParameterSpec ivSpec;
        private static final String PROVIDER = "BC"; 
    
        AesFileIo(byte[] aesKey, byte[] iv) {
            ivSpec = new IvParameterSpec(iv);
            secretKeySpec = new SecretKeySpec(aesKey, "AES");
        }
    
        public String readFile(Context c, String fileName) {
            StringBuilder stringBuilder = new StringBuilder();
            try {
                InputStream is = c.openFileInput(fileName);
                Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER);
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
                CipherInputStream cis = new CipherInputStream(is, cipher);
                InputStreamReader isr = new InputStreamReader(cis);
                BufferedReader reader = new BufferedReader(isr);
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line).append(EOL);
                }
                is.close();
            } catch (java.io.FileNotFoundException e) {
                // OK, file probably not created yet
                Log.i(this.getClass().toString(), e.getMessage(), e);
            } catch (Exception e) {
                Log.e(this.getClass().toString(), e.getMessage(), e);
            }
            return stringBuilder.toString();
        }
    
        public void writeFile(Context c, String fileName, String theFile) {
            try {
                Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER); 
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
                byte[] encrypted = cipher.doFinal(theFile.getBytes()); 
                OutputStream os = c.openFileOutput(fileName, 0);
                os.write(encrypted);
                os.flush();
                os.close();
            } catch (Exception e) {
                Log.e(this.getClass().toString(), e.getMessage(), e);
            }
        }
    }
    

    Desktop app:

    public class AesFileIo {
    
        private static final String EOL = "\n";
        private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
        private SecretKeySpec secretKeySpec;
        private IvParameterSpec ivSpec;
    
        AesFileIo(byte[] aesKey, byte[] iv) {
            Security.addProvider(new org.bouncycastle.jce.provider
                    .BouncyCastleProvider());
            ivSpec = new IvParameterSpec(iv);
            secretKeySpec = new SecretKeySpec(aesKey, "AES");
        }
    
        public String readFile(String fileName) {
            StringBuilder stringBuilder = new StringBuilder();
            try {
                FileInputStream fis = new FileInputStream(fileName);
                Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
                CipherInputStream cis = new CipherInputStream(fis, cipher);
                InputStreamReader isr = new InputStreamReader(cis);
                BufferedReader reader = new BufferedReader(isr);
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line).append(EOL);
                }
                fis.close();
            } catch (java.io.FileNotFoundException e) {
                System.out.println("FileNotFoundException: probably OK");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return stringBuilder.toString();
        }
    
        public void writeFile(String fileName, String theFile) {
            try {
                Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
                byte[] encrypted = cipher.doFinal(theFile.getBytes());
                FileOutputStream fos = new FileOutputStream(fileName);
                fos.write(encrypted);
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题