I have to decrypt an image in java which is encrypted in .NET using Rijndael Algorithm. I am using AES algorithm to decrypt on java.
Encryption code in .NET:
public void EncryptFile(string inFile, string outFile, string password)
{
using (FileStream fin = File.OpenRead(inFile),
fout = File.OpenWrite(outFile))
{
long lSize = fin.Length;
int size = (int)lSize;
byte[] bytes = new byte[BUFFER_SIZE];
int read = -1;
byte[] IV = GenerateRandomBytes(16);
byte[] salt = GenerateRandomBytes(16);
// create the crypting object
SymmetricAlgorithm sma = CreateRijndael(password, salt);
sma.IV = IV;
// write the IV and salt to the beginning of the file
fout.Write(IV, 0, IV.Length);
fout.Write(salt, 0, salt.Length);
// create the hashing and crypto streams
HashAlgorithm hasher = SHA256.Create();
using (CryptoStream cout = new CryptoStream(fout, sma.CreateEncryptor(), CryptoStreamMode.Write),
chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
{
// write the size of the file to the output file
BinaryWriter bw = new BinaryWriter(cout);
bw.Write(lSize);
// write the file cryptor tag to the file
bw.Write(FC_TAG);
// read and the write the bytes to the crypto stream in BUFFER_SIZEd chunks
while ((read = fin.Read(bytes, 0, bytes.Length)) != 0)
{
cout.Write(bytes, 0, read);
chash.Write(bytes, 0, read);
//value += read;
}
chash.Flush();
chash.Close();
// read the hash
byte[] hash = hasher.Hash;
// write the hash to the end of the file
cout.Write(hash, 0, hash.Length);
cout.Flush();
cout.Close();
}
}
}
// Creates a Rijndael SymmetricAlgorithm for use in EncryptFile and DecryptFile
private SymmetricAlgorithm CreateRijndael(string password, byte[] salt)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000);
SymmetricAlgorithm sma = Rijndael.Create();
sma.KeySize = 256;
sma.Key = pdb.GetBytes(32);
sma.Padding = PaddingMode.PKCS7;
return sma;
}
private byte[] GenerateRandomBytes(int count)
{
byte[] bytes = new byte[count];
rand.GetBytes(bytes);
return bytes;
}
Decryption code in java:
public static void decryptImageUsingRijndaelAlgorithm(){
try{
byte[] iv = new byte[]
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//Security.addProvider(new com.sun.crypto.provider.SunJCE());
SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
rand.nextBytes(salt);
PBEKeySpec password = new PBEKeySpec("Bbbbb1234".toCharArray(), salt, 1000, 128);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKey pkey = (PBEKey) factory.generateSecret(password);
SecretKey key = new SecretKeySpec(pkey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
//Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
System.out.println("Cipher provider: " + cipher.getProvider());
//AES Decryption
File encryptedImage = new File("c:/EncryptDecrypt/AESencrypted.jpg");
byte[] encryptedImageBytes = new byte[(int)encryptedImage.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(encryptedImage));
dis.readFully(encryptedImageBytes);
//cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encryptedImageBytes);
FileOutputStream desrOut = new FileOutputStream("c:/EncryptDecrypt/AESDecryptedJAVA.jpg");
desrOut.write(decrypted);
dis.close();
desrOut.close();
}
catch(Exception e){
e.printStackTrace();
}
}
When its decrypting, the cipher.doFinal(encryptedImageBytes) is giving the following exception
javax.crypto.BadPaddingException: pad block corrupted
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA12275)
I also tried with PKCS5 and PKCS7 padding. And also used BouncyCastleProvide and SunJCE provider.
Any help will be appreciated.
Thanks.