Cocoa encryption plain text?

扶醉桌前 提交于 2019-12-08 06:05:32

问题


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

        }

    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/6487710/cocoa-encryption-plain-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!