简体   繁体   中英

Code works fine on JAVA But Fails to Create Decrypted File on Android

I was trying to make a file File Locker App for Android. For encryption & Decryption I used some code which I previously tested on JAVA. It works fine on JAVA, But Fails to Create The Decrypted File on Android platform.

My Code is something like bellow,

public class Cryptographer {
private static final String ALGORITHM = "AES";

private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG";

public static int encrypt(String key, File inputFile, File outputFile)
        throws CryptographyException {
    if(!outputFile.getParentFile().exists())outputFile.getParentFile().mkdir();
    return StartCryptography(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}

public static int decrypt(String key, File inputFile, File outputFile)
        throws CryptographyException {
    if(!outputFile.getParentFile().exists())outputFile.getParentFile().mkdir();
    return StartCryptography(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}

private static int StartCryptography(int cipherMode, String key, File inputFile,
                                     File outputFile) throws CryptographyException {
    int performance = -1;
    try {
        SecureRandom random = SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR);
        random.setSeed(key.getBytes());
        KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
        generator.init(random);
        SecretKey key1 = generator.generateKey();

        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(cipherMode, key1);

        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] inputBytes = new byte[(int) inputFile.length()];
        inputStream.read(inputBytes);

        byte[] outputBytes = cipher.doFinal(inputBytes);

        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(outputBytes);

        inputStream.close();
        outputStream.close();
        performance = 1;

    } catch (NoSuchPaddingException | NoSuchAlgorithmException
            | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | IOException ex) {
        throw new CryptographyException("Error encrypting/decrypting file", ex);
    } finally {
        return performance;
    }

 }
}

I am using a button which when pressed All Files From a declared Array Should First be encrypted, and then the encrypted file will be decrypted to another location. But no Decrypted file is created.

My onClick Method of the button looks like bellow,

public void onKeyDown1(View view) throws CryptographyException {

        File outfile = new File(ExternalStorageDirectoryPath, "Encrypted");
        File outfileDec = new File(ExternalStorageDirectoryPath, "Decrypted");
        for (MyFiles f : files) {
            if (f.getCheckstate()) {
                File EncFile=new File(outfile,f.getName());
                //File DecFile=new File(outfileDec,f.getName());

                Cryptographer.encrypt(key, f, EncFile);

                Cryptographer.decrypt(key, EncFile, DecFile);

            }

        }
}

Someone please help me..!!

The ExternalStorageDirectoryPath is a String retrieved by Calling Environment.getExternalStorageDirectory().getAbsolutePath(); .

And MyFiles Class is

public class MyFiles extends File {

boolean Checkstate=false;

public MyFiles(File dir, String name) {
    super(dir, name);
}

public MyFiles(String path) {
    super(path);
}

public MyFiles(String dirPath, String name) {
    super(dirPath, name);
}

public MyFiles(URI uri) {
    super(uri);
}

public void setCheckstate(boolean checkstate) {
    Checkstate = checkstate;
}

public boolean getCheckstate(){
    return Checkstate;
}


public MyFiles[] listFiles(FileFilter filter) {
    MyFiles[] files = listFiles();
    if (filter == null || files == null) {
        return files;
    }
    List<MyFiles> result = new ArrayList<MyFiles>(files.length);
    for (MyFiles file : files) {
        if (filter.accept(file)) {
            result.add(file);
        }
    }
    return result.toArray(new MyFiles[result.size()]);
}

public MyFiles[] listFiles() {
    return filenamesToFiles(list());
}

private MyFiles[] filenamesToFiles(String[] filenames) {
    if (filenames == null) {
        return null;
    }
    int count = filenames.length;
    MyFiles[] result = new MyFiles[count];
    for (int i = 0; i < count; ++i) {
        result[i] = new MyFiles(this, filenames[i]);
    }
    return result;
}

}

I suppose it might be lack of permissions in Manifest file of your android app. Check: http://developer.android.com/guide/topics/manifest/manifest-intro.html Or you are trying to write in a location your app is not allowed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM