[英]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.