/** Base64.h **/ #import <Foundation/Foundation.h> @interface NSData (Base64) + (NSData *)dataWithBase64EncodedString:(NSString *)string; - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth; - (NSString *)base64EncodedString; @end @interface NSString (Base64) + (NSString *)stringWithBase64EncodedString:(NSString *)string; - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth; - (NSString *)base64EncodedString; - (NSString *)base64DecodedString; - (NSData *)base64DecodedData; @end
/** Base64.m **/ #import "Base64.h" #pragma GCC diagnostic ignored "-Wselector" #import <Availability.h> #if !__has_feature(objc_arc) #error This library requires automatic reference counting #endif @implementation NSData (Base64) + (NSData *)dataWithBase64EncodedString:(NSString *)string { if (![string length]) return nil; NSData *decoded = nil; #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9 || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 if (![NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)]) { decoded = [[self alloc] initWithBase64Encoding:[string stringByReplacingOccurrencesOfString:@"[^A-Za-z0-9+/=]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])]]; } else #endif { decoded = [[self alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters]; } return [decoded length]? decoded: nil; } - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth { if (![self length]) return nil; NSString *encoded = nil; #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9 || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 if (![NSData instancesRespondToSelector:@selector(base64EncodedStringWithOptions:)]) { //encoded = [self base64Encoding]; encoded = [self base64EncodedString]; } else #endif { switch (wrapWidth) { case 64: { return [self base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; } case 76: { return [self base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]; } default: { encoded = [self base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0]; } } } if (!wrapWidth || wrapWidth >= [encoded length]) { return encoded; } wrapWidth = (wrapWidth / 4) * 4; NSMutableString *result = [NSMutableString string]; for (NSUInteger i = 0; i < [encoded length]; i+= wrapWidth) { if (i + wrapWidth >= [encoded length]) { [result appendString:[encoded substringFromIndex:i]]; break; } [result appendString:[encoded substringWithRange:NSMakeRange(i, wrapWidth)]]; [result appendString:@"\r\n"]; } return result; } - (NSString *)base64EncodedString { return [self base64EncodedStringWithWrapWidth:0]; } @end @implementation NSString (Base64) + (NSString *)stringWithBase64EncodedString:(NSString *)string { NSData *data = [NSData dataWithBase64EncodedString:string]; if (data) { return [[self alloc] initWithData:data encoding:NSUTF8StringEncoding]; } return nil; } - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth { NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; return [data base64EncodedStringWithWrapWidth:wrapWidth]; } - (NSString *)base64EncodedString { NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; return [data base64EncodedString]; } - (NSString *)base64DecodedString { return [NSString stringWithBase64EncodedString:self]; } - (NSData *)base64DecodedData { return [NSData dataWithBase64EncodedString:self]; } @end
/** AES128.h **/ #import <Foundation/Foundation.h> @interface AES128 : NSObject +(NSString *)AES128Encrypt:(NSString *)plainText withKey:(NSString *)key; +(NSString *)processDecodedString:(NSString *)decoded; +(NSString *)AES128Decrypt:(NSString *)encryptText withKey:(NSString *)key; @end
/** AES128.h **/ #import "AES128.h" #import <CommonCrypto/CommonCryptor.h> #import "Base64.h" @implementation AES128 +(NSString *)AES128Encrypt:(NSString *)plainText withKey:(NSString *)key { if( ![self validKey:key] ){ return nil; } char keyPtr[kCCKeySizeAES128+1]; memset(keyPtr, 0, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; char ivPtr[kCCBlockSizeAES128+1]; memset(ivPtr, 0, sizeof(ivPtr)); [key getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger dataLength = [data length]; int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128); unsigned long newSize = 0; if(diff > 0) { newSize = dataLength + diff; } char dataPtr[newSize]; memcpy(dataPtr, [data bytes], [data length]); for(int i = 0; i < diff; i++) { dataPtr[i + dataLength] =0x0000; } size_t bufferSize = newSize + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); memset(buffer, 0, bufferSize); size_t numBytesCrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, 0x0000, [key UTF8String], kCCKeySizeAES128, [key UTF8String], dataPtr, sizeof(dataPtr), buffer, bufferSize, &numBytesCrypted); if (cryptStatus == kCCSuccess) { NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted]; return [resultData base64EncodedString]; } free(buffer); return nil; } +(NSString *)processDecodedString:(NSString *)decoded { if( decoded==nil || decoded.length==0 ){ return nil; } const char *tmpStr=[decoded UTF8String]; int i=0; while( tmpStr[i]!=‘\0‘ ) { i++; } NSString *final=[[NSString alloc]initWithBytes:tmpStr length:i encoding:NSUTF8StringEncoding]; return final; } +(NSString *)AES128Decrypt:(NSString *)encryptText withKey:(NSString *)key { if( ![self validKey:key] ){ return nil; } char keyPtr[kCCKeySizeAES128 + 1]; memset(keyPtr, 0, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; char ivPtr[kCCBlockSizeAES128 + 1]; memset(ivPtr, 0, sizeof(ivPtr)); [key getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; NSData *data = [encryptText base64DecodedData]; NSUInteger dataLength = [data length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesCrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0x0000, [key UTF8String], kCCBlockSizeAES128, [key UTF8String], [data bytes], dataLength, buffer, bufferSize, &numBytesCrypted); if (cryptStatus == kCCSuccess) { NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted]; NSString *decoded=[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding]; return [self processDecodedString:decoded]; } free(buffer); return nil; } +(BOOL)validKey:(NSString*)key { if( key==nil || key.length !=16 ){ return NO; } return YES; } -(NSString *)processDecodedString:(NSString *)decoded { if( decoded==nil || decoded.length==0 ){ return nil; } const char *tmpStr=[decoded UTF8String]; int i=0; while( tmpStr[i]!=‘\0‘ ) { i++; } NSString *final=[[NSString alloc]initWithBytes:tmpStr length:i encoding:NSUTF8StringEncoding]; return final; }
时间: 2024-10-27 21:36:50