Skip to content

Instantly share code, notes, and snippets.

@dieffrei
Created December 19, 2016 20:52
Show Gist options
  • Save dieffrei/7bb4bce4e7128ed7fcf9291f964173b7 to your computer and use it in GitHub Desktop.
Save dieffrei/7bb4bce4e7128ed7fcf9291f964173b7 to your computer and use it in GitHub Desktop.
How to decrypt salesforce data on java?
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Main {
private static final String characterEncoding = "UTF-8";
private static final String cipherTransformation = "AES/CBC/PKCS5Padding";
private static final String algorithm = "AES";
public static byte[] decrypt(byte[] cipherText, byte[] key,
byte[] initialVector) throws Exception{
Cipher cipher = Cipher.getInstance(cipherTransformation);
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
cipherText = cipher.doFinal(cipherText);
return cipherText;
}
public static String encodeToBase64(String rawValue){
return Base64.getEncoder().encodeToString(rawValue.getBytes());
}
public static byte[] decodeToBase64(String base64Value){
return Base64.getDecoder().decode(base64Value);
}
public static void main(String args[]) throws Exception{
String base64Key = "YWJjZGVmZ2hpamFiY2RlZg==";
String base64Iv = "MTIzNDU2Nzg5MDEyMzQ1Ng==";
byte[] encryptedData = decodeToBase64("sNb0en7AG8Ku+ElNlQBTJA==");
byte[] clearText = decrypt(encryptedData, decodeToBase64(base64Key),
decodeToBase64(base64Iv));
System.out.println("Decrypted text:"
+ new String(clearText,characterEncoding));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment