use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class AbstractEncryptionProvider method encrypt.
@Override
public EncryptedSecret encrypt(final byte[] secret, final String keyReference) {
try {
final Cipher cipher = this.getCipher();
cipher.init(Cipher.ENCRYPT_MODE, this.getSecretEncryptionKey(keyReference, Cipher.ENCRYPT_MODE), this.getAlgorithmParameterSpec());
return new EncryptedSecret(this.getType(), cipher.doFinal(secret));
} catch (final Exception e) {
throw new EncrypterException("Could not encrypt secret with keyReference " + keyReference, e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class CertificateHelper method createPrivateKey.
private static PrivateKey createPrivateKey(final byte[] key, final String algorithm, final String provider) {
try {
final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory privateKeyFactory;
privateKeyFactory = KeyFactory.getInstance(algorithm, provider);
return privateKeyFactory.generatePrivate(privateKeySpec);
} catch (final GeneralSecurityException e) {
throw new EncrypterException(String.format("Security exception creating private key for algorithm \"%s\" by provider \"%s\"", algorithm, provider), e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class RsaEncrypter method setPrivateKeyStore.
public void setPrivateKeyStore(final File privateKeyStoreFile) {
try {
final byte[] keyData = Files.readAllBytes(privateKeyStoreFile.toPath());
final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyData);
this.privateKey = KeyFactory.getInstance(ALG).generatePrivate(privateKeySpec);
} catch (final NoSuchAlgorithmException | IOException | InvalidKeySpecException e) {
throw new EncrypterException("Could not get cipher", e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class RsaEncrypter method encrypt.
public byte[] encrypt(final byte[] secret) {
if (secret == null) {
throw new IllegalArgumentException("Can not encrypt NULL value");
}
try {
final Cipher cipher = this.getCipher();
cipher.init(Cipher.ENCRYPT_MODE, this.getSecretEncryptionKey(Cipher.ENCRYPT_MODE));
return cipher.doFinal(secret);
} catch (final Exception e) {
throw new EncrypterException("Could not encrypt secret", e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class JreEncryptionProvider method generateAes128BitsSecret.
@Override
public byte[] generateAes128BitsSecret(final String keyReference) {
try {
final KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(KEY_LENGTH * 8);
return this.encrypt(keyGenerator.generateKey().getEncoded(), keyReference).getSecret();
} catch (final NoSuchAlgorithmException exc) {
throw new EncrypterException("Could not generate secret", exc);
}
}
Aggregations