tarjeta significa qué que proteger pasa microsd micro externa encriptar encriptada como cifrar card almacenamiento android file encryption aes

significa - Cómo cifrar el archivo de la tarjeta SD con AES en Android?



qué pasa si cifrar la tarjeta sd externa (1)

Quiero cifrar la imagen de la tarjeta SD y almacenarla de nuevo en la tarjeta SD de nuevo con AES. La idea principal es que la aplicación explore una imagen, luego la encripte cuando presiono un botón, luego la almacena en una tarjeta SD. para que mi imagen sea segura

Ya tengo éxito en el cifrado de cadenas con AES desde este tutorial http://www.androidsnippets.com/encryptdecrypt-strings , pero no tengo idea de cómo hacerlo con una imagen, no con una cadena.

Así es como lo hago con una cadena:

public static String encrypt(String seed, String cleartext) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] result = encrypt(rawKey, cleartext.getBytes()); return toHex(result); } private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; }

¿Alguien puede ayudarme a dar un código de ejemplo sobre cómo encriptar una imagen con AES?

tal vez debe usar el flujo de archivos de E / S, pero no tengo una idea de cómo implementarlo con este código.


Si toma la entrada del usuario para la contraseña, asegúrese de leer esta respuesta .

Debería echar un vistazo a: CipherInputStream y CipherOutputStream . Se usan para cifrar y descifrar flujos de bytes.

Tengo un archivo llamado cleartext . El archivo contiene:

Hi, I''m a clear text. How are you? That''s awesome!

Ahora, tienes una función encrypt() :

static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { // Here you read the cleartext. FileInputStream fis = new FileInputStream("data/cleartext"); // This stream write the encrypted text. This stream will be wrapped by another stream. FileOutputStream fos = new FileOutputStream("data/encrypted"); // Length is 16 byte // Careful when taking user input!!! https://.com/a/3452620/1188357 SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); // Create cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sks); // Wrap the output stream CipherOutputStream cos = new CipherOutputStream(fos, cipher); // Write bytes int b; byte[] d = new byte[8]; while((b = fis.read(d)) != -1) { cos.write(d, 0, b); } // Flush and close streams. cos.flush(); cos.close(); fis.close(); }

Después de ejecutar esta función, debe haber un nombre de archivo encrypted . El archivo contiene los caracteres encriptados.

Para el descifrado tienes la función de decrypt :

static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { FileInputStream fis = new FileInputStream("data/encrypted"); FileOutputStream fos = new FileOutputStream("data/decrypted"); SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sks); CipherInputStream cis = new CipherInputStream(fis, cipher); int b; byte[] d = new byte[8]; while((b = cis.read(d)) != -1) { fos.write(d, 0, b); } fos.flush(); fos.close(); cis.close(); }

Después de la ejecución del descifrado, debe haber un archivo llamado decrypted . Este archivo contiene el texto libre.

Usted escribe que es un "novato" pero, dependiendo del caso de uso del cifrado, podría hacer mucho daño si no lo hace de la manera correcta. ¡Conoce tus herramientas!

Uso de la documentación de Oracle CipherOutputStream:

SecretKeySpec skeySpec = new SecretKeySpec(y.getBytes(), "AES"); FileInputStream fis; FileOutputStream fos; CipherOutputStream cos; // File you are reading from fis = new FileInputStream("/tmp/a.txt"); // File output fos = new FileOutputStream("/tmp/b.txt"); // Here the file is encrypted. The cipher1 has to be created. // Key Length should be 128, 192 or 256 bit => i.e. 16 byte SecretKeySpec skeySpec = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); Cipher cipher1 = Cipher.getInstance("AES"); cipher1.init(Cipher.ENCRYPT_MODE, skeySpec); cos = new CipherOutputStream(fos, cipher1); // Here you read from the file in fis and write to cos. byte[] b = new byte[8]; int i = fis.read(b); while (i != -1) { cos.write(b, 0, i); i = fis.read(b); } cos.flush();

Por lo tanto, la encriptación debería funcionar. Cuando invierte el proceso, debería poder leer los bytes descifrados.