实现代码
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.annotation.SuppressLint;
import android.util.Base64;
public class AESDemo {
//算法名称
private static final String KEY_ALGORIHM="AES";
// 加解密算法/模式/填充方式
// 可以任意选择,
private static final String CIPHER_ALGORITHM="AES/ECB/PKCS7Padding";
// 生成密钥
@SuppressLint("TrulyRandom")
private static byte[] generateKey() throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORIHM);
keyGenerator.init(128);
// SecretKey key = keyGenerator.generateKey();
SecretKeySpec keySpec = new SecretKeySpec(
"加密密钥字符串".getBytes(), "AES");
return keySpec.getEncoded();
}
// 转化成JAVA的密钥格式
private static Key convertToKey(byte[] keyBytes) throws Exception {
SecretKey secretKey = new SecretKeySpec(keyBytes, KEY_ALGORIHM);
return secretKey;
}
// 加密
private static byte[] encrypt(byte[] data, byte[] keyBytes)
throws Exception {
// 转化为密钥
Key key = convertToKey(keyBytes);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// 设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
public static String doEncrypt(String data) {
String info = null;
try {
byte[] key = generateKey();
byte[] encryptedData = encrypt(data.getBytes(), key);
info = new String(Base64.encode(encryptedData,
Base64.DEFAULT));
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
// 解密
private static byte[] decrypt(byte[] encryptedData, byte[] keyBytes)
throws Exception {
Key key = convertToKey(keyBytes);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// 设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(encryptedData);
}
public static String doDecrypt(String dataInfo) {
String info = null;
try {
byte[] key = generateKey();
byte[] data = decrypt(Base64.decode(dataInfo, Base64.DEFAULT), key);
info = new String(data);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
/***测试的数据
*/
public static void runDemo() {
// 明文
String plainTextString = "Hello,Bouncy 2Castle111222222222222222233333333333333333333333334444444444444444445555555555555555555555555555555555555555666666666666";
System.out.println("明文 : " + plainTextString);
byte[] key;
try {
// 初始化密钥
key = generateKey();
// 初始化iv
// AlgorithmParameters iv = generateIV();
System.out.println("密钥 : ");
// 打印密钥
for (int i = 0; i < key.length; i++) {
System.out.printf("%x", key[i]);
}
System.out.println();
// 进行加密
byte[] encryptedData = encrypt(plainTextString.getBytes(), key);
// String info=new String(new
// Base64Encoder().encode(encryptedData));
String info = new String(Base64.encode(encryptedData,
Base64.DEFAULT));
System.out.println("-----info-----[" + info + "]");
// 输出加密后的数据
System.out.println("加密后的数据 : ");
for (int i = 0; i < encryptedData.length; i++) {
System.out.printf("%x", encryptedData[i]);
}
System.out.println();
// 进行解密
info = "TjSNxwP946EWzhYI+2a1ltJ6v8I1gZHl360gYe3lJA+zGqh3EQHIYVa1Qouerr9f96hyBpAU9ieBHFbNjCjsvA==";
// byte[] data = decrypt(new BASE64Decoder().decodeBuffer(info),
// key);
byte[] data = decrypt(Base64.decode(info, Base64.DEFAULT), key);
System.out.println("解密: " + new String(data));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
runDemo();
}
}