在Android中安全存储少量文本字段

时间:2016-06-17 20:05:27

标签: android security

我以前没有做过这件事。我需要存储少量文本,如本地的密码,用户名和PIN可以在文件或SharedPrefs中。我认为如果手机是root的话,就无法保护数据,所以我只想存储那些没有root的手机 我使用以下代码验证:

private static boolean isRooted() {
    return findBinary("su");
}

public static boolean findBinary(String binaryName) {
    boolean found = false;
    if (!found) {
        String[] places = {"/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/",
                "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/"};
        for (String where : places) {
            if ( new File( where + binaryName ).exists() ) {
                found = true;
                break;
            }
        }
    }
    return found;
}

因此,通过检查isRooted的返回值是否为TRUE / FALSE我想调用save方法!

我认为我们必须使用数据加密和解密我不确定!请指教!

添加了加密和解密(不知道如何存储不同的文本字段)

public static SecretKey generateKey() throws NoSuchAlgorithmException {
    // Generate a 256-bit key
    final int outputKeyLength = 256;
    SecureRandom secureRandom = new SecureRandom();
    // Do *not* seed secureRandom! Automatically seeded from system entropy.
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(outputKeyLength, secureRandom);
    yourKey = keyGenerator.generateKey();
    return yourKey;
}

public static byte[] encodeFile(SecretKey yourKey, byte[] fileData)
        throws Exception {
    byte[] encrypted = null;
    byte[] data = yourKey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(data, 0, data.length,
            algorithm);
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(
            new byte[cipher.getBlockSize()]));
    encrypted = cipher.doFinal(fileData);
    return encrypted;
}

public static byte[] decodeFile(SecretKey yourKey, byte[] fileData)
        throws Exception {
    byte[] decrypted = null;
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(
            new byte[cipher.getBlockSize()]));
    decrypted = cipher.doFinal(fileData);
    return decrypted;
}

void saveFile(String stringToSave) {
    try {
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator, encryptedFileName);
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(file));
        yourKey = generateKey();
        byte[] filesBytes = encodeFile(yourKey, stringToSave.getBytes());
        bos.write(filesBytes);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

void decodeFile() {

    try {
        byte[] decodedData = decodeFile(yourKey, readFile());
        String str = new String(decodedData);
        System.out.println("DECODED FILE CONTENTS : " + str);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public byte[] readFile() {
    byte[] contents = null;

    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator, encryptedFileName);
    int size = (int) file.length();
    contents = new byte[size];
    try {
        BufferedInputStream buf = new BufferedInputStream(
                new FileInputStream(file));
        try {
            buf.read(contents);
            buf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return contents;
}

0 个答案:

没有答案