2012-08-11 83 views
0

我devloping一個應用程序,它將大量的數據存儲在sqlite數據庫中。但我想保持數據安全,以便沒有人侵入我的數據庫並查看數據庫中的數據。iPhone中的Sqlite數據庫保護

現在我該如何加密和解密我的sqlite數據庫?

請幫我。

謝謝。

回答

2

我的開發,需要一個應用程序,我已經使用

首先,我發起集體這樣

,把它稱爲 ConstantMethods.h+m

ConstantMethods.h

#import <Foundation/Foundation.h> 
#import <CommonCrypto/CommonCryptor.h> 

static NSString *keyEncryption = @"YourPrivateKey"; 
@interface ConstantMethods : NSObject { 

} 
+ (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key; 
+ (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key; 

@end 

ConstantMethods.m

#import "ConstantMethods.h" 

@implementation NSData (AES256) 

- (NSData *)AES256EncryptWithKey:(NSString *)key { 
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise 
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

    // fetch key data 
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

    NSUInteger dataLength = [self length]; 

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block. 
    //That's why we need to add the size of one block here 
    size_t bufferSize = dataLength + kCCBlockSizeAES128; 
    void *buffer = malloc(bufferSize); 

    size_t numBytesEncrypted = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES256, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesEncrypted); 
    if (cryptStatus == kCCSuccess) { 
     //the returned NSData takes ownership of the buffer and will free it on deallocation 
     return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 
    } 

    free(buffer); //free the buffer; 
    return nil; 
} 

- (NSData *)AES256DecryptWithKey:(NSString *)key { 
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise 
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

    // fetch key data 
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

    NSUInteger dataLength = [self length]; 

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block. 
    //That's why we need to add the size of one block here 
    size_t bufferSize = dataLength + kCCBlockSizeAES128; 
    void *buffer = malloc(bufferSize); 

    size_t numBytesDecrypted = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES256, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesDecrypted); 

    if (cryptStatus == kCCSuccess) { 
     //the returned NSData takes ownership of the buffer and will free it on deallocation 
     return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; 
    } 

    free(buffer); //free the buffer; 
    return nil; 
} 

@end 

@implementation ConstantMethods 

+ (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key { 
    return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key]; 
} 

+ (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key { 
    return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key] 
           encoding:NSUTF8StringEncoding] autorelease]; 
} 

@end 

然後,在您的課堂上,導入ConstantMethods.h和使用類,如下:

[ConstantMethods encryptString:@"yourString"]; 

,當你要解密,使用以下命令:

[ConstantMethods decryptData:@"yourString" withKey:keyEncryption]; 

加密後,加密的數據插入到數據庫中,並檢索時來自數據庫的數據,解密它。

我希望這可以幫助你。

+0

感謝您的即時回覆。 我只是有點懷疑我這樣做,就像我將nsdata轉換爲nsstring一樣 NSData * test = [ConstantMethods encryptString:InsertName withKey:keyEncryption]; NSString * newStr = [NSString stringWithUTF8String:[test bytes]]; \t \t sqlite3_bind_text(addStmt,1,[newStr UTF8String],-1,SQLITE_TRANSIENT); – user1557103 2012-08-11 07:44:05

+0

順便說一句,爲什麼你使用SQLite,使用核心數據,它更友好和可讀,如果你喜歡,看到我的答案對你有用,不要忘記投票並將其標記爲正確的答案,謝謝。 – Scar 2012-08-11 07:45:37

+0

嘿傷疤能夠加密數據,但是當我解密數據時,它會返回零值。 – user1557103 2012-08-11 10:04:12