· 7 years ago · May 18, 2018, 03:18 PM
1@RequiresApi(api = Build.VERSION_CODES.O)
2public void Encriptar_Archivo() throws Exception {
3
4 //File archivo = new File(Ruta);
5 //Path direccion = Paths.get(("/storage/emulated/0/pelicula.mp4"));
6 File file = new File("/storage/emulated/0/pelicula.mp4");
7 InputStream is = new BufferedInputStream(new FileInputStream(file));
8 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
9
10 int nRead;
11 byte[] data = new byte[1999368];
12
13 while ((nRead = is.read(data, 0, data.length)) != -1) {
14 buffer.write(data, 0, nRead);
15 }
16
17 buffer.flush();
18
19 byte[] prueba = buffer.toByteArray();
20 byte encriptado[];
21 encriptado = Cifrar (prueba);
22
23 decrypt (encriptado); //desencriptas el archivo
24
25
26 System.out.println(encriptado);
27
28
29}
30
31
32
33
34
35 public static byte [] Cifrar(byte[] prueba) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
36
37
38
39 final MessageDigest md = MessageDigest.getInstance("md5");
40 final byte[] digestOfPassword = md.digest("OOAA2011".getBytes("utf-8"));
41 final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
42 for (int j = 0, k = 16; j < 8;) {
43 keyBytes[k++] = keyBytes[j++];
44 }
45 final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
46 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
47 final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS7Padding");
48 cipher.init(Cipher.ENCRYPT_MODE, key);
49 byte[] result= cipher.doFinal(prueba);
50
51 return result;
52
53}
54@RequiresApi(api = Build.VERSION_CODES.O)
55public static byte[] decrypt(byte[] message) throws Exception {
56 final MessageDigest md = MessageDigest.getInstance("md5");
57 final byte[] digestOfPassword = md.digest("OOAA2011".getBytes("utf-8"));
58 final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
59 for (int j = 0, k = 16; j < 8;) {
60 keyBytes[k++] = keyBytes[j++];
61 }
62 final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
63 final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
64 final Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS7Padding");
65 decipher.init(Cipher.DECRYPT_MODE, key);
66
67 final byte[] plainText = decipher.doFinal(message);
68
69
70 //Path path = Paths.get("/storage/emulated/0/Ejemplo.docx");
71 // Files.write(path, plainText);
72 Files.write(new File("/storage/emulated/0/Ejemplo2.mp4").toPath(), plainText);
73
74 return plainText;
75}