最近由于苹果的审核 和 安卓的审核用到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]);//可以为解密