use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.
the class DynamicBasicAuthorizationHeader method setup.
@Override
public void setup(String target, AdaptrisMessage msg, ResourceTargetMatcher auth) throws CoreException {
try {
String username = Args.notBlank(msg.resolve(getUsername()), "username");
String password = Args.notBlank(msg.resolve(getPassword()), "password");
String encoded = new Base64ByteTranslator().translate(String.format("%s:%s", username, Password.decode(password)).getBytes("UTF-8"));
authHeader = String.format("Basic %s", encoded);
} catch (UnsupportedEncodingException | IllegalArgumentException | PasswordException e) {
throw ExceptionHelper.wrapCoreException(e);
}
}
use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.
the class AesCrypto method decode.
@Override
@SuppressWarnings({ "lgtm [java/weak-cryptographic-algorithm]" })
public String decode(String encrypted, String charset) throws PasswordException {
String encryptedString = encrypted;
String result;
if (encrypted.startsWith(PORTABLE_PASSWORD)) {
encryptedString = encrypted.substring(PORTABLE_PASSWORD.length());
}
try {
Input input = new Input(encryptedString);
input.read();
SecretKey sessionKey = new SecretKeySpec(input.getSessionKey(), ALG);
Cipher cipher = Cipher.getInstance(CIPHER);
if (input.getSessionVector() != null) {
IvParameterSpec spec = new IvParameterSpec(input.getSessionVector());
cipher.init(Cipher.DECRYPT_MODE, sessionKey, spec);
} else {
cipher.init(Cipher.DECRYPT_MODE, sessionKey);
}
byte[] decrypted = cipher.doFinal(input.getEncryptedData());
result = unseed(decrypted, charset);
} catch (Exception e) {
throw new PasswordException(e);
}
return result;
}
use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.
the class AesGcmCrypto method encode.
@Override
public String encode(String plainText, String charset) throws PasswordException {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(KEY_LENGTH);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(CIPHER);
byte[] iv = new byte[IV_LENGTH];
SECURE_RAND.nextBytes(iv);
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH, iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, spec);
byte[] bytes = plainText.getBytes(getEncodingToUse(charset));
byte[] key = secretKey.getEncoded();
byte[] cipherText = new byte[iv.length + key.length + cipher.getOutputSize(bytes.length)];
int i = 0;
for (byte v : iv) {
cipherText[i++] = v;
}
for (byte k : key) {
cipherText[i++] = k;
}
cipher.doFinal(bytes, 0, bytes.length, cipherText, i);
secretKey.getEncoded();
return PORTABLE_PASSWORD_2 + base64.translate(cipherText);
} catch (Exception e) {
throw new PasswordException(e);
}
}
use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.
the class AesGcmCrypto method decode.
@Override
public String decode(String b64CipherText, String charset) throws PasswordException {
if (b64CipherText.startsWith(PORTABLE_PASSWORD_2)) {
b64CipherText = b64CipherText.substring(PORTABLE_PASSWORD_2.length());
}
try {
byte[] cipherText = base64.translate(b64CipherText);
byte[] iv = Arrays.copyOfRange(cipherText, 0, IV_LENGTH);
byte[] key = Arrays.copyOfRange(cipherText, IV_LENGTH, IV_LENGTH + KEY_SIZE);
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER);
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
int offset = iv.length + key.length;
byte[] plaintext = cipher.doFinal(cipherText, offset, cipherText.length - offset);
return new String(plaintext, getEncodingToUse(charset));
} catch (Exception e) {
throw new PasswordException(e);
}
}
use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.
the class MicrosoftCrypto method encode.
public String encode(String plainText, String charset) throws PasswordException {
byte[] encryptedBody = new byte[0];
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, getCertificate());
encryptedBody = cipher.doFinal(plainText.getBytes(getEncodingToUse(charset)));
} catch (Exception e) {
throw new PasswordException(e);
}
return MSCAPI_STYLE + base64.translate(encryptedBody);
}
Aggregations