Skip to content

Instantly share code, notes, and snippets.

@networkextension
Created May 21, 2016 18:12
Show Gist options
  • Select an option

  • Save networkextension/d5151866bd074d3307e93045e9ac2414 to your computer and use it in GitHub Desktop.

Select an option

Save networkextension/d5151866bd074d3307e93045e9ac2414 to your computer and use it in GitHub Desktop.
AES/CFB/Nopadding Objective-C
+(NSData*) encryptData: (NSData*) data withKey: (NSData*) key
{
//Key to Data
// NSData *key = [keyString dataUsingEncoding:NSUTF8StringEncoding];
//String to encrypt to Data
//NSData *data = [stringToEncrypt dataUsingEncoding:NSUTF8StringEncoding];
// Init cryptor
CCCryptorRef cryptor = NULL;
// Alloc Data Out
NSMutableData *cipherData = [NSMutableData dataWithLength:data.length + kCCBlockSizeAES128];
//Empty IV: initialization vector
NSData *iv = [NSMutableData dataWithLength:kCCBlockSizeAES128];
unsigned char a[] = {57, 61, -27, -98, 108, 18, 4, 23, 86, 96, 125, -74, -35, -105, 1, -98};
memcpy(iv.bytes, a, 16);
//Create Cryptor
CCCryptorStatus create = CCCryptorCreateWithMode(kCCEncrypt,
kCCModeCFB,
kCCAlgorithmAES128,//kCCAlgorithmAES,
ccNoPadding,
iv.bytes, // can be NULL, because null is full of zeros
key.bytes,
key.length,
NULL,
0,
0,
0,
&cryptor);
if (create == kCCSuccess)
{
//alloc number of bytes written to data Out
size_t outLength;
//Update Cryptor
CCCryptorStatus update = CCCryptorUpdate(cryptor,
data.bytes,
data.length,
cipherData.mutableBytes,
cipherData.length,
&outLength);
if (update == kCCSuccess)
{
//Cut Data Out with nedded length
cipherData.length = outLength;
//Final Cryptor
CCCryptorStatus final = CCCryptorFinal(cryptor, //CCCryptorRef cryptorRef,
cipherData.mutableBytes, //void *dataOut,
cipherData.length, // size_t dataOutAvailable,
&outLength); // size_t *dataOutMoved)
if (final == kCCSuccess)
{
//Release Cryptor
//CCCryptorStatus release =
CCCryptorRelease(cryptor ); //CCCryptorRef cryptorRef
}
NSMutableData *D = [NSMutableData dataWithData:iv];
[D appendData:cipherData];
return D;
}
}
else
{
//error
}
return nil;
}
+ (NSData*) decryptData: (NSData*) data withKey: (NSData*) key
{
//Key to Data
//NSData *key = [keyString dataUsingEncoding:NSUTF8StringEncoding];
// Init cryptor
CCCryptorRef cryptor = NULL;
//Empty IV: initialization vector
NSData *iv = [data subdataWithRange:NSMakeRange(0, 16)];
NSData *left = [data subdataWithRange:NSMakeRange(16,data.length-16)];
// Create Cryptor
CCCryptorStatus createDecrypt = CCCryptorCreateWithMode(kCCDecrypt, // operation
kCCModeCFB, // mode CTR
kCCAlgorithmAES128,//kCCAlgorithmAES, // Algorithm
ccNoPadding, // padding
iv.bytes, // can be NULL, because null is full of zeros
key.bytes, // key
key.length, // keylength
NULL, //const void *tweak
0, //size_t tweakLength,
0, //int numRounds,
0, //CCModeOptions options,
&cryptor); //CCCryptorRef *cryptorRef
if (createDecrypt == kCCSuccess)
{
// Alloc Data Out
NSMutableData *cipherDataDecrypt = [NSMutableData dataWithLength:left.length + kCCBlockSizeAES128];
//alloc number of bytes written to data Out
size_t outLengthDecrypt;
//Update Cryptor
CCCryptorStatus updateDecrypt = CCCryptorUpdate(cryptor,
left.bytes, //const void *dataIn,
left.length, //size_t dataInLength,
cipherDataDecrypt.mutableBytes, //void *dataOut,
cipherDataDecrypt.length, // size_t dataOutAvailable,
&outLengthDecrypt); // size_t *dataOutMoved)
if (updateDecrypt == kCCSuccess)
{
//Cut Data Out with nedded length
cipherDataDecrypt.length = outLengthDecrypt;
// Data to String
//NSString* cipherFinalDecrypt = [[NSString alloc] initWithData:cipherDataDecrypt encoding:NSUTF8StringEncoding];
//Final Cryptor
CCCryptorStatus final = CCCryptorFinal(cryptor, //CCCryptorRef cryptorRef,
cipherDataDecrypt.mutableBytes, //void *dataOut,
cipherDataDecrypt.length, // size_t dataOutAvailable,
&outLengthDecrypt); // size_t *dataOutMoved)
if (final == kCCSuccess)
{
//Release Cryptor
//CCCryptorStatus release =
CCCryptorRelease(cryptor); //CCCryptorRef cryptorRef
}
return cipherDataDecrypt ;//cipherFinalDecrypt;
}
}
else
{
//error
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment