DES算法
☆提供高质量的数据保护,防止数据未经授权的泄露和未被察觉的修改
☆具有相当高的复杂性,使得破译的开销超过可能获得的利益,同时又要便于理解和掌握
☆DES密码体制的安全性应该不依赖于算法的保密,其安全性仅以加密密钥的保密为基础
☆实现经济,运行有效,并且适用于多种完全不同的应用
苹果本身支持DES加密,在项目中引入头文件 CommonCrypto/CommonCryptor.h 即可使用相关函数.
我自己对其进行了封装,支持ARC与非ARC
YXCrypto.h
// // YXCrypto.h // 用秘钥给字符串加密或者解密 // // Created by YouXian on 14-3-18. // Copyright (c) 2014年 YouXian. All rights reserved. // #import <Foundation/Foundation.h> @interface YXCrypto : NSObject /*! * 给字符串加密 */ + (NSString *)DesEncryptString:(NSString*)src WithKey:(NSString *)key; /*! * 给字符串解密 */ + (NSString *)DesDecryptString:(NSString*)src WithKey:(NSString *)key; @end
YXCrypto.m
// // YXCrypto.m // 用秘钥给字符串加密或者解密 // // Created by YouXian on 14-3-18. // Copyright (c) 2014年 YouXian. All rights reserved. // #import "YXCrypto.h" #import <CommonCrypto/CommonCryptor.h> #if __has_feature(objc_arc) // ARC #define Auto_Release(obj) #define Safe_Release(obj) #else // 非ARC #define Auto_Release(obj) [obj autorelease] #define Safe_Release(obj) [obj release]; obj = nil #endif static YXCrypto *shareInstance = nil; @implementation YXCrypto /*! * 给字符串加密 */ + (NSString *)DesEncryptString:(NSString*)src WithKey:(NSString *)key { NSString* strRet = @""; if (shareInstance == nil) { shareInstance = [[YXCrypto alloc] init]; } // encrypt source content NSData* bytes = [src dataUsingEncoding:NSUTF8StringEncoding]; NSData* data = [shareInstance DesCryptWithOperation:kCCEncrypt bytes:bytes key:key]; // format bytes to visible string char* pBuff = (char*)[data bytes]; for (int i=0; i<data.length; i++) { strRet = [strRet stringByAppendingFormat:@"%02X", pBuff[i]& 0xFF]; } return strRet; } /*! * 给字符串解密 */ + (NSString *)DesDecryptString:(NSString*)src WithKey:(NSString *)key { if (shareInstance == nil) { shareInstance = [[YXCrypto alloc] init]; } static unsigned char _map_ch2hex[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0, 0, 0, 0, 0, 0, 0, // :, ;, <, =, >, ?, @, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, }; // decode source content to bytes unsigned char* bytes = (unsigned char*)malloc((src.length+1)*sizeof(unsigned char)); [[src uppercaseString] getCString:(char*)bytes maxLength:src.length+1 encoding:NSUTF8StringEncoding]; unsigned char *p1 = bytes, *p2 = bytes; int n = src.length/2; for (int i=0; i<n; i++) { *p1 = _map_ch2hex[*p2-'0'] * 0x10 + _map_ch2hex[*(p2+1)-'0']; p1++; p2+=2; } NSData* data = [NSData dataWithBytes:bytes length:n]; // decrypt source bytes NSData* dataOut = [shareInstance DesCryptWithOperation:kCCDecrypt bytes:data key:key]; free(bytes); NSString* strRet = [[NSString alloc] initWithData:dataOut encoding:NSUTF8StringEncoding]; Auto_Release(strRet); return strRet; } - (NSData *)DesCryptWithOperation:(CCOperation)operation bytes:(NSData*)bytes key:(NSString *)key { NSUInteger dataLength = [bytes length]; size_t bufferSize = ([bytes length] + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1); unsigned char *buffer = (unsigned char *)malloc(bufferSize*sizeof(unsigned char)); memset((void*)buffer, 0, bufferSize); size_t numBytesCrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(operation, kCCAlgorithmDES, kCCOptionPKCS7Padding | kCCOptionECBMode, (void const*)[key UTF8String], kCCKeySizeDES, NULL, [bytes bytes], dataLength, (void*)buffer, bufferSize, &numBytesCrypted); NSData* dataRet = nil; if (cryptStatus == kCCSuccess) { dataRet = [[NSData alloc] initWithBytes:buffer length:numBytesCrypted]; Auto_Release(dataRet); } free(buffer); return dataRet; } @end
使用:
附录1:https://github.com/alfaromeodev/Cryptor
// // Cryptor.h // test // // Created by Da Zhang on 11/5/12. // Copyright 2012 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> @interface Cryptor : NSObject { } /* all the methods below only support utf8 string */ + (NSString *)encodeMD5:(NSString *)str; + (NSString *)encodeDES:(NSString *)plainString key:(NSString *)key; + (NSString *)decodeDES:(NSString *)decodedString key:(NSString*)key; + (NSString *)encodeBase64:(NSString *)plainString; + (NSString *)decodeBase64:(NSString *)decodedString; @end
// // Cryptor.m // test // // Created by Da Zhang on 11/5/12. // Copyright 2012 __MyCompanyName__. All rights reserved. // #import "Cryptor.h" @interface Cryptor () + (NSString *)encodeBase64WithData:(NSData *)objData; + (NSData *)decodeBase64WithUTF8String:(NSString *)strBase64; + (NSString *)parseByte2HexString:(Byte *)bytes; @end static const char _base64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const short _base64DecodingTable[256] = { -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -1, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2, -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 }; @implementation Cryptor #pragma mark private section + (NSData *)decodeBase64WithUTF8String:(NSString *)strBase64 { const char * objPointer = [strBase64 cStringUsingEncoding:NSUTF8StringEncoding]; int intLength = strlen(objPointer); int intCurrent; int i = 0, j = 0, k; unsigned char * objResult; objResult = calloc(intLength, sizeof(char)); // Run through the whole string, converting as we go while ( ((intCurrent = *objPointer++) != '