- (NSData *)aesEncrypt:(Nsstring *)key { // 'key' should be 32 bytes for AES256,will be null-padded otherwise char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) bzero(keyPtr,sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) enCoding:NSUTF8StringEnCoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers,the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; voID *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCcoptionPKCS7padding,keyPtr,kCCKeySizeAES256,NulL /* initialization vector (optional) */,[self bytes],dataLength,/* input */ buffer,bufferSize,/* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNocopy:buffer length:numBytesEncrypted]; } free(buffer); //free the buffer; return nil;}
但我不认为这个代码可以用在iPhone上.如果我尝试加密一个5mb的文件,它将占用至少10mb的内存,因为它将被加载到NSData并返回原样.有没有一种方法可以通过读取小块并将输出写入另一个文件来加密文件?或者我错了这样做
解决方法 试试RNCryptorhttps://github.com/rnapier/RNCryptor
我在我的应用程序中成功使用了这个.
总结以上是内存溢出为你收集整理的加密iphone-sdk上的文件全部内容,希望文章能够帮你解决加密iphone-sdk上的文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)