Android手机 解密存储,aes 读取文件,解密完并保存,安卓+ios

最近由于苹果的审核 和 安卓的审核用到aes的文件解密保存

安卓的assets资源解密并保存到指定目录

//implementation files('libs/commons-io-2.6.jar') IOUtils可以去官网下载

链接: https://pan.baidu.com/s/1FD0DEb8xChGcFUcuTajOfg

提取码: dwuu

public static Stringkey ="加密的key";

private static final StringIV_STRING ="16位的偏移量";

public static final StringENCODING ="utf-8";

/**

*

* @param myContext

* @param ASSETS_NAME 要解密的文件名(asstes下的全路径,比如asstes下的happy目录下的x.png  eg: happy/x.png)

* @param savePath 要保存的路径(目标路径,不包含文件名字)

* @param saveName 复制后的文件名(保存到手机的文件名字,eg: r.png)

*/

public static void copy(Context myContext, String ASSETS_NAME,String savePath, String saveName) {

try {

String filename = savePath +"/" + saveName;

File dir =new File( savePath);

// 如果目录不中存在,创建这个目录

if (!dir.exists()){

dir.mkdirs();//忽略父层级,直接创建

}

if (!(new File(filename)).exists()) {

InputStream is = myContext.getResources().getAssets().open(ASSETS_NAME);

FileOutputStream fos =new FileOutputStream(filename);

byte[] toarray = IOUtils.toByteArray(is);

ByteBuffer byteBuffer = ByteBuffer.wrap(toarray);

Cipher mCipher = Cipher.getInstance("AES/CBC/NoPadding");

SecretKeySpec secretKey =new SecretKeySpec(key.getBytes(ENCODING), "AES");

//偏移量

byte[] initParam =IV_STRING.getBytes(ENCODING);

IvParameterSpec ivParameterSpec =new IvParameterSpec(initParam);

mCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

int count =0;

if (byteBuffer!=null){

byte[] bytes = mCipher.doFinal(byteBuffer.array());

fos.write(bytes, 0, bytes.length);

}

fos.close();

is.close();

}

}catch (Exception e) {

e.printStackTrace();

}

}

ios的Resource资源解密

NSData*CECE_aesDecryptData_XSXS(NSData*contentData,NSData*keyData) {

NSCParameterAssert(contentData);

NSCParameterAssert(keyData);

NSString *hint = [NSString stringWithFormat:@"The key size of AES-%lu should be %lu bytes!", kKeySize * 8, kKeySize];

NSCAssert(keyData.length == kKeySize, hint);

returncipherOperation(contentData, keyData,kCCDecrypt);

}

NSData*cipherOperation(NSData*contentData,NSData*keyData,CCOperationoperation) {

NSUIntegerdataLength = contentData.length;

void const *initVectorBytes = [kInitVector dataUsingEncoding:NSUTF8StringEncoding].bytes;

voidconst*contentBytes = contentData.bytes;

voidconst*keyBytes = keyData.bytes;

size_toperationSize = dataLength +kCCBlockSizeAES128;

void*operationBytes =malloc(operationSize);

if(operationBytes ==NULL) {

returnnil;

}

size_tactualOutSize =0;

CCCryptorStatuscryptStatus =CCCrypt(operation,

kCCAlgorithmAES,

kCCOptionPKCS7Padding,

keyBytes,

kKeySize,

initVectorBytes,

contentBytes,

dataLength,

operationBytes,

operationSize,

&actualOutSize);

if(cryptStatus ==kCCSuccess) {

return[NSDatadataWithBytesNoCopy:operationByteslength:actualOutSize];

}

free(operationBytes);

operationBytes =NULL;

return nil;

}

调用的

NSData* data = [NSDatadataWithContentsOfFile:[[NSBundlemainBundle]  pathForResource:finalxd_pathofType:houzhui]];

NSData* data2 =CECE_aesDecryptData_XSXS(data, [key dataUsingEncoding:NSUTF8StringEncoding]);//可以为解密

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值