1.AES-GCM加密算法原理:https://www.jianshu.com/p/ebdd0aa7d91f
2.以下是Android gcm加密解密的demo(加解密文件大小为192M大小的apk):
public class AES128SymmetricDecryption {
private static final int GCM_NONCE_LENGTH =12;// in bytes
private static final int GCM_TAG_LENGTH =16;// in bytes
@RequiresApi(api = Build.VERSION_CODES.O)
public static void encodeOrDecode (String fileInputStream, String fileOutputStream,byte[] keyStr,byte[] ivStr,int mode)throws FileNotFoundException {
FileInputStream inputStream =new FileInputStream(fileInputStream);
FileOutputStream fos =new FileOutputStream(fileOutputStream);
try {
int numRead =0;
byte[] buf =new byte[5242880+16];
while ((numRead = inputStream.read(buf)) >0) {
byte[] bytes=Arrays.copyOfRange(buf,0,numRead);
byte[] decryptedBlock=getByteEnOrDeByte(bytes,keyStr,ivStr,mode);
fos.write(decryptedBlock,0,decryptedBlock.length);
}
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fos !=null) {
fos.close();
}
if (inputStream !=null) {
inputStream.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private static byte[] getByteEnOrDeByte(byte[] bytes,byte[]keyStr,byte[] ivStr,int mode)throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
SecretKeySpec key =new SecretKeySpec(keyStr,"AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec spec =new GCMParameterSpec(GCM_TAG_LENGTH *8, Arrays.copyOf(ivStr,GCM_NONCE_LENGTH));
cipher.init(mode, key, spec);
return cipher.doFinal(bytes);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void main(String[] args)throws GeneralSecurityException, IOException
{
final String rootPath=Environment.getExternalStorageDirectory().getAbsolutePath();
final String inputFileName = rootPath +"/202_6.apk";
final String outputFileName = rootPath +"/202_6_gcm.apk";
final String key ="0987654321000000";
final String iv ="098765432100";
new Thread(new Runnable() {
@Override
public void run() {
try {
encodeOrDecode(inputFileName,outputFileName,key.getBytes("UTF-8"),iv.getBytes("UTF-8"),Cipher.ENCRYPT_MODE);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
String inputFileName =rootPath+"/202_6_gcm.apk";
String outputFileName =rootPath+"/202_6_gcm-decode.apk";
long obfStartTime = System.currentTimeMillis();
Log.d("main","gcm解密开始时间===" + obfStartTime);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
encodeOrDecode(inputFileName,outputFileName,key.getBytes("UTF-8"),iv.getBytes("UTF-8"),Cipher.DECRYPT_MODE);
}catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
long obfEndTime = System.currentTimeMillis();
Log.d("main","gcm解密结束时间===" + obfEndTime);
Log.d("main","gcm解密时长===" + (obfEndTime - obfStartTime));
}
}).start();
}
}