AES test on iphone - iphone

my encryption is working but i cant decrypt kindly suggest what i am doing wrong here
NSString *passphrase = #"hello";
NSStringEncoding myEncoding = NSASCIIStringEncoding;
NSString *alphaStringPlain = #"cell";
NSData *alphaDataPlain = [alphaStringPlain dataUsingEncoding:myEncoding];
NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];
NSLog(alphaStringCypher); // perfeclty encypted i guess
/////// FOR DECRYPTION///////////////
NSData *zCypher = [alphaDataPlain AESDecryptWithPassphrase:alphaStringCypher];
NSString *Cypher = [[NSString alloc] initWithData:zCypher encoding:myEncoding];
NSLog(#" decode %#",[Cypher dataUsingEncoding:NSUTF8StringEncoding]);
NSLog(#" decode %#",Cypher);// not working some garbage value

After struggling i got the ans
NSString *passphrase = #"1234567812345678";
NSStringEncoding myEncoding = NSASCIIStringEncoding;
NSString *alphaStringPlain = #"hello";
NSData *alphaDataPlain = [alphaStringPlain dataUsingEncoding:myEncoding];
NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];
NSLog(alphaStringCypher);
///////
NSData *zCypher = [alphaDataCypher AESDecryptWithPassphrase:passphrase];
NSString *Cypher = [[NSString alloc] initWithData:zCypher encoding:myEncoding];
//NSData *zCypher = [alphaStringCypher AESDecryptWithPassphrase:passphrase];
NSLog(#" hua kya decode %#",cypher);// working

I think you are decrypting wrong value
try with this :
NSData *zCypher = [alphaStringCypher AESDecryptWithPassphrase:passphrase];

Related

how to use 'string' variable as 'objectForkey' in iphone

I want to use string as objectForkey. my code line is this.
And I want to use "strend" as a objectForkey but it didn't work.
Code is:
NSString *strend = [[NSString alloc] initWithFormat:#"%#",[[detwec.arrdetails objectAtIndex:0]valueForKey:#"EndDate"]];
NSString *dateString = [[NSString alloc]initWithFormat:#"%#",[_currentItemValue objectForKey:#"strend" ]];
try to replace this line
NSString *dateString = [[NSString alloc]initWithFormat:#"%#",[_currentItemValue objectForKey:#"strend" ]];
by this
NSString *dateString = [[NSString alloc]initWithFormat:#"%#",[_currentItemValue objectForKey:strend]];
if you want to read a value using string as key you can use:
NSString *value = ["Your Dictionary" objectForKey:strend];
get a check that your NSString *strend shouldn't be "nil" else will not give you any value.
You Can Try
NSString *strend = [NSString stringWithFormat:#"%#",[detwec.arrdetails objectAtIndex:0]valueForKey:#"EndDate"];
NSString *dateString = [NSString stringWithFormat:#"%#",[_currentItemValue objectForKey:#"strend" ]];
or else
NSString *dateString = [_currentItemValue objectForKey:#"strend" ];
NSString *strend = [NSString stringWithFormat:#"%#",[detwec.arrdetails objectAtIndex:0]]; NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:strend forKey:#"strend"];
NSString *dateString = [storeData objectForKey:#"strend"];

Reading a Text file in xcode

First off, I'm a complete beginner.
This might be a stupid question, but here it goes:
I'm currently working on an App than contains Latin texts that the users can view and read.
I'm using Xcode 4 with the storybord function.
Theway the app is built: user selects author - then the book - then app shows the text.
I am kind of confused because i need to have various text files, depending on the users choice.
As i have done or do usually just embedded the files as resource and by using the below code I read-out the files
in .h file
-(NSString *)readFile:(NSString *)fileName;
in .m file
-(NSString *)readFile:(NSString *)fileName
{
NSLog(#"readFile");
NSString *appFile = fileName;
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile])
{
NSError *error= NULL;
NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error];
if (error == NULL)
return resultData;
}
return NULL;
}
May this will help you
Enjoy Coding :)
EDIT
What I have done is according to the date change on one button click and also at the time of viewWillApper I call the function below
-(void)dateAndContentReload
{
NSLog(#"dateAndContentReload");
NSLog(#"Appdelegate Date: %#",appDelegate.dateSelected);
self.currentDate = appDelegate.dateSelected;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSString *dateString = [dateFormatter stringFromDate: appDelegate.dateSelected];
self.navigationItem.title = dateString;
[dateFormatter setDateFormat: #"Myyyy"];
NSString *dateComp = [dateFormatter stringFromDate: appDelegate.dateSelected];
//NSLog(#"date : %#",dateString);
NSString *data;
NSString *path;
if(![dateComp isEqualToString: #"12012"])
{
NSString *size = [NSString stringWithFormat: #"%f",appDelegate.size] ;
NSLog(#"dateAndContentReload size: %f",appDelegate.size);
data = #"<html><body><font size='";
data = [data stringByAppendingString: size];
data = [data stringByAppendingString: #"'><P>"];
data = #"Data not Available for Current Date.";
data = [data stringByReplacingOccurrencesOfString: #"\n" withString:#"<br>"];
data = [data stringByAppendingString:#"</P></font></body></html>"];
}
else
{
[dateFormatter setDateFormat:#"MMMMd"];
dateString = [dateFormatter stringFromDate: appDelegate.dateSelected];
[dateFormatter release];
NSLog(#"new Format: %#",dateString);
path = [[NSBundle mainBundle] pathForResource: dateString ofType: #"txt"];
data = nil;
NSString *size = [NSString stringWithFormat: #"%f",appDelegate.size] ;
NSLog(#"dateAndContentReload size: %f",appDelegate.size);
data = #"<html><body><font size='";
data = [data stringByAppendingString: size];
data = [data stringByAppendingString: #"'><P>"];
data = [data stringByAppendingString: [self readFile: path]];
data = [data stringByReplacingOccurrencesOfString: #"\n" withString:#"<br>"];
data = [data stringByAppendingString:#"</P></font></body></html>"];
}
[self.currentView loadHTMLString: data baseURL: nil];
}
Basically I read file and display in UIWebView as client request it.

How to add GET parameters to an ASIHttpRequest?

How can I add GET parameters to an ASIHttpRequest?
I want to go from http://mysite.com/server to http://mysite.com/server?a=1&b=2 programmatically.
I have all my parameters as key-value pairs in an NSDictionary object.
Thanks
Use string format like
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:#"1",#"a",#"2",#"b", nil];
NSMutableString *prams = [[NSMutableString alloc] init];
for (id keys in dict) {
[prams appendFormat:#"%#=%#&",keys,[dict objectForKey:keys]];
}
NSString *removeLastChar = [prams substringWithRange:NSMakeRange(0, [prams length]-1)];
NSString *urlString = [NSString stringWithFormat:#"http://mysite.com/server?%#",removeLastChar];
NSLog(#"urlString %#",urlString);

how to parse a string in iphone, objective c?

I have a string in this format :- { panel : {start : [{"element_id" : 0, "element_name" :0, "element_image" : 0, "element_desc" : 0, "element_dob" : 0, "awards" :0},]} how can I parse this string. please help me to overcome this problem. I tried SBJSon - code: -
NSLog(#"response string before = %#", responseStr);
NSData *fileData = [NSData dataWithContentsOfFile:responseStr];
NSString *responseString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
NSLog(#"response string after = %#", responseString);
NSError *jsonError = nil;
NSDictionary *feed = nil;
SBJsonParser *json = [[SBJsonParser new] autorelease];
feed = [json objectWithString:responseString error:&jsonError];
NSLog(#"feed = %#", feed);
if ([jsonError code]==0) {
// get the array of "results" from the feed and cast to NSArray
NSMutableArray *localObjects = [[[NSMutableArray alloc] init] autorelease];
NSArray *results= (NSArray *)[feed valueForKey:#"start"];
// loop over all the results objects and print their names
int ndx;
for (ndx = 0; ndx < results.count; ndx++)
{
[localObjects addObject:(NSDictionary *)[results objectAtIndex:ndx]];
}
NSLog(#"local objects = %#", localObjects);
}
the NSDictionary feed is getting nil value in NSLog..
Try the following one...
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *dictResponse = (NSDictionary*) [json objectWithString:sitesResponse error:nil];
You can use Regular Expressions (wiki). Take sources from: RegexKit, add to linker flags "-fopenmp" and use in NSString message "stringByMatching".
For example, if you want take "element_name":
NSString* str = [your_string stringByMatching:#"element_name\".*:(.*?)," capture: 1L];

In iphone-exif how to see updated image metadata info?

I am new to iphone programming. using google code iphone-exif, i can read/write images tags also i can add custom image tags. But, my problem is that how can see the updated data??? OR is there any way to save image with updated data??
I have used .jpg image from net, is in other resources folder.
Here my code (.m file)
NSString *filePath = #"/.../ProjectName/1.jpg";
NSMutableData *imageData = [NSMutableData dataWithContentsOfFile:filePath];
EXFJpeg* jpegScanner = [[EXFJpeg alloc] init];
[jpegScanner scanImageData: imageData];
EXFMetaData* exifData = jpegScanner.exifMetaData;
//EXFJFIF* jfif = jpegScanner.jfif;
[exifData addTagValue: #"Changed MAke" forKey:[NSNumber numberWithInt:EXIF_Make]];
id val2 = [exifData tagValue:[NSNumber numberWithInt:EXIF_Make]];
NSLog(val2);
NSLog([exifData tagValue:[NSNumber numberWithInt:EXIF_Model]]);
NSLog([exifData tagValue:[NSNumber numberWithInt:EXIF_DateTime]]);
// SAVE THE IMAGE WITH THE NEW TAGS
[jpegScanner populateImageData:imageData];
//[imageData writeToFile:filePath atomically:YES];
After saving your new image data:
NSString *filePath = #"/.../ProjectName/1.jpg";
NSMutableData *imageData = [NSMutableData dataWithContentsOfFile:filePath];
EXFJpeg* jpegScanner = [[EXFJpeg alloc] init];
[jpegScanner scanImageData: imageData];
EXFMetaData* exifData = jpegScanner.exifMetaData;
id myValue = [exifData tagValue:[NSNumber numberWithInt:EXIF_Make]];
NSLog(#"My changedValue is: %#", myValue);
[jpegScanner release];