Package org.rat.free.security.makifx.common.exception

Examples of org.rat.free.security.makifx.common.exception.BaseApplicationException


                dir_created.toFile().mkdirs();
                return FileVisitResult.CONTINUE;
            } catch (Exception exc) {
                UtilityFX.warning("Exception EXC:" + exc.toString());
                exc.printStackTrace();
                exception = new BaseApplicationException(exc.toString()).setException(exc);
                return FileVisitResult.TERMINATE;
            }
        }
View Full Code Here


    @SuppressWarnings("UseSpecificCatch")
    public static void encryptFile(String pass, File file, String input) throws BaseApplicationException {

        if (pass == null) {
            throw new BaseApplicationException("Null Password invalid.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        if (pass.length() == 0) {
            throw new BaseApplicationException("Ivalid Password lenght.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        FileOutputStream fout = null;
        ByteArrayInputStream bais = null;
        CipherOutputStream out = null;

        try {

            char[] password = pass.toCharArray();
            byte[] salt = salt();

            SecretKeyFactory skf = SecretKeyFactory.getInstance(__SECRETKEY_MODE);
            KeySpec ks = new PBEKeySpec(password, salt, ITER_SAVE_FILE, LEN_DEFAULT_CIP);
            SecretKey tmp = skf.generateSecret(ks);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), __KEY_SPEC);

            Cipher cipher = Cipher.getInstance(__CIP_MODE);
            cipher.init(Cipher.ENCRYPT_MODE, secret);

            AlgorithmParameters p = cipher.getParameters();
            byte[] iv = p.getParameterSpec(IvParameterSpec.class).getIV();

            fout = new FileOutputStream(file);
            bais = new ByteArrayInputStream(Utility.getUTF8Bytes(input));
            out = new CipherOutputStream(fout, cipher);

            byte[] buffer = new byte[1024];

            int n = 0;

            fout.write(salt, 0, salt.length);
            fout.flush();

            fout.write(iv, 0, iv.length);
            fout.flush();

            while ((n = bais.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
                out.flush();
            }

        } catch (Exception exc) {
            System.err.println("Exception -encryptFile- exc:" + exc);
            exc.printStackTrace();
            throw new BaseApplicationException("Encrypt Error!").setException(exc);
        } finally {

            if (out != null) {
                try {
                    out.flush();
View Full Code Here

    @SuppressWarnings("UseSpecificCatch")
    public static String decryptFileDefault(String pass, File file) throws BaseApplicationException {

        if (pass == null) {
            throw new BaseApplicationException("Null Password invalid.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        if (pass.length() == 0) {
            throw new BaseApplicationException("Ivalid Password lenght.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        FileInputStream in = null;
        CipherInputStream ccin = null;

        try {

            char[] password = pass.toCharArray();

            in = new FileInputStream(file);
            byte[] buffer_salt_in = new byte[DEFAULT_SALT_HMAC_SIZE];
            byte[] buffer_iv_in = new byte[LEN_DEFAULT_CIP / 8];
            in.read(buffer_salt_in);
            in.read(buffer_iv_in);

            SecretKeyFactory skf = SecretKeyFactory.getInstance(__SECRETKEY_MODE);
            KeySpec spec_in = new PBEKeySpec(password, buffer_salt_in, ITER_SAVE_FILE, LEN_DEFAULT_CIP);
            SecretKey tmp_in = skf.generateSecret(spec_in);
            SecretKey sk = new SecretKeySpec(tmp_in.getEncoded(), __KEY_SPEC);

            Cipher cipher = Cipher.getInstance(__CIP_MODE);
            cipher.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(buffer_iv_in));

            ccin = new CipherInputStream(in, cipher);

            StringBuilder o = new StringBuilder();

            int n = 0;
            byte[] buffer = new byte[1024];
            while ((n = ccin.read(buffer)) >= 0) {
                o.append(new String(buffer, DEFAULT_CHARSET), 0, n);
            }

            return o.toString();

        } catch (Exception exc) {
            System.err.println("Exception -decryptFileDefault- exc:" + exc);
            exc.printStackTrace();
            throw new BaseApplicationException("Decrypt Error!").setException(exc);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception ex) {
View Full Code Here

    @SuppressWarnings("UseSpecificCatch")
    public static synchronized byte[] encryptStream(String pass, byte[] input) throws BaseApplicationException {

        if (pass == null) {
            throw new BaseApplicationException("Null Password invalid.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        if (pass.length() == 0) {
            throw new BaseApplicationException("Ivalid Password lenght.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        byte[] result = null;

        if (input != null && input.length > 0) {
            try {

                char[] password = pass.toCharArray();
                byte[] salt = salt();

                SecretKeyFactory skf = SecretKeyFactory.getInstance(__SECRETKEY_MODE);
                KeySpec ks = new PBEKeySpec(password, salt, ITER_SAVE_FILE, LEN_DEFAULT_CIP);
                SecretKey tmp = skf.generateSecret(ks);
                SecretKey secret = new SecretKeySpec(tmp.getEncoded(), __KEY_SPEC);

                byte[] iv = new byte[LEN_DEFAULT_CIP / 8];
                SecureRandom secure_random = SecureRandom.getInstance("SHA1PRNG");
                secure_random.nextBytes(iv);

                Cipher cipher = Cipher.getInstance(__CIP_MODE);
                cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));

                byte[] enc = cipher.doFinal(input);

                result = Utility.concatenateByte(salt, Utility.concatenateByte(iv, enc));

            } catch (Exception exc) {
                System.err.println("Exception -encryptStream- exc:" + exc);
                exc.printStackTrace();
                throw new BaseApplicationException("Encrypt Error!").setException(exc);
            }
        }

        return result;
    }
View Full Code Here

    @SuppressWarnings("UseSpecificCatch")
    public static synchronized byte[] decryptStream(String pass, byte[] input) throws BaseApplicationException {

        if (pass == null) {
            throw new BaseApplicationException("Null Password invalid.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        if (pass.length() == 0) {
            throw new BaseApplicationException("Ivalid Password lenght.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }

        byte[] result = null;

        if (input != null && input.length > 0) {
            try {

                char[] password = pass.toCharArray();

                byte[] salt = Arrays.copyOfRange(input, 0, DEFAULT_SALT_HMAC_SIZE);
                byte[] iv = Arrays.copyOfRange(input, salt.length, salt.length + (LEN_DEFAULT_CIP / 8));

                SecretKeyFactory skf = SecretKeyFactory.getInstance(__SECRETKEY_MODE);
                KeySpec spec_in = new PBEKeySpec(password, salt, ITER_SAVE_FILE, LEN_DEFAULT_CIP);
                SecretKey tmp_in = skf.generateSecret(spec_in);
                SecretKey sk = new SecretKeySpec(tmp_in.getEncoded(), __KEY_SPEC);

                Cipher cipher = Cipher.getInstance(__CIP_MODE);
                cipher.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(iv));
                result = cipher.doFinal(Arrays.copyOfRange(input, salt.length + iv.length, input.length));

            } catch (Exception exc) {
                System.err.println("Exception -decryptFileDefault- exc:" + exc);
                exc.printStackTrace();
                throw new BaseApplicationException("Decrypt Error!").setException(exc);
            }
        }

        return result;
    }
View Full Code Here

    @SuppressWarnings("UseSpecificCatch")
    public static void encryptFileData(String pass, File file, String input, String log) throws BaseApplicationException {

        if (pass == null) {
            throw new BaseApplicationException("Null Password invalid.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }
        if (pass.length() == 0) {
            throw new BaseApplicationException("Ivalid Password lenght.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }
        if (pass.length() > 16) {
            pass = pass.substring(0, 16);
        }

        try {
            pass = appendCPassData(pass);
        } catch (Exception exc) {
            throw new BaseApplicationException("Key Error!")
                    .setException(exc);
        }

        try {
            byte[] password = Utility.getUTF8Bytes(pass);
            ByteArrayInputStream secret
                    = new ByteArrayInputStream(Utility.getUTF8Bytes(input));

            OutputStream out = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            byte[] buff_of_iv_data = new byte[16];
            SecureRandom sc_r = new SecureRandom();
            sc_r.nextBytes(buff_of_iv_data);
            out.write(buff_of_iv_data);
            out.flush();

            SecretKeySpec sks = new SecretKeySpec(password, __KEY_SPEC);
            IvParameterSpec is = new IvParameterSpec(buff_of_iv_data);

            Cipher c = Cipher.getInstance(__CIP_MODE);
            c.init(Cipher.ENCRYPT_MODE, sks, is);

            out = new CipherOutputStream(out, c);

            @SuppressWarnings("UnusedAssignment")
            int n = 0;
            while ((n = secret.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            out.flush();
            out.close();
        } catch (Exception exc) {
            throw new BaseApplicationException("Encrypt Error!").setException(exc);
        }
    }
View Full Code Here

    public static String decryptFile(String pass, File file, String log) throws BaseApplicationException {

        try {
            pass = appendCPassData(pass);
        } catch (Exception exc) {
            throw new BaseApplicationException("Key Error!")
                    .setException(exc);
        }

        CipherInputStream ccin = null;

        try {

            StringBuilder o = new StringBuilder();

            InputStream in = new FileInputStream(file);
            byte[] password = Utility.getUTF8Bytes(pass);
            byte[] buffer = new byte[1024];
            byte[] buff_of_iv_data = new byte[16];
            in.read(buff_of_iv_data);

            SecretKeySpec sks = new SecretKeySpec(password, __KEY_SPEC);
            IvParameterSpec is = new IvParameterSpec(buff_of_iv_data);

            Cipher c = Cipher.getInstance(__CIP_MODE);
            c.init(Cipher.DECRYPT_MODE, sks, is);
            ccin = new CipherInputStream(in, c);

            @SuppressWarnings("UnusedAssignment")
            int n = 0;

            while ((n = ccin.read(buffer)) >= 0) {
                o.append(new String(buffer, DEFAULT_CHARSET), 0, n);
            }

            return o.toString();
        } catch (Exception exc) {
            throw new BaseApplicationException("Decrypt Error!").setException(exc);
        } finally {
            if (ccin != null) {
                try {
                    ccin.close();
                } catch (Exception ex) {
View Full Code Here

    @SuppressWarnings("UseSpecificCatch")
    public static void encryptFile(String pass, File file, String input) throws BaseApplicationException {

        if (pass == null) {
            throw new BaseApplicationException("Null Password invalid.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }
        if (pass.length() == 0) {
            throw new BaseApplicationException("Ivalid Password lenght.")
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        }
        if (pass.length() > 16) {
            pass = pass.substring(0, 16);
        }

        try {
            pass = appendCPassData(pass);
        } catch (Exception exc) {
            throw new BaseApplicationException("Key Error!")
                    .setException(exc);
        }

        try {
            byte[] password = Utility.getUTF8Bytes(pass);
            ByteArrayInputStream secret
                    = new ByteArrayInputStream(Utility.getUTF8Bytes(input));

            OutputStream out = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            byte[] buff_of_iv_data = new byte[16];
            SecureRandom sc_r = new SecureRandom();
            sc_r.nextBytes(buff_of_iv_data);
            out.write(buff_of_iv_data);
            out.flush();

            SecretKeySpec sks = new SecretKeySpec(password, __KEY_SPEC);
            IvParameterSpec is = new IvParameterSpec(buff_of_iv_data);

            Cipher c = Cipher.getInstance(__CIP_MODE);
            c.init(Cipher.ENCRYPT_MODE, sks, is);

            out = new CipherOutputStream(out, c);

            @SuppressWarnings("UnusedAssignment")
            int n = 0;
            while ((n = secret.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            out.flush();
            out.close();
        } catch (Exception exc) {
            System.err.println("Exception -encrypt- exc:" + exc);
            exc.printStackTrace();
            throw new BaseApplicationException("Encrypt Error!")
                    .setException(exc);
        }
    }
View Full Code Here

    public static String decryptFile(String pass, File file) throws BaseApplicationException {

        try {
            pass = appendCPassData(pass);
        } catch (Exception exc) {
            throw new BaseApplicationException("Key Error!")
                    .setException(exc);
        }

        try {

            StringBuilder o = new StringBuilder();

            InputStream in = new FileInputStream(file);
            byte[] password = Utility.getUTF8Bytes(pass);
            byte[] buffer = new byte[1024];
            byte[] buff_of_iv_data = new byte[16];
            in.read(buff_of_iv_data);

            SecretKeySpec sks = new SecretKeySpec(password, __KEY_SPEC);
            IvParameterSpec is = new IvParameterSpec(buff_of_iv_data);

            Cipher c = Cipher.getInstance(__CIP_MODE);
            c.init(Cipher.DECRYPT_MODE, sks, is);
            CipherInputStream ccin = new CipherInputStream(in, c);

            @SuppressWarnings("UnusedAssignment")
            int n = 0;

            while ((n = ccin.read(buffer)) >= 0) {
                o.append(new String(buffer, DEFAULT_CHARSET), 0, n);
            }

            return o.toString();
        } catch (Exception exc) {
            System.err.println("Exception -decrypt- exc:" + exc);
            exc.printStackTrace();
            throw new BaseApplicationException("Decrypt Error!")
                    .setException(exc);
        }
    }
View Full Code Here

            m.setProperty(Marshaller.JAXB_ENCODING, DEFAULT_CHARSET);
            str_writer = new StringWriter();
            m.marshal(this, str_writer);
            return str_writer;
        } catch (Exception exc) {
            throw new BaseApplicationException(
                    "Marshal Error")
                    .setException(exc)
                    .setErrorType(BaseApplicationException.ErrorType.FATAL);
        } finally {
            if (str_writer != null) {
View Full Code Here

TOP

Related Classes of org.rat.free.security.makifx.common.exception.BaseApplicationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.