des.h
#import <Foundation/Foundation.h> #import <CommonCrypto/CommonCryptor.h> @interface Des : NSObject //DES加密 + (NSString *) encryptStr:(NSString *) str key:(NSString *)sKey; //DES解密 + (NSString *) decryptStr:(NSString *) str key:(NSString *)sKey;
des.m
#import "Des.h" static const char _bas64EncodingTable[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 Des + (NSString *) encryptStr:(NSString *) str key:(NSString *)sKey { return [Des doCipher:str key:sKey context:kCCEncrypt]; } + (NSString *) decryptStr:(NSString *) str key:(NSString *)sKey { return [Des doCipher:str key:sKey context:kCCDecrypt]; } + (NSString *)doCipher:(NSString *)sTextIn key:(NSString *)sKey context:(CCOperation)encryptOrDecrypt { NSStringEncoding EnC = NSUTF8StringEncoding; NSMutableData * dTextIn; if (encryptOrDecrypt == kCCDecrypt) { dTextIn = [[Des decodeBase64WithString:sTextIn] mutableCopy]; } else{ dTextIn = [[sTextIn dataUsingEncoding: EnC] mutableCopy]; } NSMutableData * dKey = [[sKey dataUsingEncoding:EnC] mutableCopy]; [dKey setLength:kCCBlockSizeDES]; uint8_t *bufferPtr1 = NULL; size_t bufferPtrSize1 = 0; size_t movedBytes1 = 0; Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; bufferPtrSize1 = ([sTextIn length] + kCCKeySizeDES) & ~(kCCKeySizeDES -1); bufferPtr1 = malloc(bufferPtrSize1 * sizeof(uint8_t)); memset((void *)bufferPtr1, 0x00, bufferPtrSize1); CCCrypt(encryptOrDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [dKey bytes], [dKey length], iv, [dTextIn bytes], [dTextIn length], (void*)bufferPtr1, bufferPtrSize1, &movedBytes1); NSString * sResult; if (encryptOrDecrypt == kCCDecrypt){ sResult = [[ NSString alloc] initWithData:[NSData dataWithBytes:bufferPtr1 length:movedBytes1] encoding:EnC] ; } else { NSData *dResult = [NSData dataWithBytes:bufferPtr1 length:movedBytes1]; sResult = [Des encodeBase64WithData:dResult]; } return sResult; } + (NSString *)encodeBase64WithData:(NSData *)objData { const unsigned char * objRawData = [objData bytes]; char * objPointer; char * strResult; int intLength = [objData length]; if (intLength == 0) return nil; strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char)); objPointer = strResult; while(intLength > 2) { *objPointer++ = _bas64EncodingTable[objRawData[0] >> 2]; *objPointer++ = _bas64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)]; *objPointer++ = _bas64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)]; *objPointer++ = _bas64EncodingTable[objRawData[2] & 0x3f]; objRawData += 3; intLength -= 3; } if (intLength != 0) { *objPointer++ = _bas64EncodingTable[objRawData[0] >> 2]; if (intLength > 1) { *objPointer++ = _bas64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)]; *objPointer++ = _bas64EncodingTable[(objRawData[1] & 0x0f) << 2]; *objPointer++ = '='; } else { *objPointer++ = _bas64EncodingTable[(objRawData[0] & 0x03) << 4]; *objPointer++ = '='; *objPointer++ = '='; } } *objPointer = '