繁体   English   中英

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

[英]Best way to encrypt images in Android

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

提前非常感谢您:)

您当然可以尝试运行自己的crypto ..问题,这将是如何处理要使用的“密钥”,以确保它不会受到损害。 这是使用“ 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;
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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