Search in sources :

Example 1 with EncrypterException

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);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Cipher(javax.crypto.Cipher) EncryptedSecret(org.opensmartgridplatform.shared.security.EncryptedSecret) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 2 with EncrypterException

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);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) KeyFactory(java.security.KeyFactory)

Example 3 with EncrypterException

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);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 4 with EncrypterException

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);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 5 with EncrypterException

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);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)20 IOException (java.io.IOException)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 Cipher (javax.crypto.Cipher)4 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)4 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)4 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)3 UnknownHostException (java.net.UnknownHostException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 KeyFactory (java.security.KeyFactory)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 Test (org.junit.jupiter.api.Test)2 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)2 SetKeysRequestDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto)2 TechnicalException (org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)2 ArrayList (java.util.ArrayList)1 KeyGenerator (javax.crypto.KeyGenerator)1