背景:
當app與硬件交互時,雙方寫入讀取數(shù)據(jù)的協(xié)議都是根據(jù)字節(jié)位數(shù)來的建瘫,寫入讀出數(shù)據(jù)解析時崭捍,使用NSString,NSData的16進制互轉(zhuǎn)方便解析提高代碼編寫速度啰脚。
代碼如下:
.h文件===============
#import <Foundation/Foundation.h>
@interface NSData (Category)
+(NSData*)dataWithHexString:(NSString*)str;
+(NSString*)hexStringWithData:(NSData*)data;
+ (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key;
+ (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key;
+ (NSData *)threeDESDecrypt:(NSData *)data WithKey:(NSString *)key;
@end
.m文件===============
#import "NSData+Category.h"
@implementation NSData (Category)
#pragma mark-----將十六進制數(shù)據(jù)轉(zhuǎn)換成NSData
+(NSData*)dataWithHexString:(NSString*)str{
if (!str || [str length] == 0) {
return nil;
}
NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
NSRange range;
if ([str length] % 2 == 0) {
range = NSMakeRange(0, 2);
} else {
range = NSMakeRange(0, 1);
}
for (NSInteger i = range.location; i < [str length]; i += 2) {
unsigned int anInt;
NSString *hexCharStr = [str substringWithRange:range];
NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
[scanner scanHexInt:&anInt];
NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
[hexData appendData:entity];
range.location += range.length;
range.length = 2;
}
return hexData;
}
#pragma mark - 將傳入的NSData類型轉(zhuǎn)換成NSString并返回
+(NSString *)hexStringWithData:(NSData *)data
{
const unsigned char* dataBuffer = (const unsigned char*)[data bytes];
if(!dataBuffer){
return nil;
}
NSUInteger dataLength = [data length];
NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for(int i = 0; i < dataLength; i++){
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
}
NSString* result = [NSString stringWithString:hexString];
return result;
}
//DES加密
+ (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
{
// char keyPtr[kCCKeySizeAES256+1];
// bzero(keyPtr, sizeof(keyPtr));
//
// [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
const void *keyPtr = (const void *) [[NSData dataWithHexString:key] bytes];
NSUInteger dataLength = [data length];
// NSLog(@"%ld",dataLength);
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeDES,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:8];
}
free(buffer);
return nil;
}
//DES解密
+ (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key
{
// char keyPtr[kCCKeySizeAES256+1];
// bzero(keyPtr, sizeof(keyPtr));
//
// [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
const void *keyPtr = (const void *) [[NSData dataWithHexString:key] bytes];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeDES,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
+ (NSData *)threeDESDecrypt:(NSData *)data WithKey:(NSString *)key
{
NSString *key1 = [key substringWithRange:NSMakeRange(0, 16)];
NSString *key2 = [key substringFromIndex:key.length-16];
NSData *data1 = [self DESDecrypt:data WithKey:key1];
NSData *data2 = [self DESEncrypt:data1 WithKey:key2];
NSData *data3 = [self DESDecrypt:data2 WithKey:key1];
return data3;
}
@end