59 lines
1.6 KiB
Mathematica
59 lines
1.6 KiB
Mathematica
|
|
//
|
||
|
|
// NSData+AES.m
|
||
|
|
// MessagesBridge
|
||
|
|
//
|
||
|
|
// Created by James Magahern on 11/15/18.
|
||
|
|
// Copyright © 2018 James Magahern. All rights reserved.
|
||
|
|
//
|
||
|
|
|
||
|
|
#import "NSData+AES.h"
|
||
|
|
|
||
|
|
@implementation NSData (AES)
|
||
|
|
|
||
|
|
+ (NSData *)AES128Operation:(CCOperation)operation
|
||
|
|
withInputData:(NSData *)inputData
|
||
|
|
key:(NSData *)key
|
||
|
|
iv:(NSData *)iv
|
||
|
|
error:(NSError **)error
|
||
|
|
{
|
||
|
|
size_t dataMoved = 0;
|
||
|
|
NSMutableData *outputData = [NSMutableData dataWithLength:(inputData.length + kCCBlockSizeAES128)];
|
||
|
|
CCCryptorStatus status = CCCrypt(
|
||
|
|
operation,
|
||
|
|
kCCAlgorithmAES,
|
||
|
|
kCCOptionPKCS7Padding,
|
||
|
|
key.bytes,
|
||
|
|
key.length,
|
||
|
|
iv.bytes,
|
||
|
|
inputData.bytes,
|
||
|
|
inputData.length,
|
||
|
|
outputData.mutableBytes,
|
||
|
|
outputData.length,
|
||
|
|
&dataMoved
|
||
|
|
);
|
||
|
|
|
||
|
|
if (status == kCCSuccess) {
|
||
|
|
outputData.length = dataMoved;
|
||
|
|
} else {
|
||
|
|
*error = [NSError errorWithDomain:@"CommonCryptoError"
|
||
|
|
code:status
|
||
|
|
userInfo:nil];
|
||
|
|
|
||
|
|
outputData = nil;
|
||
|
|
}
|
||
|
|
|
||
|
|
return outputData;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (NSData *)encryptedDataWithKey:(NSData *)key iv:(NSData *)iv error:(NSError *__autoreleasing *)error
|
||
|
|
{
|
||
|
|
return [[self class] AES128Operation:kCCEncrypt withInputData:self key:key iv:iv error:error];
|
||
|
|
}
|
||
|
|
|
||
|
|
- (NSData *)decryptedDataWithKey:(NSData *)key iv:(NSData *)iv error:(NSError *__autoreleasing *)error
|
||
|
|
{
|
||
|
|
return [[self class] AES128Operation:kCCDecrypt withInputData:self key:key iv:iv error:error];
|
||
|
|
}
|
||
|
|
|
||
|
|
@end
|