文件名称:OC AES 128位解密
文件大小:3KB
文件格式:ZIP
更新时间:2022-04-14 05:03:44
oc iOS AES
+ (NSData *)AES128operation:(CCOperation)operation data:(NSData *)data key:(NSString *)key iv:(NSString *)iv { char keyPtr[kCCKeySizeAES128 + 1]; //kCCKeySizeAES128是加密位数 可以替换成256位的 bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; // IV char ivPtr[kCCBlockSizeAES128 + 1]; bzero(ivPtr, sizeof(ivPtr)); [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; size_t bufferSize = [data length] + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; // 设置加密参数 /** 这里设置的参数ios默认为CBC加密方式,如果需要其他加密方式如ECB,在kCCOptionPKCS7Padding这个参数后边加上kCCOptionECBMode,即kCCOptionPKCS7Padding | kCCOptionECBMode,但是记得修改上边的偏移量,因为只有CBC模式有偏移量之说 */ CCCryptorStatus cryptorStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES128, ivPtr, [data bytes], [data length], buffer, bufferSize, &numBytesEncrypted;); if(cryptorStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } else { NSLog(@"AES operation failed"); } free(buffer); return nil; }
【文件预览】:
YTAESManager
----YTAESManager.m(2KB)
----YTAESManager.h(664B)