Cocoa加密明文? [英] Cocoa encryption plain text?

查看:151
本文介绍了Cocoa加密明文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个加密应用程序,现在只加密文本文件。我需要一些帮助的连接和如何去实际加密。我收到这个代码片段来加密文件,但我有点困惑。我需要做的是有一个按钮(加密),采取这个文本文件并加密它。我应该先提取文件的内容,然后加密吗?怎么会这样?程序必须知道什么文件被选择,所以它加密它,我现在是一个完整的noob,我需要一些帮助。



非常感谢一步一步的指示。



这是代码:



- (NSData *)encryptString:(NSString *)plaintext withKey:(NSString *)key {
return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}



我使用以下代码片段实现了文件选择器:

   - (IBAction)fileChooser:(id)sender {
int i;
NSOpenPanel * openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:YES];
[openDlg setPrompt:@Select];
if([openDlg runModalForDirectory:nil file:nil] == NSOKButton)
{
NSArray * files = [openDlg filenames];
for(i = 0; i <[files count]; i ++)
{
[files objectAtIndex:i];

}

}
}


解决方案


这是代码:

   - NSData *)encryptString:(NSString *)plaintext withKey:(NSString *)key {
return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}


首先, AES256EncryptWithKey:消息。这是另一个嵌套消息:

  [plaintext dataUsingEncoding:NSUTF8StringEncoding] 
pre>

什么是明文?它在 encryptString:withKey:方法中声明:它是一个保存NSString指针的变量。



所以你发送 dataUsingEncoding:消息到一个NSString实例。



这很好,如果你打算加密一些纯文本用户输入,但它不是那么好的加密文件。 NSStrings只是人的角色;



我知道你说你的应用程序现在加密了纯文本文件,但是当您抛出此限制时,解决方案实际上更简单。



因此,您需要加密任何数据, 。删除您的 encryptString:withKey:方法 - 它是无用的。



发送 AES256EncryptWithKey:到通过向NSString实例发送 dataUsingEncoding:获得的对象。



如果您查看 NSString的文档,您可以查看 dataUsingEncoding:返回。 Spoiler:它返回一个NSData对象。



从这里,它是一个超链接到 NSData文档,其中您会发现两件事:


  1. 它没有方法通过选择器 AES256EncryptWithKey:

  2. 它有方法来创建一个数据对象文件的内容。 (我会让你找到他们。)

我假设你知道#1, 。我也假设,实际上,这个类别在NSData;



在Objective-C中,您不应该使用 @interface 一个索引循环来遍历数组,除非你实际上需要索引的东西,你通常不和你,具体来说,不要。而应使用快速枚举

  for(NSString * path in [openPanel filenames]){
}

您可以再次看到,这使得解决方案更简单。它也更快。



在该循环中,将该路径传递给NSData类方法,该方法根据文件内容创建一个NSData对象。然后,向该数据对象发送 AES256EncryptWithKey:消息以获取密文,您应该将其写入单独的文件。我将引用你回到NSString文档来获取计算输出文件路径所需的路径处理方法,以及NSData文档,了解将密文数据写入输出文件所需的方法。 / p>

I'm working on an encryption application that for now encrypts text-only files. I need some help in the connections and how to go about the actual encryption. I received this snippet of code to encrypt a file, but I am a bit confused. What I need to do is have a button (encrypt) that takes this text file and encrypts it. Am I supposed to extract the contents of the file first, then encrypt it? How so? The program must know what file has been selected so it encrypts it, and I'm a complete noob right now, and I need some help.

Step by step instructions would be greatly appreciated.

This was the code:

- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
    return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}

I've implemented a file chooser with the following snippet:

- (IBAction)fileChooser:(id)sender {
    int i;
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseFiles:YES];
    [openDlg setCanChooseDirectories:YES];
    [openDlg setPrompt:@"Select"];
    if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
    {
        NSArray* files = [openDlg filenames];
        for( i = 0; i < [files count]; i++ )
        {
            [files objectAtIndex:i];

        }

    }
}

解决方案

This was the code:

- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
    return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}

First, look at the receiver of the AES256EncryptWithKey: message. This is another, nested message:

[plaintext dataUsingEncoding:NSUTF8StringEncoding]

What's plaintext? It's declared there in your encryptString:withKey: method: It is a variable holding a pointer to an NSString.

So you're sending the dataUsingEncoding: message to an NSString instance.

That's good if you intend to encrypt some plain-text user input, but it's not so good for encrypting files. NSStrings are for nothing but human characters; the user will probably want to encrypt files that are not plain text, such as images and video files.

I know you said that your application "for now encrypts text-only files", but the solution is actually simpler when you throw out this restriction.

So you need to encrypt any data, not just a string. Delete your encryptString:withKey: method—it is useless.

From the implementation of that late method, we know that you were sending AES256EncryptWithKey: to an object obtained by sending dataUsingEncoding: to an NSString instance.

If you look in the documentation for NSString, you can see what dataUsingEncoding: returns. Spoiler: It returns an NSData object.

From there, it's one hyperlink away to the NSData documentation, where you will find two things:

  1. It has no method by the selector AES256EncryptWithKey:.
  2. It does have methods to create a data object from the contents of a file. (I'll let you find them.)

I'm assuming that you knew #1, and downloaded a category implementation from somewhere. I'm also assuming that, in fact, this category is on NSData; the category's @interface, in its header, will tell you.

In Objective-C, you should not use an index loop to iterate through an array unless you actually need the index for something, which you generally don't and you, specifically, don't. Instead, use fast enumeration:

for (NSString *path in [openPanel filenames]) {
}

You can see how, again, this makes the solution simpler. It's also faster.

Inside that loop, pass that path to the NSData class method that creates an NSData object from the contents of a file. Then, send that data object the AES256EncryptWithKey: message to obtain the ciphertext, which you should probably write out to a separate file. I'll refer you back to the NSString documentation for the path-manipulation methods you'll need to compute the output file path, and the NSData documentation for the method you'll need to write the ciphertext data out to the output file.

这篇关于Cocoa加密明文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆