#import <Foundation/Foundation.h> #import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> #import "NSData+AES.h" @interface NSString (AES) //加密字符串 - (NSString*)aes128Encrypt:(NSString *)aKey; //解密字符串 - (NSString*)aes128Decrypt:(NSString *)aKey; @end
#define gIv @"0102030405060708" //自行修改16位 -->偏移量 @implementation NSString (AES256) - (NSString *)aes128Encrypt:(NSString *)aKey { char keyPtr[kCCKeySizeAES128+]; memset(keyPtr, , sizeof(keyPtr)); [aKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; char ivPtr[kCCBlockSizeAES128+]; memset(ivPtr, , sizeof(ivPtr)); [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger dataLength = [data length]; int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128); int newSize = ; if(diff > ) { newSize = dataLength + diff; } char dataPtr[newSize]; memcpy(dataPtr, [data bytes], [data length]); for(int i = ; i < diff; i++) { dataPtr[i + dataLength] = 0x00; } size_t bufferSize = newSize + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); memset(buffer, , bufferSize); size_t numBytesCrypted = ; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, // 补码方式 keyPtr, kCCKeySizeAES128, ivPtr, dataPtr, sizeof(dataPtr), buffer, bufferSize, &numBytesCrypted); if (cryptStatus == kCCSuccess) { NSData *result = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted]; if (result && result.length > ) { Byte *datas = (Byte*)[result bytes]; NSMutableString *output = [NSMutableString stringWithCapacity:result.length * ]; for(int i = ; i < result.length; i++){ [output appendFormat:@"%02x", datas[i]]; } return output; } } free(buffer); return nil; } - (NSString *)aes128Decrypt:(NSString *)aKey { char keyPtr[kCCKeySizeAES128 + ]; memset(keyPtr, , sizeof(keyPtr)); [aKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; char ivPtr[kCCBlockSizeAES128 + ]; memset(ivPtr, , sizeof(ivPtr)); [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; NSMutableData *data = [NSMutableData dataWithCapacity:self.length / ]; unsigned char whole_byte; char byte_chars[] = {'\0','\0','\0'}; int i; for (i=; i < [self length] / ; i++) { byte_chars[] = [self characterAtIndex:i*]; byte_chars[] = [self characterAtIndex:i*+]; whole_byte = strtol(byte_chars, NULL, ); [data appendBytes:&whole_byte length:]; } NSUInteger dataLength = [data length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesCrypted = ; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCBlockSizeAES128, ivPtr, [data bytes], dataLength, buffer, bufferSize, &numBytesCrypted); if (cryptStatus == kCCSuccess) { NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted]; return [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding]; } free(buffer); return nil; } @end
iOS Objective c 16进制字符串转为二进制数组
原文:http://lizhuang.iteye.com/blog/2060143
@implementation NSString (StringToHexData) //
// Decodes an NSString containing hex encoded bytes into an NSData object
//
- (NSData *) stringToHexData
{
int len = [self length] / ; // Target length
unsigned char *buf = malloc(len)
unsigned char *whole_byte = buf;
char byte_chars[] = {'\0','\0','\0'}; int i;
for (i=; i < [self length] / ; i++) {
byte_chars[] = [self characterAtIndex:i*];
byte_chars[] = [self characterAtIndex:i*+];
*whole_byte = strtol(byte_chars, NULL, );
whole_byte++;
} NSData *data = [NSData dataWithBytes:buf length:len];
free( buf );
return data;
}
@end
@implementation NSData (DataToHexString) - (NSString *) dataToHexString
{
NSUInteger len = [self length];
char * chars = (char *)[self bytes];
NSMutableString * hexString = [[NSMutableString alloc] init]; for(NSUInteger i = ; i < len; i++ )
[hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]]; return hexString;
}
@end
16进制-