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

com.futureble.cryptography.crypto.AES Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016-2021 the original author or authors.
 * cryptography is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
  
package com.futureble.cryptography.crypto;

import com.futureble.cryptography.exception.AESException;
import com.futureble.cryptography.exception.DecryptionException;
import com.futureble.cryptography.exception.EncryptionException;
import com.futureble.rainbow.base.Arrays;
import com.futureble.standard.exception.EmptyException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

/**
 * AES
 *
 * @see com.futureble.cryptography.crypto.AbstractAES
 * @author 陈光毅
 * @since 1.0
 */
public class AES extends AbstractAES {

    private static volatile AES INSTANCE = null;

    private AES() {}

    static AES getInstance() {
        if (INSTANCE == null) {
            synchronized (AES.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AES();
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public byte[] generateKey(byte[] salt, int keySize) {
        try {
            return getKeyGenerator(salt, keySize).generateKey().getEncoded();
        } catch (Exception ex) {
            throw new AESException(ex);
        }
    }

    @Override
    public byte[] encryption(byte[] content, SecretKey secretKey) {
        try {
            return super.encrypt(content, secretKey);
        } catch (Exception ex) {
            throw new EncryptionException(ex);
        }
    }

    @Override
    public byte[] decryption(byte[] content, SecretKey secretKey) {
        try {
            return super.decrypt(content, secretKey);
        } catch (Exception ex) {
            throw new DecryptionException(ex);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy