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];
}
}
}
First, look at the receiver of the
AES256EncryptWithKey:message. This is another, nested message:What’s
plaintext? It’s declared there in yourencryptString: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 sendingdataUsingEncoding: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:
AES256EncryptWithKey:.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:
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.