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

bi.deep.EncryptionUtils Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package bi.deep;

import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class EncryptionUtils {
    public static final String base64key = "vJ4EiIZCytyU0EHYrOFCCm6JSEn8zSoab6mf3FiZg1w=";

    public static String encode64(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static byte[] decode64(String str) {
        return Base64.getDecoder().decode(str);
    }

    public static SecretKey getSecretKey() {
        return new SecretKeySpec(decode64(base64key), "AES");
    }

    public static SecretKey generateAESKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(256);
        return keyGenerator.generateKey();
    }

    public static byte[] encrypt(String data, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
    }

    public static String encryptBase64(String data, SecretKey secretKey) throws Exception {
        return encode64(encrypt(data, secretKey));
    }

    public static String decrypt(byte[] encryptedData, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(encryptedData);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static String decryptBase64(String encryptedData, SecretKey secretKey) throws Exception {
        return decrypt(decode64(encryptedData), secretKey);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        SecretKey secretKey = generateAESKey();
        byte[] keyBytes = secretKey.getEncoded();

        String keyString = encode64(keyBytes);
        System.out.println("String base64key = \"" + keyString + "\";");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy