Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I doing encryption and decryption of document. Initially i read the file in byte array and passed that byte array to my encrypeted method. Upto 50MB size , i am able to encrypt the file without any issue. But if i increase my file SIZE to 80 MB , it is faling in cipher.doFinal() saying outofmemoryException.
So how to encrypt bigger file without any issues? and also is doFinal() method have any size limitation. Please let me know.

Below is my code.
plainText = cipher.doFinal(plainText); error is throwing.

Java
public static byte[] encrypt(byte[] plainText, byte[] key,
			byte[] initialVector) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException,
			InvalidAlgorithmParameterException, IllegalBlockSizeException,
			BadPaddingException {
		System.out.println("Indside encrypt");
		Cipher cipher = Cipher.getInstance(cipherTransformation);
		System.out
				.println("cipher object created now key specification object creation");
		SecretKeySpec secretKeySpec = new SecretKeySpec(key,
				aesEncryptionAlgorithm);
		System.out.println("key specification object created");
		IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
		System.out.println("cipher init successfull" + plainText.length);
		int bytesLenght = plainText.length;
		
		
			plainText = cipher.doFinal(plainText);
		
		System.out.println("return encrypted bytes");
		return plainText;
	}
Posted
Comments
Shubhashish_Mandal 3-Jul-14 8:59am    
It is not clear whether you send all the byte information at once or not, if you did this then it might be an issue for big file. Instead of that encrypt file chunk by chunk.i.e read the file with fixed byte and then encrypt it and continue until finish

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900