package eu.dnetlib.client;

import com.googlecode.gwt.crypto.bouncycastle.DataLengthException;
import com.googlecode.gwt.crypto.bouncycastle.InvalidCipherTextException;
import com.googlecode.gwt.crypto.client.TripleDesCipher;

/**
 * Created by stefania on 3/8/15.
 */
public class Crypto {

    public static final byte[] GWT_DES_KEY = new byte[]{
            (byte)254,(byte)65,(byte)23,(byte)198,(byte)16,(byte)5,(byte)239,(byte)45,
            (byte)111,(byte)90,(byte)8,(byte)174,(byte)39,(byte)85,(byte)216,(byte)157,};

    public static String encrypt(String stringToEncrypt) {

        String encryptedString = null;

        TripleDesCipher cipher = new TripleDesCipher();
        cipher.setKey(GWT_DES_KEY);

        try {

            encryptedString = cipher.encrypt(stringToEncrypt);

        } catch (DataLengthException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (InvalidCipherTextException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return encryptedString;
    }

    public static String decrypt(String stringToDecrypt) {

        String decryptedString = null;

        TripleDesCipher cipher = new TripleDesCipher();
        cipher.setKey(GWT_DES_KEY);

        try {

            decryptedString = cipher.decrypt(stringToDecrypt);

        } catch (DataLengthException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (InvalidCipherTextException e) {
            e.printStackTrace();
        }

        return decryptedString;
    }
}
