1. EncryptUtil
import android.util.Base64;
import java.security.InvalidParameterException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES encryption and decryption
* <p>
* 1. key's length >= 16
* 2. iv's length > 16
* 3. "transformation": AES/CBC/PKCS5Padding
* 4. iv=12(bytes) length=128(bits)
* 5. iv=24(bytes) length=192(bits)
* 6. iv=32(bytes) length=256(bits)
* <p>
* Created by Anter on 2018/4/11.
*/
public class EncryptUtil {
private EncryptUtil() {
}
/**
* Base64 decode then AES decrypt
*
* @param data Data to decrypt
* @param key Decrypt key
* @param iv Decrypt key
* @param transformation AES/CBC/PKCS5Padding
* @return Decrypted bytes
* @throws Exception Decrypt exception
*/
public static byte[] decryptBase64EncodeData(byte[] data, byte[] key, byte[] iv, String transformation) throws Exception {
if (data == null || data.length == 0
|| key == null || key.length < 16
|| iv == null || iv.length < 16
|| transformation == null || transformation.length() == 0) {
throw (new InvalidParameterException());
}
byte[] textBytes = Base64.decode(data, Base64.DEFAULT);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
SecretKeySpec newKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(textBytes);
}
/**
* AES encrypt then base64 decode
*
* @param data Data to encrypt
* @param key Encrypt key
* @param iv Encrypt key
* @param transformation AES/CBC/PKCS5Padding
* @return Encrypted bytes
* @throws Exception Encrypt exception
*/
public static byte[] encryptAndBase64Encode(byte[] data, byte[] key, byte[] iv, String transformation) throws Exception {
if (data == null || data.length == 0
|| key == null || key.length == 0
|| iv == null || iv.length == 0
|| transformation == null || transformation.length() == 0) {
throw (new InvalidParameterException());
}
AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
SecretKeySpec newKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
return Base64.encode(cipher.doFinal(data), Base64.DEFAULT);
}
}
2. Usage
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.aes_btn_encryption).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
findViewById(R.id.aes_btn_decryption).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
// Unsupported key size: 15 bytes
// expected IV length of 16 but was 8
try {
String originalString = "Hello";
byte[] encryptedData = EncryptUtil.encryptAndBase64Encode(originalString.getBytes(), "1234567812345678".getBytes(), "1234567812345678".getBytes(), "AES/CBC/PKCS5Padding");
byte[] decryptedData = EncryptUtil.decryptBase64EncodeData(encryptedData, "1234567812345678".getBytes(), "1234567812345678".getBytes(), "AES/CBC/PKCS5Padding");
String decryptedString = new String(decryptedData);
String encryptedString = new String(encryptedData);
if (Arrays.equals(encryptedData, encryptedData)) {
Log.d("MainActivity", "onCreate: Good job!");
}
Log.d("MainActivity", "originalString = " + originalString + ", decryptedString = " + decryptedString + ", encryptedString = " + encryptedString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. 小解
-
Android AES
加密时加密的key
和iv
(bytes
)的长度需要>= 16
- 加密位数要求
iv
(bytes
)的长度:16 =>128, 24 => 192, 32 => 256
网友评论