如何在iOS中编码/解码文件

时间:2016-06-24 10:26:17

标签: html ios encryption

我尝试了很多方法,但我没有采用正确的方法来加密html文件。

NSString *plainString = @"This string will be encrypted";
//should be provided by a user
NSLog( @"Original String: %@", path );  
NSString *content = [path stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *encryptedString = [content dataUsingEncoding:NSUTF8StringEncoding];
NSLog( @"Encrypted String: %@", encryptedString );

1 个答案:

答案 0 :(得分:1)

添加具有NSDATA子类的新类。

将此代码粘贴到班级的.h文件中:

#import <Foundation/Foundation.h>

@interface NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end

这是你班级的.m档案:

#import "NSData+Encryption.h"
#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (Encryption)
- (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

现在加密您的html页面如下:

NSString * key = @“YourEncryptionKey”; //应该由用户提供

        NSLog( @"Original String: %@", htmlCode );
        NSData *encryptedString = [[htmlCode dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
        NSLog(@"%@",encryptedString);

        [encryptedString writeToFile:htmlFilePath atomically:YES];
        NSString *myData = [NSString stringWithContentsOfFile:htmlFilePath];

        NSLog(@"Data : %@ ",myData);

进一步将文件解密为:

NSString * key = @“YourEncryptionKey”;

NSString *decryptedString= [[NSString alloc] initWithData:[data1 AES256DecryptWithKey:key] encoding:NSUTF8StringEncoding];
NSData *utf8Data = [decryptedString dataUsingEncoding:NSUTF8StringEncoding];
[utf8Data writeToFile:_pagesPath atomically:YES];

NSLog(@"%@",decryptedString);
NSError* error;
NSString *htmlContent = [NSString stringWithContentsOfFile:_pagesPath encoding:NSUTF8StringEncoding error:&error];

NSData *data = [htmlContent dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",data);

[_webview loadData:data MIMEType:@"application/xhtml+xml" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@""]];