Package dk.brics.jwig.util

Source Code of dk.brics.jwig.util.CryptoHelper

package dk.brics.jwig.util;

import dk.brics.jwig.JWIGException;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;

public class CryptoHelper {
    static {
        KeyGenerator kgen = null;
        try {
            kgen = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            throw new JWIGException(e);
        }
        kgen.init(128);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            encrypter = cipher;
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            decrypter = cipher;
        } catch (Exception e) {
            throw new JWIGException(e);
        }


    }

    private static Cipher encrypter;
    private static Cipher decrypter;

    public static String encode(String s) {
        try {
            byte[] rawBytes = encrypter.doFinal(s.getBytes());
            return String.valueOf(Base64.encode(rawBytes));
        } catch (Exception e) {
            throw new JWIGException(e);
        }
    }


    public static String decode(String s) {
        try {
            byte[] base64Decoded = Base64.decode(s);
            return new String(decrypter.doFinal(base64Decoded));
        } catch (Exception e) {
            throw new JWIGException(e);
        }
    }
}
TOP

Related Classes of dk.brics.jwig.util.CryptoHelper

TOP
Copyright © 2018 www.massapi.com. 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.