【问题标题】:Sqlite database Protection in iPhoneiPhone中的Sqlite数据库保护
【发布时间】:2012-08-08 09:05:20
【问题描述】:

我正在开发一个在 sqlite 数据库中存储大量数据的应用程序。但我想保证数据的安全,以免有人入侵我的数据库并查看我数据库中的数据。

现在如何加密和解密我的 sqlite 数据库?

请帮帮我。

谢谢。

【问题讨论】:

    标签: iphone ios5 sqlite xcode4.3


    【解决方案1】:

    我开发了一个需要的应用程序,我一直使用这种方式

    首先,我创建了一个类,命名为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];
    

    加密后,将加密后的数据插入数据库,从数据库中取出数据时,解密。

    希望对你有帮助。

    【讨论】:

    • 感谢您的即时回复。我只是怀疑我这样做就像我将 nsdata 转换为 nsstring NSData test = [ConstantMethods encryptString:InsertName withKey:keyEncryption]; NSString newStr = [NSString stringWithUTF8String:[test bytes]]; sqlite3_bind_text(addStmt, 1, [newStr UTF8String], -1, SQLITE_TRANSIENT);
    • 对了,你为什么用SQLite,用Core Data,它更友好和可读,如果你喜欢并且看到我的回答对你有用,别忘了投票和标记它作为正确答案,谢谢。
    • 嘿,疤痕我能够加密数据,但是当我解密数据时它返回我的值为零..
    • 解密时的加密字符串以这种方式返回一个类似于“”的空字符串。
    • @Andy 让我检查一下,我会返回适合 iOS 6 的答案
    猜你喜欢
    • 2012-10-07
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多