AES128
AES128加密
+ (NSData *)encryptAES128:(NSData *)contentData andkey:(NSData *)gkey {
char keyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[gkey getBytes:keyPtr length:sizeof(keyPtr)];
NSData* data = [[NSData alloc]initWithData:contentData];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength +kCCBlockSizeAES128;
void *buffer =malloc(bufferSize);
size_t numBytesEncrypted =0;
CCCryptorStatus cryptStatus =CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
NULL,
[data bytes],
dataLength,
buffer,
bufferSize,
&numBytesEncrypted);
if(cryptStatus ==kCCSuccess) {
NSMutableData*resultData = [NSMutableData dataWithBytes:buffer length:numBytesEncrypted];
return resultData;
}
free(buffer);
return nil;
}
AES128解密
+ (NSData *)decryptADE128:(NSData *)encryptData andkey:(NSData*)gkey {
char keyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[gkey getBytes:keyPtr length:sizeof(keyPtr)];
NSData *data= [[NSData alloc]initWithData:encryptData];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength +kCCBlockSizeAES128;
void *buffer =malloc(bufferSize);
size_t numBytesCrypted =0;
CCCryptorStatus cryptStatus =CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
NULL,
[data bytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if(cryptStatus ==kCCSuccess) {
NSMutableData*resultData = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesCrypted];
return resultData;
}
free(buffer);
return nil;
}
3DES
3DES加密
/**
3DES加密
@param contentData 需要加密的內(nèi)容(NSData)
@param gkey 加密密鑰(NSData)
@return 加密好的內(nèi)容(NSData)
*/
+ (NSData*)encrypt3DES:(NSData*)contentData andkey:(NSData*)gkey {
NSData *keydata;
if(gkey.length < 24) {
NSMutableData *dddd = [[NSMutableData alloc] initWithData:gkey];
[dddd appendData:gkey];
[dddd appendData:gkey];
keydata = [dddd subdataWithRange:NSMakeRange(0,24)];
dddd = nil;
} else {
keydata = gkey;
}
size_t plainTextBufferSize = [contentData length];
const void *vplainText = (const void*)[contentData bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr =NULL;
size_t bufferPtrSize =0;
size_t movedBytes =0;
bufferPtrSize = (plainTextBufferSize +kCCBlockSize3DES) & ~(kCCBlockSize3DES-1);
bufferPtr = malloc( bufferPtrSize *sizeof(uint8_t));
memset((void*)bufferPtr,0x0, bufferPtrSize);
const void *keyvoid = (const void*)[keydata bytes];
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithm3DES,//3DES
kCCOptionPKCS7Padding|kCCOptionECBMode,//設(shè)置模式
keyvoid,//解密key
kCCKeySize3DES,
nil,//偏移量肋联,這里不用,設(shè)置為nil;不用的話脖律,必須為nil,不可以為@“”
vplainText,
plainTextBufferSize,
(void*)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = [NSData dataWithBytes:(const void*)bufferPtr length:(NSUInteger)movedBytes];
return myData;
}
3DES解密
/**
3DES解密
@param encryptData 需要解密的內(nèi)容(NSData)
@param gkey 解密密鑰(NSData)
@return 解密好的內(nèi)容(NSData)
*/
+ (NSData *)decrypt3DES:(NSData *)encryptData andkey:(NSData*)gkey {
NSData *keydata;
if(gkey.length < 24) {
NSMutableData*dddd = [[NSMutableData alloc]initWithData:gkey];
[dddd appendData:gkey];
[dddd appendData:gkey];
keydata = [dddd subdataWithRange:NSMakeRange(0,24)];
dddd = nil;
} else {
keydata = gkey;
}
size_t plainTextBufferSize = [encryptData length];
const void*vplainText = [encryptData bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr =NULL;
size_t bufferPtrSize =0;
size_t movedBytes =0;
bufferPtrSize = (plainTextBufferSize +kCCBlockSize3DES) & ~(kCCBlockSize3DES-1);
bufferPtr =malloc( bufferPtrSize *sizeof(uint8_t));
memset((void*)bufferPtr,0x0, bufferPtrSize);
const void*keyvoid = (const void*)[keydata bytes];
ccStatus =CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyvoid,
kCCKeySize3DES,
nil,
vplainText,
plainTextBufferSize,
(void*)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = [NSData dataWithBytes:(const void*)bufferPtr length:(NSUInteger)movedBytes];
return myData;
}
DES
DES加密
//DES加密
+ (NSData*)encryptDES:(NSData*)contentData andkey:(NSData*)keydata {
size_t plainTextBufferSize = [contentData length];
const void *vplainText = (const void *)[contentData bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
const void *keyvoid = (const void*)[keydata bytes];
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding|kCCOptionECBMode,//設(shè)置模式
keyvoid,
kCCKeySizeDES,
nil,
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
return myData;
}
DES解密
//DES解密
+ (NSData *)decryptDES:(NSData *)encryptData andkey:(NSData*)gkey {
size_t plainTextBufferSize = [encryptData length];
const void *vplainText = [encryptData bytes];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
const void *vkey = (const void *) [gkey bytes];
ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding|kCCOptionECBMode,//設(shè)置模式
vkey,
kCCKeySizeDES,
nil,
vplainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData *myData = [NSData dataWithBytes:(const void*)bufferPtr length:(NSUInteger)movedBytes];
return myData;
}