繁体   English   中英

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

[英]How to encode/decode file in iOS

我尝试了很多方法,但是我没有得到正确的方法来加密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 );

添加带有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:@""]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM