The original encryption and decryption is written in Java, used on the Android system. Now it's going to be used on iOS, so the encrypted files downloaded from the server are going to use Swift to implement their decryption methods.
The process is as follows:
Add a category to NSData, Nsdata+aes
Nsdata+aes.h
-(NSData *) Aes128decryptwithkey: (NSString *) Key IV: (NSDATA *) IV;
Nsdata+aes.m
-(NSData *) Aes128operation: (ccoperation) Operation key: (NSString *) Key IV: (NSDATA *) IV
{
Char keyptr[kcckeysizeaes128 + 1];
Bzero (keyptr, sizeof (KEYPTR));
[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];
Byte key2[] = {0xf0,0x1e,0x02,0xb2,0xe3,0xc9,0x7a,0x43,0xfd,0xfe,0x31,0x1a,0x2b,0xa4,0x4c,0x60};
IV
Byte ivptr[kccblocksizeaes128 + 1];
Bzero (ivptr, sizeof (IVPTR));
[IV getcstring:ivptr maxlength:sizeof (IVPTR) encoding:nsutf8stringencoding];
[IV getbytes:&ivptr LENGTH:16];
size_t buffersize = [Self length] + kCCBlockSizeAES128;
void *buffer = malloc (buffersize);
size_t numbytesencrypted = 0;
Cccryptorstatus cryptorstatus = cccrypt (operation, kCCAlgorithmAES128, Kccoptionpkcs7padding,
Key2, kCCKeySizeAES128,
Ivptr,
[self-bytes], [self length],
Buffer, buffersize,
&numbytesencrypted);
if (Cryptorstatus = = kccsuccess) {
NSLog (@ "Success");
return [NSData Datawithbytesnocopy:buffer length:numbytesencrypted];
}else{
NSLog (@ "Error");
}
Free (buffer);
return nil;
}
Swift enables AES decryption