在Android中加密图像的最佳方式

时间:2014-03-05 20:37:09

标签: android encryption

我有一个应用程序,我从服务器下载图像。我想加密这些图像,但我不知道最好的方法是什么,而不会失去很多性能。我的应用程序需要同时访问大量图像,但我需要对它们进行加密,以便用户无法轻松获取它。

非常感谢您提前:))

1 个答案:

答案 0 :(得分:1)

您可能会尝试运行自己的加密..问题当然是如何处理您想要使用的“密钥”以确保它不会受到损害。以下是使用“DES”加密文件的示例。 (您可以扩展以处理解密)。

public class Obscure {
private byte[] k = "Now is the time for all good men to come to the aid of their country."
        .getBytes();

public Obscure(String keyString) {
    k = keyString.getBytes();
}

public boolean encryptFile(String source, String target)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IOException {
    Cipher encoding;
    byte[] buffer = new byte[8192];

    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(target);

    SecretKeySpec key = new SecretKeySpec(k, "DES");
    encoding = Cipher.getInstance("DES");
    encoding.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(fos, encoding);
    int numBytes;
    while ((numBytes = fis.read(buffer)) != -1) {
        cos.write(buffer, 0, numBytes);
    }
    fos.flush();
    fis.close();
    fos.close();
    cos.close();
    return true;
}
}
相关问题