/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.core.common.security;

import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.openengsb.core.api.security.DecryptionException;
import org.openengsb.core.api.security.EncryptionException;

public final class CipherUtils {

    public static final String DEFAULT_SIGN_ALGORITHM = "SHA1withRSA";
    public static final String DEFAULT_SYMMETRIC_ALGORITHM = "AES";
    public static final int DEFAULT_SYMMETRIC_KEYSIZE = 128;
    public static final String DEFAULT_ASYMMETRIC_ALGORITHM = "RSA";
    public static final int DEFAULT_ASYMMETRIC_KEYSIZE = 2048;

    public static byte[] decrypt(byte[] text, Key key) throws DecryptionException {
        return decrypt(text, key, key.getAlgorithm());
    }

    public static byte[] decrypt(byte[] text, Key key, String algorithm) throws DecryptionException {
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, key);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }
        try {
            return cipher.doFinal(text);
        } catch (GeneralSecurityException e) {
            throw new DecryptionException(e);
        }
    }

    public static byte[] encrypt(byte[] text, Key key) throws EncryptionException {
        return encrypt(text, key, key.getAlgorithm());
    }

    public static byte[] encrypt(byte[] text, Key key, String algorithm) throws EncryptionException {
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, key);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }
        try {
            return cipher.doFinal(text);
        } catch (GeneralSecurityException e) {
            throw new EncryptionException(e);
        }
    }

    public static PublicKey deserializePublicKey(byte[] keyData, String algorithm) {
        X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(keyData);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            return keyFactory.generatePublic(pubSpec);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static PrivateKey deserializePrivateKey(byte[] keyData, String algorithm) {
        PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(keyData);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            return keyFactory.generatePrivate(privSpec);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static SecretKey deserializeSecretKey(byte[] keyData, String algorithm) {
        return new SecretKeySpec(keyData, algorithm);
    }

    public static KeyPair generateKeyPair(String algorithm, int keySize) {
        KeyPairGenerator keyPairGenerator;
        try {
            keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
        keyPairGenerator.initialize(keySize);
        return keyPairGenerator.generateKeyPair();
    }

    public static SecretKey generateKey(String algorithm, int keySize) {
        KeyGenerator secretKeyGenerator;
        try {
            secretKeyGenerator = KeyGenerator.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
        secretKeyGenerator.init(keySize);
        return secretKeyGenerator.generateKey();
    }

    public static byte[] sign(byte[] text, PrivateKey key, String algorithm) throws SignatureException {
        Signature signature;
        try {
            signature = Signature.getInstance(algorithm);
            signature.initSign(key);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }
        signature.update(text);
        return signature.sign();
    }

    public static boolean verify(byte[] text, byte[] signatureValue, PublicKey key, String algorithm)
        throws SignatureException {
        Signature signature;
        try {
            signature = Signature.getInstance(algorithm);
            signature.initVerify(key);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }

        signature.update(text);
        return signature.verify(signatureValue);
    }

    private CipherUtils() {
    }

}

@@ -2418,36 +2418,94 @@
 gumentException(
+%22unable to initialize cipher for algorithm %22 + algorithm, 
 e);%0A
-
         %7D%0A      
@@ -2634,32 +2634,87 @@
 yptionException(
+%22unable to decrypt data using algorithm %22 + algorithm, 
 e);%0A        %7D%0A  
@@ -3192,32 +3192,90 @@
 gumentException(
+%22unable to initialize cipher for algorithm %22 + algorithm, 
 e);%0A        %7D%0A  
@@ -3408,32 +3408,87 @@
 yptionException(
+%22unable to encrypt data using algorithm %22 + algorithm, 
 e);%0A        %7D%0A  
@@ -3873,32 +3873,134 @@
 gumentException(
+%22provided data could not be converted to a PublicKey for algorithm %22%0A                    + algorithm, 
 e);%0A        %7D%0A  
@@ -4392,32 +4392,135 @@
 gumentException(
+%22provided data could not be converted to a PrivateKey for algorithm %22%0A                    + algorithm, 
 e);%0A        %7D%0A  
@@ -5819,32 +5819,90 @@
 gumentException(
+%22cannot initialize signature for algorithm %22 + algorithm, 
 e);%0A        %7D%0A  
@@ -6335,32 +6335,90 @@
 gumentException(
+%22cannot initialize signature for algorithm %22 + algorithm, 
 e);%0A        %7D%0A%0A 
@@ -6411,25 +6411,24 @@
 ;%0A        %7D%0A
-%0A
         sign

improve exception-messages in CipherUtils