Http://www.cnblogs.com/chen-lhx/p/5817161.html
*************************************************
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/ **
*
* @author Administrator
*
* /
public class AES {
// encryption
public static String Encrypt (String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print ("Key is null");
return null;
}
// determine whether the Key is 16 bits
if (sKey.length ()! = 16) {
System.out.print ("Key length is not 16 bits");
return null;
}
byte [] raw = sKey.getBytes ("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES");
Cipher cipher = Cipher.getInstance ("AES / ECB / PKCS5Padding"); // "Algorithm / pattern / complement method"
cipher.init (Cipher.ENCRYPT_MODE, skeySpec);
byte [] encrypted = cipher.doFinal (sSrc.getBytes ("utf-8"));
return new Base64 (). encodeToString (encrypted); // Base64 is used here as the transcoding function, and it can also play the role of secondary encryption.
}
// decrypt
public static String Decrypt (String sSrc, String sKey) throws Exception {
try {
// determine if the Key is correct
if (sKey == null) {
System.out.print ("Key is null");
return null;
}
// determine whether the Key is 16 bits
if (sKey.length ()! = 16) {
System.out.print ("Key length is not 16 bits");
return null;
}
byte [] raw = sKey.getBytes ("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES");
Cipher cipher = Cipher.getInstance ("AES / ECB / PKCS5Padding");
cipher.init (Cipher.DECRYPT_MODE, skeySpec);
byte [] encrypted1 = new Base64 (). decode (sSrc);
try {
byte [] original = cipher.doFinal (encrypted1);
String originalString = new String (original, "utf-8");
return originalString;
} catch (Exception e) {
System.out.println (e.toString ());
return null;
}
} catch (Exception ex) {
System.out.println (ex.toString ());
return null;
}
}
public static void main (String [] args) throws Exception {
/ *
* AES-128-ECB encryption mode is used here, and the key needs to be 16 bits.
* /
String cKey = "1234567890123456";
// string to be encrypted System.out.println ("The encrypted string is:" + enString);
// decrypt
String DeString = AES.Decrypt (enString, cKey);
System.out.println ("The decrypted string is:" + DeString);
}
}
// Source code snippet from cloud code http://yuncode.net
Android
package cd.server.data;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import android.annotation.SuppressLint;
import android.util.Base64;
// http://blog.csdn.net/hbcui1984/article/details/5201247
// AES encryption and decryption tool class
public class AES128
{
private static final String UTF_8 = "UTF-8";
/ **
* Encryption
* @param content Content to be encrypted
* @param password encrypted password
* @return
* /
@SuppressLint ("TrulyRandom")
public static String encrypt (String key, String src)
{
try
{
if (key == null || key.length () <= 0 || src == null || src.length () <= 0) return null;
SecretKeySpec skey = new SecretKeySpec (key.getBytes (), "AES");
Cipher cipher = Cipher.getInstance ("AES / ECB / PKCS5Padding");
byte [] byteContent = src.getBytes (UTF_8);
cipher.init (Cipher.ENCRYPT_MODE, skey); // initialization
byte [] result = cipher.doFinal (byteContent);
return byte2Base64 (result); // encryption
}
catch (Exception ext)
{
ext.printStackTrace ();
}
return null;
}
private static String byte2Base64 (byte [] encode)
{
try
{
return Base64.encodeToString (encode, Base64.NO_WRAP);
}
catch (Exception ext)
{
ext.printStackTrace ();
}
return null;
}
}
Java decrypts AES-128-ECB encryption using AES encryption