Java AES encryptAes(String value)

Here you can find the source of encryptAes(String value)

Description

encrypt Aes

License

Open Source License

Declaration

public static String encryptAes(String value) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:// w  w  w  .  j a  va  2 s .  c o  m
 *     Eurotech - initial API and implementation
 *
 *******************************************************************************/

import java.security.InvalidKeyException;
import java.security.Key;

import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class Main {
    private static final String ALGORITHM = "AES";
    private static final byte[] SECRET_KEY = "ipsea1s214d5a6sfs8dfsdg@$%3saf".getBytes();

    public static String encryptAes(String value) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = c.doFinal(value.getBytes());

        String encryptedValue = DatatypeConverter.printBase64Binary(encryptedBytes);
        return encryptedValue;
    }

    private static Key generateKey() {
        Key key = new SecretKeySpec(SECRET_KEY, ALGORITHM);
        return key;
    }
}

Related

  1. aesKey(int keySize)
  2. aesStoreKeyToFile(Key secretKey, String path)
  3. createSessionKey()
  4. decrypt(String encryptedText, String key)
  5. decrypt_aes(String key, String initVector, String encrypted)
  6. encryptDecrypt(int mode, String data, String key, String salt, String iv)
  7. getAesKey(String key)