Actually, I searched lot from internet and in stackoverflow too for this,
Initially I don't used padding in my encryption and decryption,
But Finally I got solution from here
https://mcmap.net/q/745214/-java-missing-final-characters-when-encrypting-using-blowfish
and I updated my code with padding as AES/CBC/PKCS5Padding and the same error is coming, and last block is not decrypted...
I'm working on this for last two day, but no solution found
my Crypter Code:
package mani.droid.browsedropbox;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Crypter {
Cipher encipher;
Cipher decipher;
CipherInputStream cis;
CipherOutputStream cos;
FileInputStream fis;
byte[] ivbytes = new byte[]{(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o', (byte)'p'};
IvParameterSpec iv = new IvParameterSpec(ivbytes);
public boolean enCrypt(String key, InputStream is, OutputStream os)
{
try {
byte[] encoded = new BigInteger(key, 16).toByteArray();
SecretKey seckey = new SecretKeySpec(encoded, "AES");
encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
encipher.init(Cipher.ENCRYPT_MODE, seckey, iv);
cis = new CipherInputStream(is, encipher);
copyByte(cis, os);
return true;
}
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean deCrypt(String key, InputStream is, OutputStream os)
{
try {
byte[] encoded = new BigInteger(key, 16).toByteArray();
SecretKey seckey = new SecretKeySpec(encoded, "AES");
encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
encipher.init(Cipher.DECRYPT_MODE, seckey, iv);
cos = new CipherOutputStream(os, encipher);
copyByte(is, cos);
//cos.close();
return true;
}
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void copyByte(InputStream is, OutputStream os) throws IOException
{
byte[] buf = new byte[8192];
int numbytes;
while((numbytes = is.read(buf)) != -1)
{
os.write(buf, 0, numbytes);
os.flush();
}
os.close();
is.close();
}
}
BadPaddingException
. – Uraniannumbytes
incopyByte
, the result is when encryption, all blocks are read, 16 bits even for that last incomplete block, but for decryption last block value is not 16, less than 16..., So My guess is, its padded with null or 0, so its reading full block at the time of encryption, so the encrypted file size is same as my original file size not in multiple of 16bytes... – BaptlstadoFinal()
on the Cipher directly to get the encrypted bytes and eliminated any stream issues. – PushdoFinal()
with files – Baptlstado Final()
. Or useupdate()
and read the file in chunks if it is larger, pass the last chunk todoFinal()
. – Pushbyte[]
from file anddofinal
and write the outputbyte[]
in file, its again works in encryption, and for decryption, sameIllegalBlockException last block incomplete in decryption
Code for Read and Write byte from/to file, Read:ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); copyByte(is, byteArray); byte[] enData = byteArray.toByteArray();
, Write:byte[] deData = decipher.doFinal(enData); BufferedOutputStream bos = new BufferedOutputStream(os); bos.write(deData); bos.flush(); bos.close();
– BaptlstacopyByte
Code:public void copyByte(InputStream is, OutputStream os) throws IOException { byte[] buf = new byte[8192]; int numbytes; while((numbytes = is.read(buf)) != -1) { Log.e("tamil", Integer.toString(numbytes)); os.write(buf, 0, numbytes); } os.flush(); is.close(); os.close(); }
– Baptlsta