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

发布于 2024-12-14 11:30:02 字数 206 浏览 1 评论 0原文

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

Ciphertext:密文长度; Salt:盐的长度;

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

我该怎么做?然后取回盐进行解密?

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

I need to implement salts in my encryption, but to do so, I need to store it in a file format that I need to create so I can later retrieve it to decrypt. I'm a noob when it comes to encryption. The specifications of the file format should be as so:

Ciphertext: length of ciphertext ;
Salt: length of salt ;

Then the ciphertext and salt written out. This is where xcode really confuses me, as in creating a new file, etc.

How can I do this? And then retrieve the salt for decryption?

Thank you, your help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-12-21 11:30:02

您可以考虑使用 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

You might consider using NSMutableDictionary and NSKeyedUnarchiver like this:

// 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);

Output:

Extracted ciphertext: the ciphertext
Extracted salt: the salt

EDIT

Response to comment: Both NSData and NSString feature length.

Quick example:

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

Output:

String length: 14
Data length: 14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文