Bonjour à tous,

Je cherche à développer en JAVA un moyen de crypter un fichier mp4. J'utilise pour cela la classe CryptoUtils, qui fonctionne bien pour des fichiers légers.

Par contre, dès que j'essaye de passe des fichiers un peu plus lourds, ça bloque, au niveau du chargement dans un buffer de la taille du fichier (beaucoup trop lourd pour la JVM).

Je cherche donc un moyen d'optimiser cette gestion de buffers en intervenant sur la fonction 'doCrypto' : j'aimerais découper l'opération en manipulant des buffers de plus petite taille, mais je n'ai pas trouvé le moyen de faire ça (découpage, reconstitution pour le flux sortant, etc...)

Quelqu'un aurait-il une idée ?

J'utilise ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
 
public class CryptoUtils {
	private static final String ALGORITHM = "AES";
	private static final String TRANSFORMATION = "AES";
 
	public static void encrypt(String key, File inputFile, File outputFile) throws CryptoException {
		doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
	}
 
	public static void decrypt(String key, File inputFile, File outputFile) throws CryptoException {
		doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
	}
 
	private static void doCrypto(int cipherMode, String key, File inputFile, File outputFile) throws CryptoException {
		try {
			Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
			Cipher cipher = Cipher.getInstance(TRANSFORMATION);
			cipher.init(cipherMode, secretKey);
 
			FileInputStream inputStream = new FileInputStream(inputFile);
 
			long IFLength = inputFile.length();
 
			// Quelque chose à faire ICI :)
 
			byte[] inputBytes = new byte[(int) IFLength];
			int nbLecture;
			while ((nbLecture = inputStream.read(inputBytes)) != -1) {
				inputStream.read(inputBytes, 0, nbLecture);
			}
 
			byte[] outputBytes = cipher.doFinal(inputBytes);
 
			FileOutputStream outputStream = new FileOutputStream(outputFile);
			outputStream.write(outputBytes);
 
			inputStream.close();
			outputStream.close();
 
		} catch (Exception ex) {
			throw new CryptoException("Error encrypting/decrypting file", ex);
		}
	}
}