+ Start a Discussion
Dimitri DDimitri D 

Decrypt something encrypted in a Java class

Hi everybody


For a customer, I should decrypt a userid received crypted from a java class.


I have made different tests in order to try to encrypt the same string and compare both values (before trying to decode the recevied string) but unsuccessfully until now.


The java class is the following (I am not a java specialist) :



byte[] keyBytes = keyText.getBytes(); 
      // Instantiate the key (base on the keybytes and the encryption algorithm) 
SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
      // Instantiate the cipher
      Cipher cipher;
      // AES/CBC/PKCS5Padding is used to keep compatibility with other languages platform 
      cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      // Instantiate the Initialisation vector ( only based on the keybytes)  
      IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
      encrypted = cipher.doFinal(   (userId).getBytes());
      BASE64Encoder _64e = new BASE64Encoder();
      String sEncode = _64e.encode(encrypted);
      // due to problem with "+;/" the value encoded in base64 is encoded into a URL encoding
      result = URLEncoder.encode(sEncode, "UTF-8");



The AEPX code used is the following :



trigger Decrypt on Account (before update) {

	for (Account ac : trigger.new) {
		ac.Name = ac.Name.substring(0,4) + ' - ' + system.now().hour() + ':' + system.now().minute() + ':' + system.now().second();

		// Generate the data to be encrypted.
		Blob data = Blob.valueOf('EAF099');
		// Key Definition
		Blob myBlobKey	= Blob.valueof('xxxyyyzzz'); // replaced with true value
		// Encrypt the data and have Salesforce.com generate the initialization vector 
		Blob encryptedData = Crypto.encryptWithManagedIV('AES128', myBlobKey, data);
		// Update Description on Account
		ac.Description = ac.Description + ' - - - encryptedData.toString() : ' + encryptedData.toString();
		ac.Description = ac.Description + ' - - - EncodingUtil.base64Encode(encryptedData) : ' + EncodingUtil.base64Encode(encryptedData);
		// Decrypt the data
		Blob decryptedData = Crypto.decryptWithManagedIV('AES128', myBlobKey, encryptedData);
		// Update Description on Account
		ac.Description = ac.Description + '___ decryptedData.toString() : ' + decryptedData.toString();





Has anyone more experience with crypto class ?? Is this possible ??


Thanks in advance



Manos SpanoudakisManos Spanoudakis

Hi there,


I think the problem is the Init Vector which is used by the encryptWithManagedIV function. As mentioned in the docs this is random...

I would suggest using encrypt/decrypt and specify the same Init Vector as the one on the Java side.

Hope this helps





Hello Dimitri,


Did you ever figure this one out.  Any additional code samples would be much appreciated!  Thanks!