import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* 加密
* Created by fxf on 2018/1/9.
*/
public class AESUtil {
/**
* 秘鑰長(zhǎng)度
*/
private static final int SECURE_KEY_LENGTH = 16;
private static final String IV_STRING = "16-Bytes--String";
/**
* 采用AES128加密
*
* @param content 要加密的內(nèi)容
* @param secureKey 密鑰
* @return
*/
public static String encrypt(String content, String secureKey) {
if (content == null) {
return null;
}
try {
// 獲得密匙數(shù)據(jù)
byte[] rawKeyData = getAESKey(secureKey);
// 從原始密匙數(shù)據(jù)創(chuàng)建KeySpec對(duì)象
SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
// Cipher對(duì)象實(shí)際完成加密操作
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 用密匙初始化Cipher對(duì)象
byte[] initParam = IV_STRING.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
// 正式執(zhí)行加密操作
byte[] encryptByte = cipher.doFinal(content.getBytes());
return Base64.encodeToString(encryptByte,Base64.NO_WRAP);
}catch (UnsupportedEncodingException e){
}catch (NoSuchAlgorithmException e){
}catch (NoSuchPaddingException E){
}catch (InvalidAlgorithmParameterException e){
}catch (InvalidKeyException e){
}catch (IllegalBlockSizeException e){
}catch (BadPaddingException e){
}
return null;
}
public static byte[] getAESKey(String key)
throws UnsupportedEncodingException {
byte[] keyBytes;
keyBytes = key.getBytes("UTF-8");
byte[] keyBytes16 = new byte[SECURE_KEY_LENGTH];
System.arraycopy(keyBytes, 0, keyBytes16, 0,
Math.min(keyBytes.length, SECURE_KEY_LENGTH));
return keyBytes16;
}
/**
* 采用AES128解密
*
* @param content
* @param secureKey
* @return
* @throws Exception
* ,Exception
* @throws Exception
*/
public static String decrypt(String content, String secureKey) {
if (content == null) {
return null;
}
byte[] data = Base64.decode(content,Base64.NO_WRAP);
try {
// 獲得密匙數(shù)據(jù)
byte[] rawKeyData = getAESKey(secureKey); // secureKey.getBytes();
// 從原始密匙數(shù)據(jù)創(chuàng)建一個(gè)KeySpec對(duì)象
SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
// Cipher對(duì)象實(shí)際完成解密操作
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 用密匙初始化Cipher對(duì)象
byte[] initParam = IV_STRING.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
return new String(cipher.doFinal(data),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchPaddingException e) {
} catch (InvalidKeyException e) {
} catch (InvalidAlgorithmParameterException e) {
} catch (IllegalBlockSizeException e) {
} catch (BadPaddingException e) {
}
return null;
}
}
Android AES+Base64加解密
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。