[安卓/Android/Java] AES加密解密工具
·
加密模式(Mode)
/**
加密模式
*/
@Retention( RetentionPolicy.SOURCE )
public @interface Mode {
String ECB = "ECB";
String CBC = "CBC";
String CTR = "CTR";
String OFB = "OFB";
String CFB = "CFB";
}
填充(Padding)
/**
填充
*/
@Retention( RetentionPolicy.SOURCE )
public @interface Padding {
String PKCS5_PADDING = "PKCS5Padding ";
String PKCS7_PADDING = "PKCS7Padding ";
String ZERO_PADDING = "ZeroPadding";
String ISO_10126 = "ISO10126";
String ANSIX_923 = "ANSIX923";
String NO_PADDING = "NoPadding";
}
//加密模式
private static String mMode = Mode.ECB;
//填充
private static String mPadding = Padding.NO_PADDING;
//编码类型
private static Charset mCharset = Charset.defaultCharset();
创建用于AES加密解密的Cipher
/**
创建用于AES加密解密的Cipher
@param key 密钥
@param iv 偏移量
@param opMode 加密/解密
@return {@link Cipher}
*/
@Nullable
private static Cipher createCipher(String key, String iv, int opMode) {
Cipher cipher = null;
String type = "AES";
try {
//加密模式
cipher = Cipher.getInstance( type + "/" + mMode + "/" + mPadding );
} catch(NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
//密钥
SecretKeySpec skSpec = new SecretKeySpec( key.getBytes( mCharset ), type );
//偏移量(ECB模式不支持iv)
IvParameterSpec ivpSpec = Mode.ECB.equals( mMode ) ?
null :
new IvParameterSpec( iv.getBytes( mCharset ) );
if( ivpSpec == null ) {
try { cipher.init( opMode, skSpec );
} catch(InvalidKeyException e) {
e.printStackTrace();
}
}else {
try {
cipher.init( opMode, skSpec, ivpSpec );
} catch(InvalidAlgorithmParameterException | InvalidKeyException e) {
e.printStackTrace();
}
}
return cipher;
}
Base64编码/解码
/**
base64编码
@param data 数据
@return 结果
*/
private static String encodeBase64(byte[] data) {
//Android环境
return Base64.encodeToString( data, Base64.DEFAULT )
.replaceAll( String.valueOf( (char) 10 ), "" )
.trim();
// //Java环境
// return java.util.Base64.getEncoder().encodeToString( data )
// .replaceAll( String.valueOf( (char) 10 ), "" )
// .trim();
}
/**
base64解码
@param data 数据
@return 结果
*/
private static byte[] decodeBase64(String data) {
//Android环境
return Base64.decode( data.getBytes( mCharset ), Base64.DEFAULT );
// //Java环境
// return java.util.Base64.getDecoder().decode( data.getBytes( mCharset ) );
}
加密(Encrypt)
/**
加密方法
@param data 数据
@param key 密钥
@param iv 偏移量
@return 结果
*/
@NonNull
public static String encrypt(String data, String key, String iv) {
try {
Cipher cipher = createCipher( key, iv, Cipher.ENCRYPT_MODE );
if( cipher == null ) return "";
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes( mCharset );
int plaintextLength = dataBytes.length;
if( plaintextLength % blockSize != 0 ) {
plaintextLength = plaintextLength + ( blockSize - ( plaintextLength % blockSize ) );
}
byte[] plaintext = new byte[ plaintextLength ];
System.arraycopy( dataBytes, 0, plaintext, 0, dataBytes.length );
return encodeBase64( cipher.doFinal( plaintext ) );
} catch (BadPaddingException | IllegalBlockSizeException | AssertionError e) {
e.printStackTrace();
}
return "";
}
/**
加密方法
@param data 数据
@param key 密钥
@return 结果
*/
public static String encrypt(String data, String key) { return encrypt( data, key, key ); }
解密(Decrypt)
/**
解密方法
@param data 数据
@param key 密钥
@param iv 偏移量
@return 解密的结果
*/
@NonNull
public static String decrypt(String data, String key, String iv) {
try {
Cipher cipher = createCipher( key, iv, Cipher.DECRYPT_MODE );
return new String(
cipher == null ? new byte[ 0 ] : cipher.doFinal( decodeBase64( data ) ),
mCharset
).trim();
} catch (BadPaddingException | IllegalBlockSizeException | IllegalArgumentException e) {
e.printStackTrace();
}
return "";
}
/**
解密方法
@param data 数据
@param key 密钥
@return 解密的结果
*/
public static String decrypt(String data, String key) { return decrypt( data, key, key ); }
调用
public static void main(String[] args) {
//默认加密模式:ECB,默认填充:NO_PADDING
//ECB不支持iv填充(所以修改填充是无效的)
String data = "abcde";
String pwd = "1234567890123456";
//加密
String en = encrypt( data, pwd );
//解密
String de = decrypt( en, pwd );
System.out.println(
"内容:" + data + "\n" +
"密码:" + pwd + "\n" +
"加密结果:" + en + "\n" +
"解密结果:" + de + "\n"
);
}
输出结果:
内容:abcde
密码:1234567890123456
加密结果:B3BfZ12Zg/2qEWRAMh/2sw==
解密结果:abcde
更多推荐


所有评论(0)