Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-03-2010, 09:05 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default AES:encryption in objective c and php

Hi guys,
in my project I have to encrypt some data and have to get in php to save in server.

Please suggest me any tutorial.

I have searched a lot but could not find sutable tutorial.

Thanks a lot.
mramin05 is offline   Reply With Quote
Old 10-03-2010, 10:21 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default My code which create error:AES256EncryptWithKey unrecognized selector

Quote:
Originally Posted by mramin05 View Post
Hi guys,
in my project I have to encrypt some data and have to get in php to save in server.

Thanks a lot.
Code:
#import <CommonCrypto/CommonCryptor.h>

 - (void)viewDidLoad {

	 NSString *text = @"my password";
	 NSString *secretKey = @"1234567812345678";
	
	 [self encryptString:text withKey:secretKey ];
	 
	 
	 [super viewDidLoad];
 }
- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
	return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];

}

- (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key {
	return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key]
								  encoding:NSUTF8StringEncoding] autorelease];
}
But it show me the error
HTML Code:
[NSConcreteMutableData AES256EncryptWithKey:]: unrecognized selector sent to instance 0x5f4f2d0
2010-10-04 12:17:59.507 test[1030:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData AES256EncryptWithKey:]: unrecognized selector sent to instance 0x5f4f2d0'
Please help me..
Thanks in advance.
mramin05 is offline   Reply With Quote
Old 10-14-2010, 08:31 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 4
makami is on a distinguished road
Default

UP!

Any news about it ?
makami is offline   Reply With Quote
Old 11-07-2010, 12:40 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by makami View Post
UP!

Any news about it ?
No success yet.
Please help.... anybody.. with any tutorial for AES will be grest help.
mramin05 is offline   Reply With Quote
Old 11-07-2010, 04:59 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 22
Ciechan is on a distinguished road
Default

I guess you are missing category on NSData, there are no AES256EncryptWithKey and AES256DecryptWithKey methods by default.

Here's are the files:

NSDataEncryption.h
Code:
#import <Foundation/Foundation.h>


@interface NSData (AES256)

- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;

@end
NSDataEncryption.m
Code:
#import "NSDataEncryption.h"

#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (AES256)

- (NSData *)AES256EncryptWithKey:(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;
}

- (NSData *)AES256DecryptWithKey:(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 numBytesDecrypted = 0;
	CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
										  keyPtr, kCCKeySizeAES256,
										  NULL /* initialization vector (optional) */,
										  [self bytes], dataLength, /* input */
										  buffer, bufferSize, /* output */
										  &numBytesDecrypted);
	
	if (cryptStatus == kCCSuccess) {
		//the returned NSData takes ownership of the buffer and will free it on deallocation
		return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
	}
	
	free(buffer); //free the buffer;
	return nil;
}

@end
Then just import header file.
__________________



Portaball
Portaball Lite
Ciechan is offline   Reply With Quote
Old 11-08-2010, 12:04 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by Ciechan View Post
I guess you are missing category on NSData, there are no AES256EncryptWithKey and AES256DecryptWithKey methods by default.

@end[/code]

Then just import header file.
Thx a lot.
mramin05 is offline   Reply With Quote
Old 01-03-2011, 04:19 AM   #7 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 1
BerndderHeld is on a distinguished road
Default

I Use this method to encrypt a password for a XML-API. My Problem is that i cant encode the NSDATA with the Cipher to a proper plain String.

How do you use this?
BerndderHeld is offline   Reply With Quote
Old 01-29-2011, 01:29 AM   #8 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
Metsdev is on a distinguished road
Default

can u give php code for encryption for this method ?
Metsdev is offline   Reply With Quote
Old 01-29-2011, 01:31 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
Metsdev is on a distinguished road
Default

Quote:
Originally Posted by mramin05 View Post
Thx a lot.
what is the php code to encrypt data ?
Metsdev is offline   Reply With Quote
Old 04-05-2012, 07:03 AM   #10 (permalink)
Registered Member
 
Join Date: Apr 2012
Posts: 1
SolomiyaN2 is on a distinguished road
Default

Quote:
Originally Posted by BerndderHeld View Post
I Use this method to encrypt a password for a XML-API. My Problem is that i cant encode the NSDATA with the Cipher to a proper plain String.

How do you use this?
I've got the same issue. In spite of the fact that decrypted string equals initial one I would like to be able to cast nsdata to proper nsstring.
Have you managed to solve this problem? Or did you find any other solution for that?

Thank you in adnvance
SolomiyaN2 is offline   Reply With Quote
Reply

Bookmarks

Tags
aes encryption, data encryption, php script

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 351
7 members and 344 guests
headkaze, mistergreen2011, nobstudio, Objective Zero, rayjeong, revg, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0