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

标签 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 );

最佳答案

添加带有 NSDATA 子类的新类。

将此代码粘贴到您的类的 .h 文件中:

#import <Foundation/Foundation.h>

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

这在你类(class)的 .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:@""]];

关于html - 如何在 iOS 中编码/解码文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011166/

相关文章:

css - 使用 css 将 div 复制到弹出窗口

ios - 尝试在 Moya 中发送 [JSON] 作为参数值时出现错误

java - AES 解密给出不一致的结果

c - 仿射代码错误

java - 使用 thymeleaf 和 spring boot 的动态下拉菜单

javascript - 如何修复 Javascript 中的 'invalid ciphertext size' 错误? (aes)

jquery - 如何聚焦div?

ios - 通过 BeeTee 应用程序连接到其他蓝牙设备

ios - 我应该如何访问存储在包含容器 View 的 View Controller 中的 IBOutlet? ( swift 4)

java - Keystore -- 从命令行自定义 SecretKey 进入 Keystore