use of com.adaptris.security.exc.EncryptException in project interlok by adaptris.
the class StdSecurityService method encrypt.
/**
* @see SecurityService#encrypt(byte[], Alias, Alias)
*/
public Output encrypt(byte[] payload, Alias sender, Alias receiver) throws AdaptrisSecurityException {
PrivateKey us = null;
Output output = null;
if (alg == null) {
throw new EncryptException("Encryption requires an " + "EncryptionAlgorithm object");
}
us = getPrivateKey(sender.getAlias(), sender.getAliasPassword());
CertificateHandler them = createCertificateHandler(getCertificate(receiver.getAlias()));
output = encrypt(payload, us, them);
return output;
}
use of com.adaptris.security.exc.EncryptException in project interlok by adaptris.
the class StdSecurityService method sign.
/**
* @see SecurityService#sign(byte[], Alias, Output)
*/
public Output sign(byte[] payload, Alias us, Output output) throws AdaptrisSecurityException {
PrivateKey pk = null;
StdOutput target = null;
CertificateHandler ch = null;
try {
target = output == null ? new StdOutput(Output.PLAIN) : (StdOutput) output;
target.setType(target.getType() | Output.SIGNED);
} catch (ClassCastException e) {
if (output != null)
throw new EncryptException("Class " + output.getClass() + " not recognised", e);
else
throw new EncryptException("Output null, therefore not recognised", e);
}
pk = getPrivateKey(us.getAlias(), us.getAliasPassword());
ch = createCertificateHandler(getCertificate(us.getAlias()));
try {
Signature sig = getSignatureInstance(ch);
sig.initSign(pk, SecurityUtil.getSecureRandom());
sig.update(payload);
target.setSignature(sig.sign());
target.setDecryptedData(payload);
} catch (Exception e) {
throw new EncryptException(e);
}
return target;
}
use of com.adaptris.security.exc.EncryptException in project interlok by adaptris.
the class StdOutput method formatBase64.
/**
* Return the encrypted message ready for immediate writing to file.
*
* @return the bytes ready for writing.
* @throws AdaptrisSecurityException if an error occurs
*/
private byte[] formatBase64() throws EncryptException {
DataOutputStream out = null;
ByteArrayOutputStream byteStream = null;
byte[] returnBytes = null;
try {
byteStream = new ByteArrayOutputStream();
out = new DataOutputStream(byteStream);
write(out, getSessionVector());
write(out, getSessionKey());
write(out, getEncryptedData(false) == null ? getDecryptedData(false) : getEncryptedData(false));
write(out, getSignature());
returnBytes = Base64.encodeBase64(byteStream.toByteArray());
} catch (Exception e) {
throw new EncryptException(e);
} finally {
try {
if (out != null) {
out.close();
}
if (byteStream != null) {
byteStream.close();
}
} catch (Exception ignored) {
;
}
}
return returnBytes;
}
use of com.adaptris.security.exc.EncryptException in project interlok by adaptris.
the class StdSecurityService method encrypt.
private Output encrypt(byte[] payload, PrivateKey pk, CertificateHandler ch) throws AdaptrisSecurityException {
StdOutput output = new StdOutput(Output.ENCRYPTED);
try {
KeyGenerator kg = KeyGenerator.getInstance(getCipherName(alg.getAlgorithm()));
kg.init(alg.getKeyLength(), SecurityUtil.getSecureRandom());
SecretKey sessionKey = kg.generateKey();
Cipher dataCipher = Cipher.getInstance(alg.getAlgorithm());
/*,
Constants.SECURITY_PROVIDER);*/
dataCipher.init(Cipher.ENCRYPT_MODE, sessionKey);
byte[] encryptedBody = dataCipher.doFinal(payload);
Cipher keyCipher = Cipher.getInstance(ch.getKeyAlgorithm());
/*,
Constants.SECURITY_PROVIDER);*/
keyCipher.init(Cipher.ENCRYPT_MODE, ch.getPublicKey(), SecurityUtil.getSecureRandom());
byte[] encryptedSessionKey = keyCipher.doFinal(sessionKey.getEncoded());
output.setSessionKey(encryptedSessionKey);
output.setSessionVector(dataCipher.getIV());
output.setEncryptedData(encryptedBody);
} catch (Exception e) {
throw new EncryptException(e);
}
return output;
}
Aggregations