代码在JAVA上运行正常但在Android上无法创建解密文件

时间:2015-11-30 14:33:20

标签: java android encryption

我正在尝试制作一个适用于Android的File Locker App文件。对于加密&解密我使用了一些我之前在JAVA上测试过的代码。它在JAVA上工作正常,但无法在Android平台上创建解密文件。

我的代码类似于贝娄,

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;
    }

 }
}

我正在使用一个按钮,当按下所有文件时,首先要对已声明的数组进行加密,然后将加密文件解密到另一个位置。但是没有创建解密文件。

按钮的onClick方法看起来像是吼叫,

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);

            }

        }
}

有人请帮帮我.. !!

ExternalStorageDirectoryPath是通过致电String检索到的Environment.getExternalStorageDirectory().getAbsolutePath();

MyFiles Class是

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;
}

}

1 个答案:

答案 0 :(得分:0)

我想它可能是你的Android应用程序的Manifest文件中缺少权限。检查:http://developer.android.com/guide/topics/manifest/manifest-intro.html  或者您正试图在不允许应用的位置写信