Issues with reading & decrypting very LARGE files, in bytes, in Java - java

When reading from a very large encrypted file in Java, I am using the following code:
FileInputStream in = new FileInputStream("file.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveLocation), "utf-8"));
int read;
byte buffer[] = new byte[16384];
byte getData[] = new byte[16384];
while((read = in.read(buffer)) != -1)
{
baos.write(buffer, 0, read);
Cipher cipher = Cipher.getInstance(symCipher);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initVecBytes);
cipher.init(Cipher.DECRYPT_MODE, originalKey, ivParameterSpec);
byte[] original = cipher.doFinal(baos.toByteArray());
String s = new String(original);
writer.append(s);
baos.reset();
}
writer.close();
As the file is very large (too large for me to load into memory in one go) I am reading it into a small buffer, then encrypting the small bytes of data and finally, writing them to a file.
However, when I do this, some of the data looks to be corrupted:
</AddressLine><_��SR����_�hEE</AddressLine></AddressLines><Postcode>
When I use a smaller file that isn't 16k it works fine, I only seem to get small amounts of corrupted data at the start of a new array read, then it's fine again until the next array read, and so on.
Anyone got any idea why this isn't working properly?

It's not working because most ciphers are stateful. Specifically, in cipher block chaining mode the plaintext must be XOR-ed with previous cipher text block. But every 16k, you are XORing it with the IV instead. You can't re-initialize the Cipher in the middle of a decryption operation.
Here are the five lines of code to which EJP alluded.
Cipher cipher = Cipher.getInstance(symCipher);
cipher.init(Cipher.DECRYPT_MODE, originalKey, new IvParameterSpec(initVecBytes));
try (InputStream in = Files.newInputStream(Paths.get("file.txt"))) {
Files.copy(new CipherInputStream(in, cipher), Paths.get(saveLocation));
}

Get rid of the ByteArrayOutputStream and the Writer and write the decrypted arrays directly to a FileOutputStream.
Use the same Cipher for the whole file, both when encrypting and decrypting, and initialize it once, not once per read.
You can do all this in about five lines of code with a CipherInputStream.

Related

Padding exception in java 7:Caused by: javax.crypto.BadPaddingException: Given final block not properly padded

Hi I am using tripedes key to read from input stream and write to output stream.
Getting this execption in java7/8:Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
byte[] passwd = Base64Util.decode(pwd);
bais = new ByteArrayInputStream(passwd);
baos = new ByteArrayOutputStream();
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);
// Read bytes, decrypt, and write them out.
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = bais.read(buffer)) != -1) {
out.write(cipher.update(buffer, 0, bytesRead));
}
// Write out the final bunch of decrypted bytes
out.write(cipher.doFinal());
out.flush();
Can anyone please tell me what might be error in cipher.doFinal?
Update, encryption code copied from comment:
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Create a special output stream to do the work for us
CipherOutputStream cos = new CipherOutputStream(out, cipher);
// Read from the input and write to the encrypting output stream
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
cos.close();
If the input data is not always a multiple of the block size then padding must be added to the input data; specify padding, PKCS#5 is the preferred padding for DES/3DES.
The getInstance call is not fully specified, missing are the mode and padding options. Add the full specification such as: "DESede/ECB/PKCS5Padding (168)". See Class Cipher documentation.
Do not use 3DES in new code, it is insecure and also do not use ECB mode, it too is insecure, see ECB mode, scroll down to the Penguin.

Java Decryption Returns Blank

I'm currently working on an encryption program, and I'm having an issue when decrypting. The resulting file is blank, and I have been trying to find the reason for this for about an hour. My decryption code is below...
Can someone please tell me why my data might come out blank?
file = x;
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
file = new File(file.getAbsolutePath().substring(0,
file.getAbsolutePath().length() - 4));
FileOutputStream fos = new FileOutputStream(file);
byte k[] = Hash.MD5(password).getBytes("UTF-8");
SecretKeySpec key = new SecretKeySpec(k, "AES");
Cipher cipher = Cipher.getInstance(algorithm);
byte[] iv = batchIV;
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
CipherInputStream cin = new CipherInputStream(fis,
cipher);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = cin.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.flush();
fos.close();
cin.close();
Links go to larger code portions.
Decrypt Method Here: http://pastebin.com/2p2juUTa
Full Class Here: http://pastebin.com/hgZHT4wg
I've found that the CipherInputStream is returning -1 when you try to read from it... I'm still unsure as to what might cause this, if anyone can help.
You don't state what Hash.MD5() and Hash.MD5R() do, let alone supply the source code, but evidently MD5() returns a String, which is already an error (see below), and that MD5R() only returns the input argument, which is basically pointless. Unless you think you've discovered a way to reverse MD5? You haven't.
If you make the following changes to your code:
change MD5() to return the byte[] resulting from the MessageDigest.digest() operation that it must perform, instead of wrapping that in a String, and make the corresponding adjustments at the callings sites (i.e. remove .toByteArray("UTF-8") in a couple of places, and
change MD5R() to return the input argument, or just remove the method,
you will find that your code works.
NB:
Repeat after me: 'String is not a container for binary data'.
flush() before close() is redundant.
The File variable file should be method-local, not static.
You don't need all those File.getAbsolutePath() method calls. getPath() will work just as well in most cases, or just the File object itself in others.

Decrypting image using java

I take this code from YouTube video.From this code I encrypt image correctly but could not decrypt that image..
Can anyone help me???
Encrypt code
FileInputStream file = new FileInputStream("src/image/A.jpg");
FileOutputStream output = new FileOutputStream("src/image/AA.jpg");
byte j[]="12345678".getBytes();
SecretKeySpec kye = new SecretKeySpec(j,"DES");
System.out.println(kye);
Cipher enc = Cipher.getInstance("DES");
enc.init(Cipher.ENCRYPT_MODE,kye);
CipherOutputStream cos = new CipherOutputStream(output, enc);
byte[] buf = new byte[1024];
int read;
while((read=file.read(buf))!=-1){
cos.write(buf,0,read);
}
file.close();
output.flush();
cos.close();
Decrypt code
FileInputStream file = new FileInputStream("src/image/AA.jpg");
FileOutputStream output = new FileOutputStream("src/image/AAA.jpg");
byte j[]="12345678".getBytes();
SecretKeySpec kye = new SecretKeySpec(j,"DES");
System.out.println(kye);
Cipher enc = Cipher.getInstance("DES");
enc.init(Cipher.DECRYPT_MODE,kye);
CipherOutputStream cos = new CipherOutputStream(output, enc);
byte[] buf = new byte[1024];
int read;
while((read=file.read(buf))!=-1){
cos.write(buf,0,read);
}
file.close();
output.flush();
cos.close();
thank you
It is a relativly old post but I think I can help.
First, you should encode the Image into a ASCII representation. I would recommend Base64. It is much easier and less error attached when encrypting Base64. (Maybe not as strong but that depends on your needs)
The benefit of Base64 is the Alphabet it is using. No weird symbols at all.
1) Convert the image into a ByteArrayOutputStream by writing it with the ImageIO Class into one.
2) Encode the byte array into a Base64 String
3) Encrypt like you did above (Do not forget the flush).
4) Save bytes to new File. Delete old one.
Decrypt accordingly .....
Be aware, encoding into Base64 will blow up your memory and the file will be much bigger because of the Base64 AND the Encryption overhead.
Hope that helps !

Decrypt file in parts

So I have these large files (6GB+) that I need to decrypt on a 32 bit computer. The general procedure that I used previously was to read the entire file in memory, then pass it on to the decrypt function and then write it all back to a file. This doesn't really work due to memory limitations. I did try passing the file in parts to the decrypt function but it seems to mess up around the boundaries of where I break up the file before sending it to the decrypt function.
I've tried breaking up the file in parts relative to key size but that doesnt seem to matter. I tried a byte array of size 2048 as well as a byte aray of size 294 thinking that might be the special boundary but, no luck. I can see parts of the file correctly decrypted but parts which are total gibberish.
Is it just NOT POSSIBLE to decrypt the file in chunks? If there is a way, then how?
Here is my decryption function / my attempt to decrypt in parts.
private Path outFile;
private void decryptFile(FileInputStream fis, byte[] initVector, byte[] aesKey, long used) {
//Assume used = 0 for this function.
byte[] chunks = new byte[2048]; //If this number is greater than or equal to the size of the file then we are good.
try {
if (outFile.toFile().exists())
outFile.toFile().delete();
outFile.toFile().createNewFile();
FileOutputStream fos = new FileOutputStream(outFile.toFile());
OutputStreamWriter out = new OutputStreamWriter(fos);
IvParameterSpec spec = new IvParameterSpec(Arrays.copyOfRange(initVector, 0, 16));
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, spec);
int x;
while ((x = fis.read(chunks, 0, chunks.length)) != -1) {
byte[] dec = cipher.doFinal(Arrays.copyOfRange(chunks, 0, x));
out.append(new String(dec));
}
out.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
LOG.error(ExceptionUtils.getStackTrace(e));
}
}
Consider using Cipher#update(byte[], int, int, byte[], int) instead of doFinal() for multipart operations. This will take care of part boundaries for you.
The last part of the deciphered data can be obtained by calling the doFinal(byte[] output, int outputOffset) method.

Why CipherOutputStream cannot write to ByteArrayOutputStream?

I am trying to encrypt a string and store the encrypted bytes in primitive array of bytes using CipherOutputStream which is backed by ByteArrayOutputStream but the size of ByteArrayOutputStream object remains zero and it does not conatin any bytes after something is written to CipherOutputStream object. Here is the code.
ByteArrayOutputStream out = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(out, c);
PrintWriter pw = new PrintWriter(cos);
pw.println("Write something");
cos.flush();
out.flush();
System.out.println(out.size());
pw.close();
So I tried to make a comparison by changing the ByteArrayOutputStream to FileOutputStream using the code below. It turned out that the encrypted bytes are written to the target file. Does anyone have any idea why I cannot use ByteArrayOutputStream here? Can you suggest a solution as well?
FileOutputStream out = new FileOutputStream("/path/encrypted.txt");
CipherOutputStream cos = new CipherOutputStream(out, c);
PrintWriter pw = new PrintWriter(cos);
pw.println("Write something");
pw.close();
The only difference between these snippets is that in the first case you check the content before closing the stream, whereas in the second case - after closing. So, I guess you need to close the stream before checking.
The problem is the cipher.
Cipher cipher = Cipher.getInstance("RSA");
Has no padding.
Use
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
instead

Categories

Resources