I have chosen an algorithm to encrypt a file. i may encrypt text or image files. How can I write a generic type of encryption method that works with both te开发者_开发技巧xt and image files? I'm working with Objective-C.
What ever you are encrypting you might be using the data or specifically NSData, then converting it to char array and then applying the algorithm. So make a method that takes NSData as an argument, and return encrypted NSData.
e.g.
-(NSData*)encrypt:(NSData*)data{
///your algorithm
return enCryptedData;
}
Convert your Image
or File
into NSData
and pass it to the method.
Write an encryption method that operates on a stream/sequence/etc. of bytes (char). Then read in the text file or the image file as bytes (i.e. don't try to apply any sort of encoding to the contexts of the text file to turn it into Unicode) and do the encryption.
精彩评论