• Ganesh Kothavale
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
Hi,
I tried to retrieve all the components of a org with known credentials using Salesforce ant migration tool. Some of the components were successfully retrieved while some of the components have content as
"(hidden)".
Why this happens?
Are there any access privilages on the components?
If yes how to make them accessible.
How AES decryption in apex is different from AES decryption in java using Cipfer.doFinal method in java. Please help with the method used for Decryption.
Thanks,
Ganesh Kothavale.
Hi team,
Currently I am trying to Decrypt a AES String Encrypted by AES128 technique without initialisation vector. The decryption code for the same is as below in java
public class AESEncryptionUtil
{
      private static final String ALGO = "AES";
	  /**
	  encryptedData=data to be decrypted 
	  key=private key "Pass12"
	  **/
	  
       public static String decrypt(String encryptedData, String key)
       throws Exception
       {
              byte[] keyData = new byte[16];
              System.arraycopy(key.getBytes(Charset.forName("UTF-8")), 0,
                           keyData, 0, key.length());
              Key secretKey = new SecretKeySpec(keyData, "AES");
              Cipher c = Cipher.getInstance("AES");
              c.init(2, secretKey);
             return new String(c.doFinal(Base64.decodeBase64(encryptedData)));
              } 
}

where encryptedData is the encrypted String and key is the private key used for decryption.Where key value is "Pass12" in java.

I tried the decryption with below code in Apex
public class IconsoleIntegration {

	static Map<String,String> base64DecodeTable=new Map<String,String>{'A'=>'000000',
	'B'=>'000001','Q'=>'010000','f'=>'011111','u'=>'101110','9'=>'111101',
	'C'=>'000010','R'=>'010001','g'=>'100000','v'=>'101111','+'=>'111110',
	'D'=>'000011','S'=>'010010','h'=>'100001','w'=>'110000','/'=>'111111',
	'E'=>'000100','T'=>'010011','i'=>'100010','x'=>'110001','='=>'000000',
	'F'=>'000101','U'=>'010100','j'=>'100011','y'=>'110010',
	'G'=>'000110','V'=>'010101','k'=>'100100','z'=>'110011',
	'H'=>'000111','W'=>'010110','l'=>'100101','0'=>'110100',
	'I'=>'001000','X'=>'010111','m'=>'100110','1'=>'110101',
	'J'=>'001001','Y'=>'011000','n'=>'100111','2'=>'110110',
	'K'=>'001010','Z'=>'011001','o'=>'101000','3'=>'110111',
	'L'=>'001011','a'=>'011010','p'=>'101001','4'=>'111000',
	'M'=>'001100','b'=>'011011','q'=>'101010','5'=>'111001',
	'N'=>'001101','c'=>'011100','r'=>'101011','6'=>'111010',
	'O'=>'001110','d'=>'011101','s'=>'101100','7'=>'111011',
	'P'=>'001111','e'=>'011110','t'=>'101101','8'=>'111100'};

	//map to convert eight bit binary string to decmal value
	//static Map<Integer,Integer> binaryToDecimalMap=new Map<Integer,Integer>{0=>-128,1=>64,2=>32,3=>16,4=>8,5=>4,6=>2,7=>1};

	static Map<String,String> binaryToHexTable=new Map<String,String>{
		'0000'=>'0',
		'0001'=>'1',
		'0010'=>'2',
		'0011'=>'3',
		'0100'=>'4',
		'0101'=>'5',
		'0110'=>'6',
		'0111'=>'7',
		'1000'=>'8',
		'1001'=>'9',
		'1010'=>'A',
		'1011'=>'B',
		'1100'=>'C',
		'1101'=>'D',
		'1110'=>'E',
		'1111'=>'F'

	};

	//Method to call the web service
	public static String  callOut(){
	Http http = new Http();
	
	// Getting value of encrypted challenge
	//URl for webservice
	String endPointForChallenge = 'http://auth-devint.zycus.com/ZygrateSecurity/rest/v1/auth/user';

	HttpRequest requestForChallenge=createRequest(endPointForChallenge,'GET',null);
	requestForChallenge.setHeader('x-zycus-applicationid', '37yPDNlpZP9uARetqKtupA==');
	requestForChallenge.setHeader('x-zycus-username', 'user1dev.Lowes.Zycusadmin@zycus.com');
	HttpResponse response = http.send(requestForChallenge);
	System.debug(response.toString());

	String challenge=getChallengeFromResponse(response);
	System.debug('Decrypted String:'+challenge);

	return challenge;
	}

	//Method to get decrypted vaue of challenge
	
	public static String getChallengeFromResponse(HttpResponse response){

		/*
		For converting key to a blob 
		At first I had taken the byte array of the key then I took 8 bit binary equivalent of each byte and make a binary string 
		Equivalent to key data and then converted it into a equivalent hex String by taking hex equivalent of each 4 bits in binary String
		and finally converted hexString to the keyBlob

		# arrayOfChars=byte array equivalent To key 
		# keyBinary=binary String(length=128)
		# keyHex= hex equivalent of binary string(length=32)
		# keyBlob= blob from keyHex(length=16)
		*/


		//The byte array similar to keyData in java
		//Ascii codes equivalent to 'Pass12'(P=80,a=97,s=115,1=49,2=50) and 0 for making it 16 bit
		
		Integer[] arrayOfChars=new Integer[]{80, 97, 115, 115, 49, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};	

		String keyBinary='';
		for(Integer z=0;z<16;z++){
			//keyBinary is binary Equivalent of the above byteArray to convert the same to hex String
			keyBinary+=decimalToBinaryString(arrayOfChars[z]);
			System.debug('Conversion:'+arrayOfChars[z]+':'+decimalToBinaryString(arrayOfChars[z]));
		}
		System.debug(keyBinary.length());

		String keyHex='';
		for(Integer i=0;i<keyBinary.length()/4;i++){	
		    //logic to convert keyBinary to hex String			
			keyHex+= binaryToHexString(keyBinary.substring(i*4,(i+1)*4));	

		}

		System.debug('Length of hex Key:'+keyHex.length());
		
		//Private Key generation for Decryption
		Blob keyBlob=EncodingUtil.convertFromHex(keyHex);	
		System.debug('Length of key:'+keyBlob.size());


		/*

		For converting data to be decrypted to a blob First I decoded the data to a binary String
		Using base 64 decode table then I foiund hex equivalent of same and coverted hex to blob using EncodingUtil.convertFromHex(String hexString)

		# challenge=data to be decrypted
		# base64DecodedString= binary equivalent of base 64 decoded challenge
		# hexStringForData=hex equivalent of base64DecodedString (length=32)
		# dataBlob= blob from hexStringForData (length=16)

		*/


		//retreiving challenge  the encrypted Data
		String challenge=response.getHeader('x-zycus-challenge');	
		System.debug('challenge:'+challenge);	

		//base64DecodedString represents base64 decryption of encrypted String
		String base64DecodedString='';
		for(Integer i=0;i<challenge.length();i++){	
			//As challenge is Base 64 Encoded first decoding it		
			base64DecodedString+=base64DecodeTable.get(challenge.substring(i,i+1));	
		}

		System.debug('base64DecodedString:'+base64DecodedString);
		
		//Removing padding from decrypted String
		base64DecodedString=base64DecodedString.subString(0,base64DecodedString.length()-16);
		System.debug('Length of decrypted Base 64 data:'+base64DecodedString.length().format());

		//Hex String equivalent to the base64DecodedString
 		String hexStringForData='';
		for(Integer i=0;i<base64DecodedString.length()/4;i++){
			hexStringForData+= binaryToHexString(base64DecodedString.substring(i*4,(i+1)*4));
		}
		
		//data Blob to be used for data decryption
		Blob dataBlob=EncodingUtil.convertFromHex(hexStringForData);
		
		System.debug('Size of data blob'+dataBlob.size());	

		//Code to decrypt the data
		Blob decryptedChallengeBlob=Crypto.decryptWithManagedIV('AES128',keyBlob,dataBlob);
		
		String decryptedChallenge=decryptedChallengeBlob.toString();
		
		System.debug(decryptedChallenge);
		
		return decryptedChallenge;
	}

	public static HttpRequest createRequest(String endpoint,String method,String body){
		HttpRequest request = new HttpRequest();
        request.setEndpoint(endPoint);   
        request.setMethod(method);
        if(method.equalsIgnoreCase('POST'))
        {
            request.setHeader('Content-Type', 'application/json; charset=UTF-8');
            request.setBody(body);
        }
        return request;
	}


	//Method to get hex equivalent of 8 bit binary data
	public static String binaryToHexString(String binaryString){
		return binaryToHexTable.get(binaryString);
	}

	//Method to get binary Equivalent of a decimal number
	public static String decimalToBinaryString(Integer numberToConvert){

		String binary='';
		Integer remainder;
		if(numberToConvert>=0){
		while(numberToConvert > 0)
        {	
            remainder= math.mod(numberToConvert,2);          
            binary+=remainder.format();
            //System.debug('For '+numberToConvert+':'+binary);
            numberToConvert/=2;
        }
        	binary=binary.reverse();
        if(binary.length()<8){        	
        	Integer diff=8-binary.length();
        	for(Integer i=0;i<diff;i++){
        		binary='0'+binary;      
        	}
        }     
        return binary;
    	}
    	else{
    		Integer maxRange=128;
    		Integer difference=maxRange+numberToConvert;
    		String tempString='';
    		if(difference==0){
    			binary='10000000';
    		}
    		else{
    			
    			while(difference > 0){	
           			 remainder= math.mod(difference,2);          
           			 tempString+=remainder.format();
           			 difference/=2;
        		}
        		tempString=tempString.reverse();
        		Integer length=7-tempString.length();
        		if(length>0){
        			for(Integer i=0;i<length;i++){
        				tempString='0'+tempString;
        			}

        		}	
        		binary='1'+tempString;
    		}
    		
    	}
    	return binary;
	}

}

When I ran the code  I got debugged values as follow:
DEBUG|System.HttpResponse[Status=OK, StatusCode=200]
DEBUG|binary key Data:01010000011000010111001101110011001100010011001000000000000000000000000000000000000000000000000000000000000000000000000000000000
	  binary key length:128
DEBUG|Hex key :50617373313200000000000000000000 
      Hex key length:32
DEBUG|Length of key:16
DEBUG|challenge:LVgP00WwqCbCFUy70MmMdg==
base64DecodedString:001011010101100000001111110100110100010110110000101010000010011011000010000101010100110010111011110100001100100110001100011101100000000000000000
DEBUG|Length of decrypted Base 64 data:128
DEBUG|Data blob size:16
DEBUG|Decrypted challenge String
DEBUG|Decrypted String:

So I am getting decrypted value as blank
After comparison with java code I found that keyData variable  in java is having same byte array as arrayOfChars variable in apex, Also if I convert the base64DecodedString to decimal equivalents of each 8 bits in base64DecodedString I get the same byte array which I recieve in java code as below
#Apex code for converting binaryData to byte array(Integer array in apex)
		
		static Map<Integer,Integer> binaryToDecimalMap=new Map<Integer,Integer>{0=>-128,1=>64,2=>32,3=>16,4=>8,5=>4,6=>2,7=>1};
		
		
		Integer[] dataByteArray=new Integer[16];
		Integer temp;
		for(Integer i=0;i<16;i++){
			
			temp=conversionOfStringToInteger(base64DecodedString.substring(i*8,(i+1)*8));

			dataByteArray[i]=temp;
		}
		System.debug(dataByteArray) 
		
		public static Integer conversionOfStringToInteger(String binaryString){		
		Integer i=0;
		Integer sum=0;
		Integer temp;
		for(i=0;i<8;i++){
			temp=Integer.valueOf(binaryString.substring(i,i+1));			
			sum+=temp*binaryToDecimalMap.get(i);			
		}
		return sum;
		}
		
		
# JAVA code
public class AESEncryptionUtil
	{
      private static final String ALGO = "AES";
	  /**
	  encryptedData=data to be decrypted 
	  key=private key "Pass12"
	  **/
	  
       public static String decrypt(String encryptedData, String key)
       throws Exception
       {
              byte[] keyData = new byte[16];
              System.arraycopy(key.getBytes(Charset.forName("UTF-8")), 0,
                           keyData, 0, key.length());
              Key secretKey = new SecretKeySpec(keyData, "AES");
              Cipher c = Cipher.getInstance("AES");
              c.init(2, secretKey);
         
	//Byte array of base 64 data	   
    	return new String(c.doFinal(Base64.decodeBase64(encryptedData)));
              } 
	}

In above codes After running codes in apex I got dataByteArray same as the byte array which I got at line marked as "Byte array of base 64 data" on displaying the value of the code
   "Base64.decodeBase64(encryptedData)" using debug as follows
# output for apex code
 DEBUG|Byte array for base64 decoded data: (89, -93, -7, 112, 36, -63, 52, -80, 94, -28, ...)
  
 # output for java code: 
 Base64.decodeBase64(encryptedData) value
 [89, -93, -7, 112, 36, -63, 52, -80, 94, -28, 97, -33, -1, -62, 107, 14]

As both key and data are coming same like the java code what am I doing wrong ?
Is the data first encoded using base64 in the method encryptWithManagedIV?
Is there Any differnce in AES implementation of java  and apex?
Please help me with some solution to this or any other method to decrypt the encrypted string.

Thanks.  
 
Hi team,
Currently I am trying to Decrypt a AES String Encrypted by AES128 technique without initialisation vector. The decryption code for the same is as below in java
public class AESEncryptionUtil
{
      private static final String ALGO = "AES";
	  /**
	  encryptedData=data to be decrypted 
	  key=private key "Pass12"
	  **/
	  
       public static String decrypt(String encryptedData, String key)
       throws Exception
       {
              byte[] keyData = new byte[16];
              System.arraycopy(key.getBytes(Charset.forName("UTF-8")), 0,
                           keyData, 0, key.length());
              Key secretKey = new SecretKeySpec(keyData, "AES");
              Cipher c = Cipher.getInstance("AES");
              c.init(2, secretKey);
             return new String(c.doFinal(Base64.decodeBase64(encryptedData)));
              } 
}

where encryptedData is the encrypted String and key is the private key used for decryption.Where key value is "Pass12" in java.

I tried the decryption with below code in Apex
public class IconsoleIntegration {

	static Map<String,String> base64DecodeTable=new Map<String,String>{'A'=>'000000',
	'B'=>'000001','Q'=>'010000','f'=>'011111','u'=>'101110','9'=>'111101',
	'C'=>'000010','R'=>'010001','g'=>'100000','v'=>'101111','+'=>'111110',
	'D'=>'000011','S'=>'010010','h'=>'100001','w'=>'110000','/'=>'111111',
	'E'=>'000100','T'=>'010011','i'=>'100010','x'=>'110001','='=>'000000',
	'F'=>'000101','U'=>'010100','j'=>'100011','y'=>'110010',
	'G'=>'000110','V'=>'010101','k'=>'100100','z'=>'110011',
	'H'=>'000111','W'=>'010110','l'=>'100101','0'=>'110100',
	'I'=>'001000','X'=>'010111','m'=>'100110','1'=>'110101',
	'J'=>'001001','Y'=>'011000','n'=>'100111','2'=>'110110',
	'K'=>'001010','Z'=>'011001','o'=>'101000','3'=>'110111',
	'L'=>'001011','a'=>'011010','p'=>'101001','4'=>'111000',
	'M'=>'001100','b'=>'011011','q'=>'101010','5'=>'111001',
	'N'=>'001101','c'=>'011100','r'=>'101011','6'=>'111010',
	'O'=>'001110','d'=>'011101','s'=>'101100','7'=>'111011',
	'P'=>'001111','e'=>'011110','t'=>'101101','8'=>'111100'};

	//map to convert eight bit binary string to decmal value
	//static Map<Integer,Integer> binaryToDecimalMap=new Map<Integer,Integer>{0=>-128,1=>64,2=>32,3=>16,4=>8,5=>4,6=>2,7=>1};

	static Map<String,String> binaryToHexTable=new Map<String,String>{
		'0000'=>'0',
		'0001'=>'1',
		'0010'=>'2',
		'0011'=>'3',
		'0100'=>'4',
		'0101'=>'5',
		'0110'=>'6',
		'0111'=>'7',
		'1000'=>'8',
		'1001'=>'9',
		'1010'=>'A',
		'1011'=>'B',
		'1100'=>'C',
		'1101'=>'D',
		'1110'=>'E',
		'1111'=>'F'

	};

	//Method to call the web service
	public static String  callOut(){
	Http http = new Http();
	
	// Getting value of encrypted challenge
	//URl for webservice
	String endPointForChallenge = 'http://auth-devint.zycus.com/ZygrateSecurity/rest/v1/auth/user';

	HttpRequest requestForChallenge=createRequest(endPointForChallenge,'GET',null);
	requestForChallenge.setHeader('x-zycus-applicationid', '37yPDNlpZP9uARetqKtupA==');
	requestForChallenge.setHeader('x-zycus-username', 'user1dev.Lowes.Zycusadmin@zycus.com');
	HttpResponse response = http.send(requestForChallenge);
	System.debug(response.toString());

	String challenge=getChallengeFromResponse(response);
	System.debug('Decrypted String:'+challenge);

	return challenge;
	}

	//Method to get decrypted vaue of challenge
	
	public static String getChallengeFromResponse(HttpResponse response){

		/*
		For converting key to a blob 
		At first I had taken the byte array of the key then I took 8 bit binary equivalent of each byte and make a binary string 
		Equivalent to key data and then converted it into a equivalent hex String by taking hex equivalent of each 4 bits in binary String
		and finally converted hexString to the keyBlob

		# arrayOfChars=byte array equivalent To key 
		# keyBinary=binary String(length=128)
		# keyHex= hex equivalent of binary string(length=32)
		# keyBlob= blob from keyHex(length=16)
		*/


		//The byte array similar to keyData in java
		//Ascii codes equivalent to 'Pass12'(P=80,a=97,s=115,1=49,2=50) and 0 for making it 16 bit
		
		Integer[] arrayOfChars=new Integer[]{80, 97, 115, 115, 49, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};	

		String keyBinary='';
		for(Integer z=0;z<16;z++){
			//keyBinary is binary Equivalent of the above byteArray to convert the same to hex String
			keyBinary+=decimalToBinaryString(arrayOfChars[z]);
			System.debug('Conversion:'+arrayOfChars[z]+':'+decimalToBinaryString(arrayOfChars[z]));
		}
		System.debug(keyBinary.length());

		String keyHex='';
		for(Integer i=0;i<keyBinary.length()/4;i++){	
		    //logic to convert keyBinary to hex String			
			keyHex+= binaryToHexString(keyBinary.substring(i*4,(i+1)*4));	

		}

		System.debug('Length of hex Key:'+keyHex.length());
		
		//Private Key generation for Decryption
		Blob keyBlob=EncodingUtil.convertFromHex(keyHex);	
		System.debug('Length of key:'+keyBlob.size());


		/*

		For converting data to be decrypted to a blob First I decoded the data to a binary String
		Using base 64 decode table then I foiund hex equivalent of same and coverted hex to blob using EncodingUtil.convertFromHex(String hexString)

		# challenge=data to be decrypted
		# base64DecodedString= binary equivalent of base 64 decoded challenge
		# hexStringForData=hex equivalent of base64DecodedString (length=32)
		# dataBlob= blob from hexStringForData (length=16)

		*/


		//retreiving challenge  the encrypted Data
		String challenge=response.getHeader('x-zycus-challenge');	
		System.debug('challenge:'+challenge);	

		//base64DecodedString represents base64 decryption of encrypted String
		String base64DecodedString='';
		for(Integer i=0;i<challenge.length();i++){	
			//As challenge is Base 64 Encoded first decoding it		
			base64DecodedString+=base64DecodeTable.get(challenge.substring(i,i+1));	
		}

		System.debug('base64DecodedString:'+base64DecodedString);
		
		//Removing padding from decrypted String
		base64DecodedString=base64DecodedString.subString(0,base64DecodedString.length()-16);
		System.debug('Length of decrypted Base 64 data:'+base64DecodedString.length().format());

		//Hex String equivalent to the base64DecodedString
 		String hexStringForData='';
		for(Integer i=0;i<base64DecodedString.length()/4;i++){
			hexStringForData+= binaryToHexString(base64DecodedString.substring(i*4,(i+1)*4));
		}
		
		//data Blob to be used for data decryption
		Blob dataBlob=EncodingUtil.convertFromHex(hexStringForData);
		
		System.debug('Size of data blob'+dataBlob.size());	

		//Code to decrypt the data
		Blob decryptedChallengeBlob=Crypto.decryptWithManagedIV('AES128',keyBlob,dataBlob);
		
		String decryptedChallenge=decryptedChallengeBlob.toString();
		
		System.debug(decryptedChallenge);
		
		return decryptedChallenge;
	}

	public static HttpRequest createRequest(String endpoint,String method,String body){
		HttpRequest request = new HttpRequest();
        request.setEndpoint(endPoint);   
        request.setMethod(method);
        if(method.equalsIgnoreCase('POST'))
        {
            request.setHeader('Content-Type', 'application/json; charset=UTF-8');
            request.setBody(body);
        }
        return request;
	}


	//Method to get hex equivalent of 8 bit binary data
	public static String binaryToHexString(String binaryString){
		return binaryToHexTable.get(binaryString);
	}

	//Method to get binary Equivalent of a decimal number
	public static String decimalToBinaryString(Integer numberToConvert){

		String binary='';
		Integer remainder;
		if(numberToConvert>=0){
		while(numberToConvert > 0)
        {	
            remainder= math.mod(numberToConvert,2);          
            binary+=remainder.format();
            //System.debug('For '+numberToConvert+':'+binary);
            numberToConvert/=2;
        }
        	binary=binary.reverse();
        if(binary.length()<8){        	
        	Integer diff=8-binary.length();
        	for(Integer i=0;i<diff;i++){
        		binary='0'+binary;      
        	}
        }     
        return binary;
    	}
    	else{
    		Integer maxRange=128;
    		Integer difference=maxRange+numberToConvert;
    		String tempString='';
    		if(difference==0){
    			binary='10000000';
    		}
    		else{
    			
    			while(difference > 0){	
           			 remainder= math.mod(difference,2);          
           			 tempString+=remainder.format();
           			 difference/=2;
        		}
        		tempString=tempString.reverse();
        		Integer length=7-tempString.length();
        		if(length>0){
        			for(Integer i=0;i<length;i++){
        				tempString='0'+tempString;
        			}

        		}	
        		binary='1'+tempString;
    		}
    		
    	}
    	return binary;
	}

}

When I ran the code  I got debugged values as follow:
DEBUG|System.HttpResponse[Status=OK, StatusCode=200]
DEBUG|binary key Data:01010000011000010111001101110011001100010011001000000000000000000000000000000000000000000000000000000000000000000000000000000000
	  binary key length:128
DEBUG|Hex key :50617373313200000000000000000000 
      Hex key length:32
DEBUG|Length of key:16
DEBUG|challenge:LVgP00WwqCbCFUy70MmMdg==
base64DecodedString:001011010101100000001111110100110100010110110000101010000010011011000010000101010100110010111011110100001100100110001100011101100000000000000000
DEBUG|Length of decrypted Base 64 data:128
DEBUG|Data blob size:16
DEBUG|Decrypted challenge String
DEBUG|Decrypted String:

So I am getting decrypted value as blank
After comparison with java code I found that keyData variable  in java is having same byte array as arrayOfChars variable in apex, Also if I convert the base64DecodedString to decimal equivalents of each 8 bits in base64DecodedString I get the same byte array which I recieve in java code as below
#Apex code for converting binaryData to byte array(Integer array in apex)
		
		static Map<Integer,Integer> binaryToDecimalMap=new Map<Integer,Integer>{0=>-128,1=>64,2=>32,3=>16,4=>8,5=>4,6=>2,7=>1};
		
		
		Integer[] dataByteArray=new Integer[16];
		Integer temp;
		for(Integer i=0;i<16;i++){
			
			temp=conversionOfStringToInteger(base64DecodedString.substring(i*8,(i+1)*8));

			dataByteArray[i]=temp;
		}
		System.debug(dataByteArray) 
		
		public static Integer conversionOfStringToInteger(String binaryString){		
		Integer i=0;
		Integer sum=0;
		Integer temp;
		for(i=0;i<8;i++){
			temp=Integer.valueOf(binaryString.substring(i,i+1));			
			sum+=temp*binaryToDecimalMap.get(i);			
		}
		return sum;
		}
		
		
# JAVA code
public class AESEncryptionUtil
	{
      private static final String ALGO = "AES";
	  /**
	  encryptedData=data to be decrypted 
	  key=private key "Pass12"
	  **/
	  
       public static String decrypt(String encryptedData, String key)
       throws Exception
       {
              byte[] keyData = new byte[16];
              System.arraycopy(key.getBytes(Charset.forName("UTF-8")), 0,
                           keyData, 0, key.length());
              Key secretKey = new SecretKeySpec(keyData, "AES");
              Cipher c = Cipher.getInstance("AES");
              c.init(2, secretKey);
         
	//Byte array of base 64 data	   
    	return new String(c.doFinal(Base64.decodeBase64(encryptedData)));
              } 
	}

In above codes After running codes in apex I got dataByteArray same as the byte array which I got at line marked as "Byte array of base 64 data" on displaying the value of the code
   "Base64.decodeBase64(encryptedData)" using debug as follows
# output for apex code
 DEBUG|Byte array for base64 decoded data: (89, -93, -7, 112, 36, -63, 52, -80, 94, -28, ...)
  
 # output for java code: 
 Base64.decodeBase64(encryptedData) value
 [89, -93, -7, 112, 36, -63, 52, -80, 94, -28, 97, -33, -1, -62, 107, 14]

As both key and data are coming same like the java code what am I doing wrong ?
Is the data first encoded using base64 in the method encryptWithManagedIV?
Is there Any differnce in AES implementation of java  and apex?
Please help me with some solution to this or any other method to decrypt the encrypted string.

Thanks.