public static String encrypt3DES(String data, String key, String iv) {
byte[] decodedIv = Base64.getDecoder().decode(iv);
byte[] decodedKey = Base64.getDecoder().decode(key);
IvParameterSpec ivSpec = new IvParameterSpec(decodedIv);
SecretKey secretKey = new SecretKeySpec(decodedKey, "DESede");
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
byte[] plainTextBytes = data.getBytes(StandardCharsets.UTF_8);
byte[] encryptedBytes = cipher.doFinal(plainTextBytes);
byte[] base64Bytes = Base64.getEncoder().encode(encryptedBytes);
return new String(base64Bytes);
throw new RuntimeException(e);
public static String decrypt3DES(String data, String key, String iv) {
byte[] decodedIv = Base64.getDecoder().decode(iv);
byte[] decodedKey = Base64.getDecoder().decode(key);
IvParameterSpec ivSpec = new IvParameterSpec(decodedIv);
SecretKey secretKey = new SecretKeySpec(decodedKey, "DESede");
byte[] encryptedBytes = Base64.getDecoder().decode(data);
Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
byte[] plainText = decipher.doFinal(encryptedBytes);
return new String(plainText, StandardCharsets.UTF_8);
} catch (IllegalBlockSizeException | NoSuchPaddingException | NoSuchAlgorithmException |
InvalidAlgorithmParameterException | InvalidKeyException | BadPaddingException e) {
throw new RuntimeException(e);
public static String generateInitializationVector() {
byte[] randomBytes = new byte[8];
new Random().nextBytes(randomBytes);
return new String(Base64.getEncoder().encode(randomBytes));
public static String sample() {
String decryptionKey = "DECRYPTION_KEY";
String encryptionKey = "ENCRYPTION_KEY";
String companyHash = "COMPANY_HASH";
String iv = generateInitializationVector();
String action = "GENERATE_USER_HASH"; // GENERATE_USER_HASH || GENERATE_USER_HASH_FORCE_USER || INACTIVATE_USER
String userId = "USER_ID";
String username = "USERNAME";
String startService = "START_SERVICE";
// Only for MESSENGER actions and GENERATE_USER_HASH_FORCE_USER
String uniqueDocType = "UNIQUE_DOC_TYPE";
String uniqueDocValue = "UNIQUE_DOC_VALUE";
// Only for MESSENGER_ALERT_COUNTER and MESSENGER_REQUESTS actions
String messengerFilters = "{";
messengerFilters+="\"DepartmentAlias\": \"DEPARTMENT_ALIAS\",";
messengerFilters+="\"ChannelAlias\": \"CHANNEL_ALIAS\",";
messengerFilters+="\"DataIni\": \"START_DATE\","; // dd/MM/yyyy
messengerFilters+="\"DataFim\": \"END_DATE\","; // dd/MM/yyyy
messengerFilters+="\"Status\": \"STATUS\","; // A | E
messengerFilters+="\"Pendentes\": \"PENDENTES\""; // S | N
String encryptedAction = encrypt3DES(action, encryptionKey, iv);
String encryptedUserId = encrypt3DES(userId, encryptionKey, iv);
String encryptedUserName = encrypt3DES(username, encryptionKey, iv);
String encryptedStartService = encrypt3DES(startService, encryptionKey, iv);
String encryptedUniqueDocType = encrypt3DES(uniqueDocType, encryptionKey, iv);
String encryptedUniqueDocValue = encrypt3DES(uniqueDocValue, encryptionKey, iv);
String encryptedMessengerFilters = encrypt3DES(messengerFilters, encryptionKey, iv);
String postBody = "HASH=" + URLEncoder.encode(companyHash, StandardCharsets.UTF_8);
postBody += "&ANNAEXEC=" + URLEncoder.encode(iv, StandardCharsets.UTF_8);
postBody += "&ACTION=" + URLEncoder.encode(encryptedAction, StandardCharsets.UTF_8);
postBody += "&USER_ID=" + URLEncoder.encode(encryptedUserId, StandardCharsets.UTF_8);
postBody += "&USER_NAME=" + URLEncoder.encode(encryptedUserName, StandardCharsets.UTF_8);
postBody += "&START_SERVICE=" + URLEncoder.encode(encryptedStartService, StandardCharsets.UTF_8);
postBody += "&MESSENGER_UNIQUE_DOC_TYPE=" + URLEncoder.encode(encryptedUniqueDocType, StandardCharsets.UTF_8);
postBody += "&MESSENGER_UNIQUE_DOC_VALUE=" + URLEncoder.encode(encryptedUniqueDocValue, StandardCharsets.UTF_8);
postBody += "&MESSENGER_FILTER=" + URLEncoder.encode(encryptedMessengerFilters, StandardCharsets.UTF_8);
String environmentUrl = "http://YOUR_ANNA_URL";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(environmentUrl + "/aannacorporateuser.aspx"))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(postBody))
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseFromServer = response.body();
if (responseFromServer.contains(iv)) {
String newEncryptedIv = responseFromServer.substring(responseFromServer.indexOf(iv));
newEncryptedIv = newEncryptedIv.replace(iv, "");
String encryptedResponse = responseFromServer.substring(0, responseFromServer.indexOf(iv));
String newIv = decrypt3DES(newEncryptedIv, encryptionKey, iv);
return decrypt3DES(encryptedResponse, decryptionKey, newIv);
return responseFromServer;
throw new RuntimeException(e);