iOS开发之Objective-c的AES128加密和解密算法的实现
1 #import <Foundation/Foundation.h> 2 3 #import <CommonCrypto/CommonDigest.h> 4 5 #import <CommonCrypto/CommonCryptor.h> 6 7 #import "NSData+AES.h" 8 9 @interface NSString (AES) 10 11 //加密字符串 12 13 - (NSString*)aes128Encrypt:(NSString *)aKey; 14 15 //解密字符串 16 17 - (NSString*)aes128Decrypt:(NSString *)aKey; 18 19 @end
1 #define gIv @"0102030405060708" //自行修改16位 -->偏移量 2 3 @implementation NSString (AES256) 4 5 - (NSString *)aes128Encrypt:(NSString *)aKey 6 7 { 8 9 char keyPtr[kCCKeySizeAES128+1]; 10 11 memset(keyPtr, 0, sizeof(keyPtr)); 12 13 [aKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 14 15 16 17 char ivPtr[kCCBlockSizeAES128+1]; 18 19 memset(ivPtr, 0, sizeof(ivPtr)); 20 21 [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; 22 23 24 25 NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding]; 26 27 NSUInteger dataLength = [data length]; 28 29 30 31 int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128); 32 33 int newSize = 0; 34 35 36 37 if(diff > 0) 38 39 { 40 41 newSize = dataLength + diff; 42 43 } 44 45 46 47 char dataPtr[newSize]; 48 49 memcpy(dataPtr, [data bytes], [data length]); 50 51 for(int i = 0; i < diff; i++) 52 53 { 54 55 dataPtr[i + dataLength] = 0x00; 56 57 } 58 59 60 61 size_t bufferSize = newSize + kCCBlockSizeAES128; 62 63 void *buffer = malloc(bufferSize); 64 65 memset(buffer, 0, bufferSize); 66 67 68 69 size_t numBytesCrypted = 0; 70 71 72 73 CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 74 75 kCCAlgorithmAES128, 76 77 kCCOptionPKCS7Padding, // 补码方式 78 79 keyPtr, 80 81 kCCKeySizeAES128, 82 83 ivPtr, 84 85 dataPtr, 86 87 sizeof(dataPtr), 88 89 buffer, 90 91 bufferSize, 92 93 &numBytesCrypted); 94 95 96 97 if (cryptStatus == kCCSuccess) { 98 99 NSData *result = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted]; 100 101 if (result && result.length > 0) { 102 103 104 105 Byte *datas = (Byte*)[result bytes]; 106 107 NSMutableString *output = [NSMutableString stringWithCapacity:result.length * 2]; 108 109 for(int i = 0; i < result.length; i++){ 110 111 [output appendFormat:@"%02x", datas[i]]; 112 113 } 114 115 return output; 116 117 } 118 119 } 120 121 free(buffer); 122 123 return nil; 124 125 } 126 127 128 129 - (NSString *)aes128Decrypt:(NSString *)aKey 130 131 { 132 133 char keyPtr[kCCKeySizeAES128 + 1]; 134 135 memset(keyPtr, 0, sizeof(keyPtr)); 136 137 [aKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 138 139 140 141 char ivPtr[kCCBlockSizeAES128 + 1]; 142 143 memset(ivPtr, 0, sizeof(ivPtr)); 144 145 [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; 146 147 148 149 NSMutableData *data = [NSMutableData dataWithCapacity:self.length / 2]; 150 151 unsigned char whole_byte; 152 153 char byte_chars[3] = {'\0','\0','\0'}; 154 155 int i; 156 157 for (i=0; i < [self length] / 2; i++) { 158 159 byte_chars[0] = [self characterAtIndex:i*2]; 160 161 byte_chars[1] = [self characterAtIndex:i*2+1]; 162 163 whole_byte = strtol(byte_chars, NULL, 16); 164 165 [data appendBytes:&whole_byte length:1]; 166 167 } 168 169 170 171 NSUInteger dataLength = [data length]; 172 173 size_t bufferSize = dataLength + kCCBlockSizeAES128; 174 175 void *buffer = malloc(bufferSize); 176 177 178 179 size_t numBytesCrypted = 0; 180 181 CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 182 183 kCCAlgorithmAES128, 184 185 kCCOptionPKCS7Padding, 186 187 keyPtr, 188 189 kCCBlockSizeAES128, 190 191 ivPtr, 192 193 [data bytes], 194 195 dataLength, 196 197 buffer, 198 199 bufferSize, 200 201 &numBytesCrypted); 202 203 if (cryptStatus == kCCSuccess) { 204 205 NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted]; 206 207 return [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding]; 208 209 } 210 211 free(buffer); 212 213 return nil; 214 215 } 216 217 @end
iOS Objective c 16进制字符串转为二进制数组
原文:http://lizhuang.iteye.com/blog/2060143
1 @implementation NSString (StringToHexData) 2 3 // 4 // Decodes an NSString containing hex encoded bytes into an NSData object 5 // 6 - (NSData *) stringToHexData 7 { 8 int len = [self length] / 2; // Target length 9 unsigned char *buf = malloc(len) 10 unsigned char *whole_byte = buf; 11 char byte_chars[3] = {'\0','\0','\0'}; 12 13 int i; 14 for (i=0; i < [self length] / 2; i++) { 15 byte_chars[0] = [self characterAtIndex:i*2]; 16 byte_chars[1] = [self characterAtIndex:i*2+1]; 17 *whole_byte = strtol(byte_chars, NULL, 16); 18 whole_byte++; 19 } 20 21 NSData *data = [NSData dataWithBytes:buf length:len]; 22 free( buf ); 23 return data; 24 } 25 @end 26 @implementation NSData (DataToHexString) 27 28 - (NSString *) dataToHexString 29 { 30 NSUInteger len = [self length]; 31 char * chars = (char *)[self bytes]; 32 NSMutableString * hexString = [[NSMutableString alloc] init]; 33 34 for(NSUInteger i = 0; i < len; i++ ) 35 [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]]; 36 37 return hexString; 38 } 39 @end