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;
/**
* AES Secure Encoding Component
*/
Public abstract class Aescoder {
/**
* Key algorithm
*/
Public static final String key_algorithm = "AES";
/**
* Encryption/decryption algorithm/working mode/Fill Way Java 6 supports pkcs5padding fill mode bouncy
* Castle Support pkcs7padding fill mode
*/
public static final String cipher_algorithm = "aes/ecb/pkcs5padding";
/**
* Convert key
*
* @param key
* Binary Key
* @return Key key
* @throws Exception
*/
private static key Tokey (byte[] key) throws Exception {
//instancing of AES keying material
secretkey secretkey = new Secretkeyspec (key, key_algorithm);
return secretkey;
}
/**
* Decryption
*
* @param data
* Data to be decrypted
* @param key
* Key
* @return byte[] Decrypt data
* @throws Exception
*/
public static byte[] Decrypt (byte[] data, byte[] key) throws Exception {
//Restore key
key k = Tokey (key);
/*
* Instantiation using pkcs7padding Fill method, implement Cipher.getinstance as follows (Cipher_algorithm,
* "BC");
*/
Cipher Cipher = cipher.getinstance (cipher_algorithm);
//Initialize, set to decryption mode
cipher.init (cipher. Decrypt_mode, k);
//Perform actions
return cipher.dofinal (data);
}
/**
* Encryption
*
* @param data
* Pending Encryption data
* @param key
* Key
* @return byte[] Encrypt data
* @throws Exception
*/
public static byte[] Encrypt (byte[] data, byte[] key) throws Exception {
//Restore key
key k = Tokey (key);
/*
* Instantiation using pkcs7padding Fill method, implement Cipher.getinstance as follows (Cipher_algorithm,
* "BC");
*/
Cipher Cipher = cipher.getinstance (cipher_algorithm);
//Initialize, set to encryption mode
cipher.init (cipher. Encrypt_mode, k);
//Perform actions
return cipher.dofinal (data);
}
/**
* Generate key <br>
*
* @return byte[] binary key
* @throws Exception
*/
public static byte[] Initkey () throws Exception {
//Instantiation
keygenerator kg = keygenerator.getinstance (key_algorithm);
/*
* AES requires a key length of 128-bit, 192-bit or 256-bit, where 128-bit
*/
Kg.init (+);
//Generate Secret key
secretkey secretkey = kg.generatekey ();
//Get the binary encoded form of the key
return secretkey.getencoded ();
}
/**
* Convert byte array to 16 binary string
* @param buf
* @return
*/
public static String parsebyte2hexstr (byte buf[]) {
stringbuffer sb = new StringBuffer ();
For (int i = 0; i < buf. Length i++) {
String hex = integer.tohexstring (buf[i] & 0xFF);
if (hex.length () = = 1) {
hex = ' 0 ' + hex;
}
sb.append (hex.touppercase ());
}
return sb.tostring ();
}
/**
* Converts a 16 binary string to a byte array
* @param hexstr
* @return
*/
public static byte[] Parsehexstr2byte (String hexstr) {
if (hexstr.length () < 1)
return null;
byte[] result = new byte[hexstr.length ()/2];
For (int i = 0; i < hexstr.length ()/2; i++) {
int High = Integer.parseint (hexstr.substring (i * 2, I * 2 + 1), +);
int low = Integer.parseint (hexstr.substring (i * 2 + 1, I * 2 + 2), +);
result[i] = (byte) (High* + low );
}
return result;
}
public static void Main (string[] args) throws Exception {
//Initialize key
byte[] key = Base64.decodebase64 ("geyd4zscwabjt9vrwr5cuq==");
String inputstr = "Tcweb.bank";
byte[] inputdata = inputstr.getbytes ();
inputdata = Aescoder.encrypt (inputdata, key);
String userName = base64.encodebase64string (inputdata);
System. out.println ("after encryption:" + userName);
//decryption
byte[] usernamedata = Aescoder.decrypt (base64.decodebase64 (userName), key);
userName = new String (Usernamedata);
System. out.println ("userName:" + userName);
}
}
AES Encryption 16 binary and binary conversion