|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- //
- // AESCipher.m
- // iOSFirst
- //
- // Created by 孙宇峰 on 2023/3/2.
- //
-
- #import <Foundation/Foundation.h>
- #import "AESCipher.h"
- #import <CommonCrypto/CommonCryptor.h>
-
- NSString const *kInitVector = @"x00/x00/x00/x00/x00/x00/x00/x00";
- size_t const kKeySize = kCCKeySizeAES128;
-
- NSData * cipherOperation(NSData *contentData, NSData *keyData, CCOperation operation) {
- NSUInteger dataLength = contentData.length;
-
- void const *initVectorBytes = [kInitVector dataUsingEncoding:NSUTF8StringEncoding].bytes;
- void const *contentBytes = contentData.bytes;
- void const *keyBytes = keyData.bytes;
-
- size_t operationSize = dataLength + kCCBlockSizeAES128;
- void *operationBytes = malloc(operationSize);
- if (operationBytes == NULL) {
- return nil;
- }
- size_t actualOutSize = 0;
-
- CCCryptorStatus cryptStatus = CCCrypt(operation,
- kCCAlgorithmAES128,
- kCCOptionPKCS7Padding|kCCOptionECBMode,
- keyBytes,
- kCCKeySizeAES128,
- nil,
- contentBytes,
- dataLength,
- operationBytes,
- operationSize,
- &actualOutSize);
-
- if (cryptStatus == kCCSuccess) {
- return [NSData dataWithBytesNoCopy:operationBytes length:actualOutSize];
- }
- free(operationBytes);
- operationBytes = NULL;
- return nil;
- }
-
- NSString * aesEncryptString(NSString *content, NSString *key) {
- NSCParameterAssert(content);
- NSCParameterAssert(key);
-
- NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
- NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
- NSData *encrptedData = aesEncryptData(contentData, keyData);
- return [encrptedData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
- }
-
- NSString * aesDecryptString(NSString *content, NSString *key) {
- NSCParameterAssert(content);
- NSCParameterAssert(key);
-
- NSData *contentData = [[NSData alloc] initWithBase64EncodedString:content options:NSDataBase64DecodingIgnoreUnknownCharacters];
- NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
- NSData *decryptedData = aesDecryptData(contentData, keyData);
- return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
- }
-
- NSData * aesEncryptData(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);
- return cipherOperation(contentData, keyData, kCCEncrypt);
- }
-
- NSData * aesDecryptData(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);
- return cipherOperation(contentData, keyData, kCCDecrypt);
- }
|