All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.accyourate.utilities.encrypter.StringEncrypter Maven / Gradle / Ivy

package com.accyourate.utilities.encrypter;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class StringEncrypter {
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";

    /**
     * Given a string returns the encrypted version in base64 using a custom cipher with the key given in acyKey
     * Use the same key for encryption or decryption or it won't work.
     *
     * @param data data
     * @param acyKey acyKey (accepts any string, it will create the key with a private method)
     * @return String
     * @throws Exception
     * exception
     */
    public static String encrypt(String data, String acyKey) throws Exception {
        SecretKey secretKey = generateKey(acyKey);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    /**
     * Given an encryption string returns the decrypted version in base64 using a custom cipher with the key given in acyKey
     * Use the same key for encryption or decryption or it won't work.
     *
     * @param encryptedData data
     * @param acyKey acyKey (accepts any string, it will create the key with a private method)
     * @return String
     * @throws Exception
     * exception
     */
    public static String decrypt(String encryptedData, String acyKey) throws Exception {
        SecretKey secretKey = generateKey(acyKey);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedBytes);
    }

    private static SecretKey generateKey(String acyKey) throws Exception {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec keySpec = new PBEKeySpec(acyKey.toCharArray(), acyKey.getBytes(), 65536, 256);
        SecretKey secretKey = new SecretKeySpec(secretKeyFactory.generateSecret(keySpec).getEncoded(), ALGORITHM);
        return secretKey;
    }

    /**
     * Given a string returns a version encrypted in MD5.
     *
     * @param input text input
     * @return String
     * exception
     */
    public static String encryptMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            byte[] digest = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy