您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)iOS如何實(shí)現(xiàn)base64加密解密通用類,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
使用示例: 將此通用類的頭文件引入到目標(biāo)類后,直接使用類名進(jìn)行調(diào)用即可。
NSString *str = [NSString stringWithFormat:@"YWE="]; NSString *str1 = [NSString stringWithFormat:@"aa"]; NSLog(@"resultStr========%@",[CommonFunc textFromBase64String:str]); //使用類名進(jìn)行調(diào)用 NSLog(@"resultStr=========%@",[CommonFunc base64StringFromText:str1]); //使用類名進(jìn)行調(diào)用
在項(xiàng)目中遇到字符串的base64編解碼,分享一下工具類:
JKCommonFunc.h
#define __BASE64( text ) [CommonFunc base64StringFromText:text] #define __TEXT( base64 ) [CommonFunc textFromBase64String:base64] #import <Foundation/Foundation.h> @interface JKCommonFunc : NSObject /****************************************************************************** 函數(shù)名稱 : + (NSString *)base64StringFromText:(NSString *)text 函數(shù)描述 : 將文本轉(zhuǎn)換為base64格式字符串 輸入?yún)?shù) : (NSString *)text 文本 輸出參數(shù) : N/A 返回參數(shù) : (NSString *) base64格式字符串 備注信息 : ******************************************************************************/ + (NSString *)base64StringFromText:(NSString *)text; /****************************************************************************** 函數(shù)名稱 : + (NSString *)textFromBase64String:(NSString *)base64 函數(shù)描述 : 將base64格式字符串轉(zhuǎn)換為文本 輸入?yún)?shù) : (NSString *)base64 base64格式字符串 輸出參數(shù) : N/A 返回參數(shù) : (NSString *) 文本 備注信息 : ******************************************************************************/ + (NSString *)textFromBase64String:(NSString *)base64; @end
JKCommonFunc.m
#import "CommonFunc.h" //引入IOS自帶密碼庫 #import <CommonCrypto/CommonCryptor.h> //空字符串 #define LocalStr_None @"" static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @implementation CommonFunc + (NSString *)base64StringFromText:(NSString *)text { if (text && ![text isEqualToString:LocalStr_None]) { //取項(xiàng)目的bundleIdentifier作為KEY 改動(dòng)了此處 //NSString *key = [[NSBundle mainBundle] bundleIdentifier]; NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding]; //IOS 自帶DES加密 Begin 改動(dòng)了此處 //data = [self DESEncrypt:data WithKey:key]; //IOS 自帶DES加密 End return [self base64EncodedStringFrom:data]; } else { return LocalStr_None; } } + (NSString *)textFromBase64String:(NSString *)base64 { if (base64 && ![base64 isEqualToString:LocalStr_None]) { //取項(xiàng)目的bundleIdentifier作為KEY 改動(dòng)了此處 //NSString *key = [[NSBundle mainBundle] bundleIdentifier]; NSData *data = [self dataWithBase64EncodedString:base64]; //IOS 自帶DES解密 Begin 改動(dòng)了此處 //data = [self DESDecrypt:data WithKey:key]; //IOS 自帶DES加密 End return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } else { return LocalStr_None; } } /****************************************************************************** 函數(shù)名稱 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key 函數(shù)描述 : 文本數(shù)據(jù)進(jìn)行DES加密 輸入?yún)?shù) : (NSData *)data (NSString *)key 輸出參數(shù) : N/A 返回參數(shù) : (NSData *) 備注信息 : 此函數(shù)不可用于過長(zhǎng)文本 ******************************************************************************/ + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key { char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [data length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; voidvoid *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:numBytesEncrypted]; } free(buffer); return nil; } /****************************************************************************** 函數(shù)名稱 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key 函數(shù)描述 : 文本數(shù)據(jù)進(jìn)行DES解密 輸入?yún)?shù) : (NSData *)data (NSString *)key 輸出參數(shù) : N/A 返回參數(shù) : (NSData *) 備注信息 : 此函數(shù)不可用于過長(zhǎng)文本 ******************************************************************************/ + (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key { char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [data length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; voidvoid *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; } /****************************************************************************** 函數(shù)名稱 : + (NSData *)dataWithBase64EncodedString:(NSString *)string 函數(shù)描述 : base64格式字符串轉(zhuǎn)換為文本數(shù)據(jù) 輸入?yún)?shù) : (NSString *)string 輸出參數(shù) : N/A 返回參數(shù) : (NSData *) 備注信息 : ******************************************************************************/ + (NSData *)dataWithBase64EncodedString:(NSString *)string { if (string == nil) [NSException raise:NSInvalidArgumentException format:nil]; if ([string length] == 0) return [NSData data]; static charchar *decodingTable = NULL; if (decodingTable == NULL) { decodingTable = malloc(256); if (decodingTable == NULL) return nil; memset(decodingTable, CHAR_MAX, 256); NSUInteger i; for (i = 0; i < 64; i++) decodingTable[(short)encodingTable[i]] = i; } const charchar *characters = [string cStringUsingEncoding:NSASCIIStringEncoding]; if (characters == NULL) // Not an ASCII string! return nil; charchar *bytes = malloc((([string length] + 3) / 4) * 3); if (bytes == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (YES) { char buffer[4]; short bufferLength; for (bufferLength = 0; bufferLength < 4; i++) { if (characters[i] == '\0') break; if (isspace(characters[i]) || characters[i] == '=') continue; buffer[bufferLength] = decodingTable[(short)characters[i]]; if (buffer[bufferLength++] == CHAR_MAX) // Illegal character! { free(bytes); return nil; } } if (bufferLength == 0) break; if (bufferLength == 1) // At least two characters are needed to produce one byte! { free(bytes); return nil; } // Decode the characters in the buffer to bytes. bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4); if (bufferLength > 2) bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2); if (bufferLength > 3) bytes[length++] = (buffer[2] << 6) | buffer[3]; } bytes = realloc(bytes, length); return [NSData dataWithBytesNoCopy:bytes length:length]; } /****************************************************************************** 函數(shù)名稱 : + (NSString *)base64EncodedStringFrom:(NSData *)data 函數(shù)描述 : 文本數(shù)據(jù)轉(zhuǎn)換為base64格式字符串 輸入?yún)?shù) : (NSData *)data 輸出參數(shù) : N/A 返回參數(shù) : (NSString *) 備注信息 : ******************************************************************************/ + (NSString *)base64EncodedStringFrom:(NSData *)data { if ([data length] == 0) return @""; charchar *characters = malloc((([data length] + 2) / 3) * 4); if (characters == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (i < [data length]) { char buffer[3] = {0,0,0}; short bufferLength = 0; while (bufferLength < 3 && i < [data length]) buffer[bufferLength++] = ((charchar *)[data bytes])[i++]; // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary. characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2]; characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)]; if (bufferLength > 1) characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)]; else characters[length++] = '='; if (bufferLength > 2) characters[length++] = encodingTable[buffer[2] & 0x3F]; else characters[length++] = '='; } return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES]; } @end
關(guān)于“iOS如何實(shí)現(xiàn)base64加密解密通用類”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。