DES+Base64 加密解密的方法

为了保护存储文件不被无聊的吊丝破解,对存储文件里一些明感数据加密还是必须的。 

之前自己参考资料写了个Base64,没调试通,死于胎腹。之后在网上找到个GTMBase64Google Toolbox for Mac。

EncryptUtil.h 

//
//  EncryptUtil.h
//
//  Created by Wu Kurodo on 12-6-27.
//  Copyright (c) 2012年 Kurodo Inc. All rights reserved.
//

#import 

@interface EncryptUtil : NSObject

+ (NSString *)encryptWithText:(NSString *)sText;
+ (NSString *)decryptWithText:(NSString *)sText;

@end

EncryptUtil.m

//

//  EncryptUtil.m

//

//  Created by Wu Kurodo on 12-6-27.

//  Copyright (c) 2012年 Kurodo Inc. All rights reserved.

//



#import "EncryptUtil.h"

#import 

#import "GTMBase64.h"



@implementation EncryptUtil



+ (NSString *)encrypt:(NSString *)sText encryptOrDecrypt:(CCOperation)encryptOperation key:(NSString *)key

{

    const void *vplainText;

    size_t plainTextBufferSize;

    

    if (encryptOperation == kCCDecrypt)

    {

        NSData *decryptData = [GTMBase64 decodeData:[sText dataUsingEncoding:NSUTF8StringEncoding]];

        plainTextBufferSize = [decryptData length];

        vplainText = [decryptData bytes];

    }

    else

    {

        NSData* encryptData = [sText dataUsingEncoding:NSUTF8StringEncoding];

        plainTextBufferSize = [encryptData length];

        vplainText = (const void *)[encryptData bytes];

    }

    

    CCCryptorStatus ccStatus;

    uint8_t *bufferPtr = NULL;

    size_t bufferPtrSize = 0;

    size_t movedBytes = 0;

    

    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);

    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));

    memset((void *)bufferPtr, 0x0, bufferPtrSize);

    

    NSString *initVec = @"init Kurodo";

    const void *vkey = (const void *) [key UTF8String];

    const void *vinitVec = (const void *) [initVec UTF8String];

    

    ccStatus = CCCrypt(encryptOperation,

                       kCCAlgorithm3DES,

                       kCCOptionPKCS7Padding,

                       vkey,

                       kCCKeySize3DES,

                       vinitVec, 

                       vplainText,

                       plainTextBufferSize,

                       (void *)bufferPtr,

                       bufferPtrSize,

                       &movedBytes);

    

    NSString *result = nil;

    

    if (encryptOperation == kCCDecrypt)

    {

        result = [[[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding] autorelease];

    }

    else

    {

        NSData *data = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];

        result = [GTMBase64 stringByEncodingData:data];

    }

    

    return result;

}



+ (NSString *)encryptWithText:(NSString *)sText

{

    return [self encrypt:sText encryptOrDecrypt:kCCEncrypt key:@"Kurodo"];

}



+ (NSString *)decryptWithText:(NSString *)sText

{

    return [self encrypt:sText encryptOrDecrypt:kCCDecrypt key:@"Kurodo"];

}

@end


你可能感兴趣的:(ios,苹果开发)