AI智能
改变未来

iOS开发 AES+Base64

```objectivecsize_t const kKeySize = kCCKeySizeAES128;NSString *const kInitVector = @\"16-Bytes--String\";- (NSString *)encryptAES:(NSString *)content key:(NSString *)key {NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];NSUInteger dataLength = contentData.length;
// 为结束符\'\\\\0\' +1char keyPtr[kKeySize + 1];memset(keyPtr, 0, sizeof(keyPtr));[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];// 密文长度 <= 明文长度 + BlockSize```objectivecsize_t encryptSize = dataLength + kCCBlockSizeAES128;void *encryptedBytes = malloc(encryptSize);size_t actualOutSize = 0;NSData *initVector = [kInitVector dataUsingEncoding:NSUTF8StringEncoding];CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES,kCCOptionPKCS7Padding,  // 系统默认使用 CBC,然后指明使用 PKCS7PaddingkeyPtr,kKeySize,initVector.bytes,contentData.bytes,dataLength,encryptedBytes,encryptSize,&actualOutSize);if (cryptStatus == kCCSuccess) {// 对加密后的数据进行 base64 编码return [[NSData dataWithBytesNoCopy:encryptedBytes length:actualOutSize] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];}free(encryptedBytes);return nil;}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » iOS开发 AES+Base64