tk.playerforcehd.networklib.shared.utils.StringCryptionUtils.java Source code

Java tutorial

Introduction

Here is the source code for tk.playerforcehd.networklib.shared.utils.StringCryptionUtils.java

Source

/*
 *     NetworkLib - A Spigot/BungeeCord plugin messaging Library
 *     Copyright (C) 2015 Pascal Zarrad
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License along
 *     with this program; if not, write to the Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package tk.playerforcehd.networklib.shared.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Give the ability to encrypt Strings
 *
 * @author PlayerForceHD
 */
public class StringCryptionUtils {

    /**
     * Encrypt a String with AES | You MUST use a 128 bit key!
     *
     * @param value The String to encrypt
     * @param key   The key for the encrypted String
     * @return The encrypted String
     * @throws InvalidAlgorithmParameterException -
     * @throws InvalidKeyException                -
     * @throws NoSuchPaddingException             -
     * @throws NoSuchAlgorithmException           -
     * @throws UnsupportedEncodingException       -
     * @throws BadPaddingException                -
     * @throws IllegalBlockSizeException          -
     */
    public static String encrypt(String value, String key)
            throws InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException,
            NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        IvParameterSpec iv = new IvParameterSpec("9rh5os4n8m24gu9e".getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        return Base64.encodeBase64String(encrypted);
    }

    /**
     * Decrypt a String which is encrypted with AES | You MUST use a 128 bit key!
     *
     * @param value The String to decrypt
     * @param key   The key for the decryption
     * @return The decrypted String
     * @throws InvalidAlgorithmParameterException -
     * @throws InvalidKeyException                -
     * @throws NoSuchPaddingException             -
     * @throws NoSuchAlgorithmException           -
     * @throws UnsupportedEncodingException       -
     * @throws BadPaddingException                -
     * @throws IllegalBlockSizeException          -
     */
    public static String decrypt(String value, String key)
            throws InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException,
            NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        IvParameterSpec iv = new IvParameterSpec("9rh5os4n8m24gu9e".getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(Base64.decodeBase64(value));
        return new String(original);
    }

}