常在一些网站源码中看到&#x开头的内容,这是转化成unicode编码后的汉字。
--------------------------------------------unicode编码后的汉字(呵呵)转化成中文(呵呵)---------------------------------------
step1:unicode解码(呵呵 ---------------- u5475u5475)
+(NSString *)change:(NSString *)str { NSString *changeStr = @""; if (!str||[str isEqualToString:@""]) { return @""; } changeStr = [str stringByReplacingOccurrencesOfString:@"&#x" withString:@"\u"]; changeStr = [changeStr stringByReplacingOccurrencesOfString:@";" withString:@""]; return changeStr; }
step2:unicode转UTF-8(u5475u5475 ---------------- 呵呵)
+(NSString *) unicodeToUtf8:(NSString *)string { NSString *tempStr1 = [string stringByReplacingOccurrencesOfString:@"\u" withString:@"\U"]; NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@""" withString:@"\""]; NSString *tempStr3 = [[@""" stringByAppendingString:tempStr2] stringByAppendingString:@"""]; NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding]; NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL]; return [returnStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "]; }
------------------------------------------------------------UrlEncode--------------------------------------------------
对UTF-8中文UrlEncode编码(呵呵------------------------%e5%91%b5%e5%91%b5)
- (NSString *)URLEncodedString { NSString *encodedString = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8); return encodedString; } - (NSString*)URLDecodedString { NSString *result = ( NSString *) CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8)); return result; }
对gb2312中文UrlEncode编码(呵呵-----------------------%ba%c7%ba%c7)
MD5加密
#import "CommonCrypto/CommonDigest.h" @implementation MD5 +(NSString *) md5: (NSString *) inPutText { const char *cStr = [inPutText UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStr, strlen(cStr), result); return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ] lowercaseString]; } +(NSString *) MD5: (NSString *) inPutText { const char *cStr = [inPutText UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStr, strlen(cStr), result); return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ] uppercaseString]; }
.
#import <CommonCrypto/CommonDigest.h> //生成随机guid串 (全球唯一标识符) - (NSString *)getUniqueStrByUUID { CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID //get the string representation of the UUID NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj); CFRelease(uuidObj); return uuidString ; } ////获取系统当前的时间戳 (*1000代表毫秒级) -(NSString *)getTimeInterval{ UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000; return [NSString stringWithFormat:@"%llu",recordTime]; } //sha1(安全哈希算法) - (NSString *)sha1:(NSString *)input { const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:input.length]; uint8_t digest[CC_SHA1_DIGEST_LENGTH]; CC_SHA1(data.bytes, data.length, digest); NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) { [output appendFormat:@"%02x", digest[i]]; } return output; }
对称加密代表AES,NSData进行AES加密的扩展,Key是和后台约定的key。
#import <CommonCrypto/CommonCryptor.h> @implementation NSData (SDK_Encrypt) - (NSData *)sdk_AESEncryptWithKey:(NSString *)key { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); char iv[kCCKeySizeAES128]; bzero(iv, sizeof(iv)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, iv /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); //free the buffer; return nil; } - (NSData *)sdk_AESDecryptWithKey:(NSString *)key { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); char iv[kCCKeySizeAES128]; bzero(iv, sizeof(iv)); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, iv /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesDecrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); //free the buffer; return nil; } @end
RSA加密解密相关:
需要证书文件(*.der)及p12文件(需记录密码)
公钥的作用加密,私钥的作用解密。
http://www.jianshu.com/p/2927ca2b3719
http://witcheryne.iteye.com/blog/2171850