gpt4 book ai didi

cocoa - 实现与加密一起使用的文件格式 - Cocoa

转载 作者:行者123 更新时间:2023-12-03 18:00:04 26 4
gpt4 key购买 nike

我需要在加密中实现盐,但要做到这一点,我需要将其存储为我需要创建的文件格式,以便稍后检索它进行解密。在加密方面我是个菜鸟。文件格式规范如下:

密文:密文长度;salt:盐的长度;

然后将密文和盐写出来。这就是 xcode 真正让我困惑的地方,比如创建新文件等。

我该怎么做?然后检索盐进行解密?

谢谢您,非常感谢您的帮助。

最佳答案

您可以考虑使用 NSMutableDictionaryNSKeyedUnarchiver,如下所示:

// Example ciphertext and salt
NSString *ciphertext = @"the ciphertext";
NSString *salt = @"the salt";

// File destination
NSString *path = @"/Users/Anne/Desktop/Archive.dat";

// Create dictionary with ciphertext and salt
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:ciphertext forKey:@"ciphertext"];
[dictionary setObject:salt forKey:@"salt"];

// Archive dictionary and write to file
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
[data writeToFile:path options:NSDataWritingAtomic error:nil];

// Read file and unarchive
NSMutableDictionary *theDictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

// Get ciphertext and salt
NSString *theCiphertext = [theDictionary objectForKey:@"ciphertext"];
NSString *theSalt = [theDictionary objectForKey:@"salt"];

// Show Result
NSLog(@"Extracted ciphertext: %@",theCiphertext);
NSLog(@"Extracted salt: %@",theSalt);

输出:

Extracted ciphertext: the ciphertext
Extracted salt: the salt

编辑

对评论的回应:NSDataNSString都具有长度

简单示例:

NSString *theString = @"Example String";
NSData *theData = [theString dataUsingEncoding:NSUTF8StringEncoding];

NSUInteger stringLength = [theString length];
NSUInteger dataLength = [theData length];

NSLog(@"String length: %ld",stringLength);
NSLog(@"Data length: %ld",dataLength);

输出:

String length: 14
Data length: 14

关于cocoa - 实现与加密一起使用的文件格式 - Cocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com