废话不多说,直接上!
/**
* 数据加密
* mazhanzhu
*/
public class AES128Util {
//密钥
public static final String AESKEY = "******************mzz";
//偏移量
public static final String IVVAL = "******************mzz";
//算法名
public static final String DATA_KEY = "AES";
//加解密算法/模式/填充方式
//可以任意选择,为了方便后面与iOS端的加密解密,采用与其相同的模式与填充方式
//ECB模式只用密钥即可对数据进行加密解密,CBC模式需要添加一个参数iv
public static final String DATA_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
//生成密钥
private static byte[] generateKey(String aesKey) throws Exception {
//Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
/* KeyGenerator kgen =KeyGenerator.getInstance(KEY_ALGORITHM);
kgen.init(128, new SecureRandom(aesKey.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] encodeFormat = secretKey.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(encodeFormat, "AES");
return keySpec.getEncoded();*/
return aesKey.getBytes();
}
//生成iv
private static AlgorithmParameters generateIV(String ivVal) throws Exception {
byte[] iv = ivVal.getBytes();
AlgorithmParameters params = AlgorithmParameters.getInstance(DATA_KEY);
params.init(new IvParameterSpec(iv));
return params;
}
//转化成JAVA的密钥格式
private static Key convertToKey(byte[] keyBytes) throws Exception {
SecretKey secretKey = new SecretKeySpec(keyBytes, DATA_KEY);
return secretKey;
}
/**
* 加密
*
* @param plainText 需要加密的字符串
* @return
*/
public static String JiaMi(String plainText) {
try {
byte[] data = plainText.getBytes();
AlgorithmParameters iv = generateIV(IVVAL);
byte[] keyBytes = generateKey(AESKEY);
//转化为密钥
Key key = convertToKey(keyBytes);
//Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(DATA_CIPHER_ALGORITHM);
//设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptData = cipher.doFinal(data);
return bytesToHexString(encryptData);
} catch (Exception e) {
Log_Ma.e("加密报错", e.toString());
return "";
}
}
/**
* 解密
*
* @param encryptedStr 需要解密的字符串
* @return
*/
public static String JieMi(String encryptedStr) {
try {
byte[] encryptedData = hexStringToByte(encryptedStr);
byte[] keyBytes = generateKey(AESKEY);
Key key = convertToKey(keyBytes);
Cipher cipher = Cipher.getInstance(DATA_CIPHER_ALGORITHM);
AlgorithmParameters iv = generateIV(IVVAL);
//设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decryptData = cipher.doFinal(encryptedData);
return new String(decryptData);
} catch (Exception e) {
Log_Ma.e("解密报错", e.toString());
return "";
}
}
/**
* 十六进制字符串转换成数组
*
* @param hex
* @return
*/
private static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789abcdef".indexOf(c);
return b;
}
/**
* 把字节数组转换成16进制字符串
*
* @param bArray
* @return
*/
private static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toLowerCase());
}
return sb.toString();
}
//fixme -----------文件加密------------------------------------------------------------
private static final byte[] IMG_KEY = {56, 57, 58, 59, 60, 61, 62, 63};//秘钥长度必须是8 位或以上
private static final String IMG_ALGORITHM = "DES";//秘钥长度必须是8 位或以上
private static final String IMG_CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
/**
* 文件进行加密并保存加密后的文件到指定目录
*
* @param fromFilePath 要加密的文件 如c:/test/待加密文件.txt
* @param toFilePath 加密后存放的文件 如c:/加密后文件.txt
*/
public static void encrypt(String fromFilePath, String toFilePath) {
Log_Ma.e("encrypting...");
File fromFile = new File(fromFilePath);
if (!fromFile.exists()) {
Log_Ma.e("to be encrypt file no exist!");
return;
}
File toFile = getFile(toFilePath);
SecretKey secretKey = new SecretKeySpec(IMG_KEY, IMG_ALGORITHM);
InputStream is = null;
OutputStream out = null;
CipherInputStream cis = null;
try {
Cipher cipher = Cipher.getInstance(IMG_CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
is = new FileInputStream(fromFile);
out = new FileOutputStream(toFile);
cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
} catch (Exception e) {
Log_Ma.e(e.toString());
} finally {
try {
if (cis != null) {
cis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log_Ma.e("encrypt completed");
}
private static File getFile(String filePath) {
File fromFile = new File(filePath);
if (!fromFile.getParentFile().exists()) {
fromFile.getParentFile().mkdirs();
}
return fromFile;
}
/**
* 文件进行解密并保存解密后的文件到指定目录
*
* @param fromFilePath 已加密的文件 如c:/加密后文件.txt
* @param toFilePath 解密后存放的文件 如c:/ test/解密后文件.txt
*/
public static void decrypt(String fromFilePath, String toFilePath) {
Log_Ma.e("decrypting...");
File fromFile = new File(fromFilePath);
if (!fromFile.exists()) {
Log_Ma.e("to be decrypt file no exist!");
return;
}
File toFile = getFile(toFilePath);
SecretKey secretKey = new SecretKeySpec(IMG_KEY, IMG_ALGORITHM);
InputStream is = null;
OutputStream out = null;
CipherOutputStream cos = null;
try {
Cipher cipher = Cipher.getInstance(IMG_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
is = new FileInputStream(fromFile);
out = new FileOutputStream(toFile);
cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
} catch (Exception e) {
Log_Ma.e(e.toString());
} finally {
try {
if (cos != null) {
cos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log_Ma.e("decrypt completed");
}
}
没了