Error while encrypting string in staticLibrary - ios

In my application I am using encryption and decryption.
Before entering the string in to local database, I am encrypting that and after fetching the data from database I am decrypting it and using in my application.
It is working fine. I have used encryption/decryption from
link below
At the time of Encrypting:
NSString *myKey=#"any string more than 8 char";
NSData *data ;
NSData *encryptedData;
NSString *encryptPassword,*encryptPasscode;
// 1) Encrypt
data = [password dataUsingEncoding: NSASCIIStringEncoding];
encryptedData = [data AESEncryptWithPassphrase:myKey];
// 2) Encode Base 64
[Base64 initialize];
encryptPassword = [Base64 encode:encryptedData];
At the time of Decrypting :
NSData *decryptedData;
NSData *b64DecData;
field1 = (char *) sqlite3_column_text(selectPasscodeStatement, 0);
NSString *fieldStr1 = [[NSString alloc] initWithUTF8String: field1];
// 3) Decode Base 64
b64DecData = [Base64 decode:fieldStr1];
// 4) Decrypt
decryptedData = [b64DecData AESDecryptWithPassphrase:myKey];
retrivedPasscode = [[NSString alloc] initWithData:decryptedData encoding:NSASCIIStringEncoding];
But I have made staticLibrary of that same project. I am using that staticLibrary in another project. When I run that project, at the time of encrypting it gave me error below
-[NSConcreteMutableData AESEncryptWithPassphrase:]: unrecognized selector sent to instance 0x6a3fe40

You need to make change in the Build Settings of the project which will link the static library with the main project.
Follow these steps:
1)Click on Build Settings tab.
2)Search for "Other Linker Flags".
3)Add '-all_load' flag to it.
4)Build and run the project.
It worked fine for me.

Did you import the category header file:
#import "NSData-AES.h"
I believe there is the AESDecryptWithPassphrase method defined. Without that the app does not know about the method.
Just to clarify - you have to import the category header file to every file where you want to use the capabilities added by that category.

Related

How to encrypt the NSString in ios locally

I am working on the static application that means no webservices. My application contains activation page so that we need to enter text inside that textfield to validate.
if([textfield.text isEqualToString:#"AKS_BI"]) {
//loading home screen
} else {
//show alert
}
For this one, I would like to encrypt the "AKS_BI" in order to hide the string while reverse engineering or Mat testing.
Can you anyone help me on this.
To encrypt:
//for best practise encrypting string length must be >=8
NSString *yourString=#"abcdefghij";
NSString *YourPasswordString = #"123456";//i've took static but you can set it dynamically
NSString *encryptPassword;
//Encrypt
NSData *data = [YourPasswordString dataUsingEncoding: NSASCIIStringEncoding];
NSData *encryptedData = [data AESEncryptWithPassphrase:yourString];
//Encode Base 64
[Base64 initialize];
encryptPassword = [Base64 encode:encryptedData];
For more details you can check here
Hope this helps.
You can use this library https://github.com/RNCryptor/RNCryptor.
However, you will still need to store the encryption key securely. For that I would recommend to split them up and perform some operation on them to combine.
Download AES encryption files from github. Download from here
After adding these downloaded files in your project now compare ,
if ([[AESCrypt encrypt:textfield.text password:[[NSBundle mainBundle] bundleIdentifier]] isEqualToString:ACTIVATION_STRING])
Here, ACTIVATION_STRING = hYjhuOO+GYTUBS05== .... This encrypted string needs to be created with the below syntax and make sure that remove the below syntax from code after generation of encrypted string,
NSString *encryptedData = [AESCrypt encrypt:#"AKS_BI" password: [[NSBundle mainBundle] bundleIdentifier]];
That's it. It simple.

make response Url Valid Ios

I am developing ios application where I am waiting for response which should be uploaded image Url. I am converting NSData to NSString this way.
NSString* resultInString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
when I log resultInString, I get __NSCFString * #"\"http:\/\/em.avatars.s3.amazonaws.com\/avatarsd765c404-887c-4c0e-a08b-f7066ec9befe.png\"" 0x17777080
I have no idea how to validate this url to set UIImageview in my application. please give me a hint.
before say something about solution i think your getting url from NSData is incorrect.
The response seems to be JSON-encoded. So simply decode the response string using a JSON library (SBJson, JsonKit etc.)
or you can user correct encode for your NSData.
after all you can create a NSUrl from your string & if it's exist so it's valid.
i write a sample code for you
you should remove some charectar from your url by this way or like this
[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and for validation
NSUrl * url = [[NSURL alloc] initWithString:urlString];
if(url){ //valid url
}

Decrypting RSA with MIHCrypto (OpenSSL-Universal on iOS)

Hi I've got the a Problem with decrypting using MIHCrypto v0.3.2. These are my lines of code:
NSString *encrypted_text = #"BdhFH0sd7e9DExiCd50Ykh4spm2BX126skjJ1o8HHjKsN+J7r9IoI9kbB9AAacEpJsAfyesiJsq5gDBhQtcNbB6l88aSgPrEoVwR9ilzuzVcv1q3J1dxs4uIEMuhzoWT+R8//dD2jDdXPyFsdGWJc10CEizPFKpmy2jWhvU8CVs=";
NSBundle *myBundle = [NSBundle mainBundle];
NSString *privateKeyPath= [myBundle pathForResource:#"rsa_1024_priv" ofType:#"pem"];
NSData *privateKeyData = [[NSFileManager defaultManager] contentsAtPath:privateKeyPath];
MIHRSAPrivateKey *privateKey = [[MIHRSAPrivateKey alloc] initWithData:privateKeyData];
NSError *decryptionError = nil;
// decryption
NSData *encData = [encrypted_text dataUsingEncoding:NSUTF8StringEncoding];
NSData *decryptedEncData = [privateKey decrypt:encData error:&decryptionError];
NSString* decryptedText = [[NSString alloc] initWithData:decryptedEncData encoding:NSUTF8StringEncoding]; // iOS 7+, by iOS Core API
if(decryptionError){
DDLogDebug(#"error: %#",[encryptionError localizedDescription]);
}
DDLogDebug(#"decrypted: %#",decryptedEncData);
The problem is debugged here:
error: OpenSLL internal error! (Code=67522668,Description=error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len)
Do you have any Idea?
I finally found a solution:
Using shorter Blocks of Data!
Background (posted by Hohl - here):
Using RSA with large blocks of data seems to be a common issue. Some
wrappers handle this by splitting the data into smaller blocks and
encrypting every block separately. But since RSA isn't intended to
encrypt large blocks of data this won't be implemented in this wrapper.
(Better combine RSA with something like AES if you need features of
both worlds.)

Archiving on iOS using NSKeyedArchiver

I'd like my app to decompress and handle the contents of zip files.
I'm trying to do this the Cocoa way, without using external libraries, so I'm turning to NSKeyedUnarchiver and here's my code:
-(void) unarchive{
NSString *outputDirectory = [[NSHomeDirectory() stringByAppendingString:#"/Documents/"] stringByAppendingString:#"TheNewFolderName/"];
NSLog(#"Output Directory: %#", outputDirectory);
//MyArchive.zip is the filename of the zip file I placed in Xcode
NSData *theArchive = [NSKeyedUnarchiver unarchiveObjectWithFile:#"MyArchive.zip"];
NSError *error;
[theArchive writeToFile:outputDirectory options:NSDataWritingAtomic error:&error];
NSLog(#"theArchive is %#", theArchive);
And this prints "theArchive is null"! What am I doing wrong..?
NSKeyedArchiver and NSKeyedUnarchiver are used for archiving and unarchiving objects (NSObject subclasses, with certain restrictions) in your application. I'm afraid you cannot use it with zip files.

Decrypting 250MB PDF file

I have a 250Mb encrypted PDF file in my X-Code project, which I need to decrypt and display during run time. Since the file size is large, I can't decrypt it. So I split the original file into chunks of NSData, encrypted them into different part files. In the code, I am decrypting these multiple chunks and writing them into the same file and then displaying it.
NSData *decryptedData = [[NSData alloc] init];
NSString *thepath = [NSString stringWithFormat:#"%#/decryptedfile.%#",[paths objectAtIndex:0], fileType];
[decryptedData writeToFile:thepath atomically:YES];
[decryptedData release];
NSFileHandle *myFile = [NSFileHandle fileHandleForUpdatingAtPath:thepath];
[myFile truncateFileAtOffset:0];
for (int k=1; k<50; k++) { //i had 49 parts, hence the condition
NSString *partFilePath = [NSString stringWithFormat:#"%#/%#-part%d.pdf",[paths objectAtIndex:0], [fileName objectAtIndex:i], k];
NSData *tempDData = [[NSData alloc] initWithContentsOfFile:partFilePath];
[myFile writeData:[tempDData AES256DecryptWithKey:#"secretkey"]];
[tempDData release];
}
[myFile closeFile];
I had 49 parts, and am decrypting them each, writing them into a single file. This program runs fine in the simulator and am able to recover my original PDF. But on the device, my app gets terminated.
Its getting terminated when the for loop iterates for the 31st time and when am trying write my decrypted data into the Documents folder of the app. In other words, when i try to add data more than 150MB, my app is terminated. Is there any other way i can implement this feature?
Try calling
[myFile synchronizeFile];
at every iteration; this flushes the buffer. If this doesn't work, I guess you need to go below Foundation and need to directly use unbuffered io at the BSD level.

Resources