DES+Base64 加密解密的方法
为了保护存储文件不被无聊的吊丝破解,对存储文件里一些明感数据加密还是必须的。
之前自己参考资料写了个Base64,没调试通,死于胎腹。之后在网上找到个GTMBase64,Google Toolbox for Mac。
EncryptUtil.h
1 //
2 // EncryptUtil.h
3 //
4 // Created by Wu Kurodo on 12-6-27.
5 // Copyright (c) 2012年 Kurodo Inc. All rights reserved.
6 //
7
8 #import <Foundation/Foundation.h>
9
10 @interface EncryptUtil : NSObject
11
12 + (NSString *)encryptWithText:(NSString *)sText;
13 + (NSString *)decryptWithText:(NSString *)sText;
14
15 @end
2 // EncryptUtil.h
3 //
4 // Created by Wu Kurodo on 12-6-27.
5 // Copyright (c) 2012年 Kurodo Inc. All rights reserved.
6 //
7
8 #import <Foundation/Foundation.h>
9
10 @interface EncryptUtil : NSObject
11
12 + (NSString *)encryptWithText:(NSString *)sText;
13 + (NSString *)decryptWithText:(NSString *)sText;
14
15 @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 <CommonCrypto/CommonCryptor.h>
#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
// EncryptUtil.m
//
// Created by Wu Kurodo on 12-6-27.
// Copyright (c) 2012年 Kurodo Inc. All rights reserved.
//
#import "EncryptUtil.h"
#import <CommonCrypto/CommonCryptor.h>
#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
效果如下