mvn test -Dmaven.test.failure.ignore=true -Dapp.properties.path=conf/area/sz -P profile-A
public class EncryptedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private List<String> encryptPropertyNames;
@Override
protected String convertProperty(String propertyName, String propertyValue) {
String propertyValueDecrypt = propertyValue;
if (containsProperty(propertyName)) {
propertyValueDecrypt = PropertyDecryptor.decrypt(propertyValue);
}
return convertPropertyValue(propertyValueDecrypt);
}
private boolean containsProperty(String propertyName) {
return encryptPropertyNames != null && encryptPropertyNames.contains(propertyName);
}
public void setEncryptPropertyNames(List<String> encryptPropertyNames) {
this.encryptPropertyNames = encryptPropertyNames;
}
}
public class PropertyDecryptor {
private static void checkForHex(char c) {
if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == 'A' || c == 'B' ||
c == 'C' || c == 'D' || c == 'E' || c == 'F') {
return;
}
throw new IllegalArgumentException("Expected hex character for " + Character.toString(c));
}
/**
* @param encryptedText in the form AA[11BB3344 ..] -- All hex numbers Where AA is the key that is XOR'ed againt the
* other numbers to decrypt.
* @return Encrypted string
*/
public static String decrypt(String encryptedText) {
char[] encodedText = encryptedText.toUpperCase().toCharArray();
// First Get the Key.
int key = theConv(encodedText);
char[] textString = new char[encodedText.length / 2 - 1]; // ' - 1'
// since the
// first hex
// is the key
for (int pos = 2; pos < encodedText.length; pos += 2) {
char[] hexNum = {encryptedText.charAt(pos), encryptedText.charAt(pos + 1)};
int x = (theConv(hexNum) ^ key);
char theChar = (char) x;
textString[pos / 2 - 1] = theChar;
}
return new String(textString);
}
private static int theConv(char[] c) {
// Check its Hex
checkForHex(c[0]);
checkForHex(c[1]);
Character hexStr0 = Character.toLowerCase(c[0]);
Character hexStr1 = Character.toLowerCase(c[1]);
int MSB;
int LSB;
// 48 is ASCII value of 0
// 97 is ASCII value of a
if (Character.isDigit(hexStr0)) {
MSB = (hexStr0 - 48);
}
else {
int asciiVal = hexStr0;
MSB = (asciiVal - 97 + 10);
}
if (Character.isDigit(hexStr1)) {
LSB = (hexStr1 - 48);
}
else {
int asciiVal = hexStr1;
LSB = (asciiVal - 97 + 10);
}
return (MSB * 16 + LSB);
}
public static void main(String args[]) {
System.out.println(decrypt(args[0]));
}
}