java gcm_Android 用java现实AES-GCM 加解密文件

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值