Java AES encryption tools

AESCodec.java

Package util; Import java.security.Key;

Import javax.crypto.Cipher; Import javax.crypto.KeyGenerator;
Import javax.crypto.SecretKey;
Import javax.crypto.spec.SecretKeySpec;

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

public class AESCodec {

   
// key algorithm
    Private static Final String KEY_ALGORITHM = "the AES" ;

   
// encryption and decryption algorithm / working mode / fill mode, Java6.0 support PKCS5Padding fill mode, BouncyCastle support PKCS7Padding fill mode
    Private static Final CIPHER_ALGORITHM = String "AES / ECB / PKCS5Padding" ;

   
//Default Key
    Private static Final String KEY = "FFK / B5sFXeITk6cXV5rTNA ==" ;

   
/ **
    * generate a key
   
* /
    public static String InitKey () throws Exception {

       
// instantiate key generator
        the KeyGenerator kg = KeyGenerator.getInstance (KEY_ALGORITHM );

       
// initialize key generators: AES key length requires 128,192,256 bit
        kg.init (128 );

       
// generate the key
        a secretKey secretKey = kg.generateKey ();

       
// Get key binary coded form
        return the Base64. encodeBase64String (secretKey.getEncoded ());
    }

   
/ **
    * conversion key
   
* /
    public static Key toKey ( byte [] Key) throws Exception {
       
return new new SecretKeySpec (Key, KEY_ALGORITHM);
    }

   
/ **
    * encrypted data
    *
    *
@param Data to be encrypted data
    *
@param key Key
    *
@return encrypted data
    *
* /
    public static String the encrypt (Data String, String key) throws Exception {

       
// restore the key
        key K = toKey (Base64.decodeBase64 (key));

       
// use PKCS7Padding fill mode, there have so written (i.e. call BouncyCastle component implementation)

       
//Cipher object is instantiated, it is used to do the actual encryption of
        Cipher cipher = Cipher.getInstance (CIPHER_ALGORITHM);

       
// Cipher object is initialized, the encryption mode is set
        cipher.init (Cipher.ENCRYPT_MODE, K);

       
// perform cryptographic operations. Results usually encrypted for transmission Base64 encoding
        return Base64.encodeBase64String (Cipher.doFinal (data.getBytes ()));
    }

   
/ **
    * decrypted data
    *
    *
@param Data to be decrypted data
    *
@param Key Key
    *
@return decrypted data
    *
* /
    public static String the decrypt (data String, String Key) throws Exception {
        Key K
=toKey (Base64.decodeBase64 (Key));
        Cipher cipher
= Cipher.getInstance (CIPHER_ALGORITHM);

       
// Cipher object is initialized, is provided to decryption mode
        cipher.init (Cipher.DECRYPT_MODE, K);

       
// perform decryption operations
        return new new String ( Cipher.doFinal (Base64.decodeBase64 (data)));
    }

   
/ **
    * encrypted data
    *
    *
@param data to be encrypted data
    *
@param key key
    *
@return data encrypted
    *
* /
    public static String the encrypt (String Data) throws Exception {
       
return the encrypt (Data, KEY);
    }

   
/ **
    * decrypted data
    *
    *
@param Data to be decrypted data
    *
@param Key key
    *
@return decrypted data
    *
* /
    public static String the decrypt (String Data) throws Exception {
       
return the decrypt (Data, KEY);
    }

   
public static void main (String [] args) throws Exception {
       
// System.out.println ( "Key:" + InitKey ());
        String Data = "123456" ;

        String the encrypt
= the encrypt (Data);
        System.out.println(encrypt);
        System.out.println(decrypt(encrypt));

    }
}

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162771.htm