import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class JI9049266 {
	public void newDecodeTest(final String expectedClearString, final byte[] keyData, final byte[] ivData, 
			final byte[] encryptedData) 
	{ 
		try 
		{ 
			SecretKey aesKey = new SecretKeySpec(keyData, "AES"); 
			AlgorithmParameterSpec iv = new IvParameterSpec(ivData); 

			Cipher cipher = Cipher.getInstance("AES/OFB32/PKCS5PADDING"); 
			cipher.init(Cipher.DECRYPT_MODE, aesKey, iv); 

			byte[] clearData = cipher.doFinal(encryptedData); 

			String clearText = new String(convertByteArrayToCharArray(clearData)); 
			if(clearText.equals(expectedClearString)) 
			{ 
				System.out.println("Successs !!! "); 
			} 

		} 
		catch (Exception e) 
		{ 
			e.printStackTrace(); 
		} 
	} 

	public static char[] convertByteArrayToCharArray(final byte[] source) 
	{ 
		if (source == null) 
		{ 
			throw new IllegalArgumentException("source is null."); 
		} 

		CharBuffer sourceCharBuffer = ByteBuffer.wrap(source).asCharBuffer(); 

		char[] result = new char[sourceCharBuffer.length()]; 
		for (int i = 0; i < result.length; i++) 
		{ 
			result[i] = sourceCharBuffer.get(); 
		} 
		return result; 
	} 

	public static void main (String[] args) 
	{ 
		JI9049266 doFinalBug = new JI9049266(); 
		String expectedClearString = "Xyz"; 
		byte[] keyData= new byte[] {-20, -67, 94, -85, -125, -113, -15, 29, -100, 69, 124, -84, -64, -2, 21, -1}; 
		byte[] ivData=new byte[] {-52, 10, -84, 57, -100, 6, -60, 92, -127, -47, -60, 92, -32, -77, -24, 53}; 
		byte[] encryptedData = new byte[] {-100, -8, -97, 116, 81, -58, -61, -42, 3, 72, 62, -32, -49, 84, -2, 73 }; 

		doFinalBug.newDecodeTest(expectedClearString, keyData, ivData, encryptedData); 

	} 

}
