Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

AES:encryption in objective c and php

mramin05mramin05 Posts: 49Registered Users
edited April 2012 in iPhone SDK Development
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.
Post edited by mramin05 on

Replies

  • mramin05mramin05 Posts: 49Registered Users
    edited October 2010
    mramin05 wrote: »
    Hi guys,
    in my project I have to encrypt some data and have to get in php to save in server.

    Thanks a lot.
    #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][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'[/HTML]

    Please help me..
    Thanks in advance.
  • makamimakami Posts: 4New Users
    edited October 2010
  • mramin05mramin05 Posts: 49Registered Users
    edited November 2010
    makami wrote: »
    UP!

    Any news about it ?

    No success yet.
    Please help.... anybody.. with any tutorial for AES will be grest help.
  • CiechanCiechan Posts: 22Registered Users
    edited November 2010
    I guess you are missing category on NSData, there are no AES256EncryptWithKey and AES256DecryptWithKey methods by default.

    Here's are the files:

    NSDataEncryption.h
    #import <Foundation/Foundation.h>
    
    
    @interface NSData (AES256)
    
    - (NSData *)AES256EncryptWithKey:(NSString *)key;
    - (NSData *)AES256DecryptWithKey:(NSString *)key;
    
    @end
    

    NSDataEncryption.m
    #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.
  • mramin05mramin05 Posts: 49Registered Users
    edited November 2010
    Ciechan wrote: »
    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.
  • BerndderHeldBerndderHeld Posts: 1New Users
    edited January 2011
    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?
  • MetsdevMetsdev Posts: 4New Users
    edited January 2011
    can u give php code for encryption for this method ?
  • MetsdevMetsdev Posts: 4New Users
    edited January 2011
    mramin05 wrote: »
    Thx a lot.

    what is the php code to encrypt data ?
  • SolomiyaN2SolomiyaN2 Posts: 1New Users
    edited April 2012
    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
  • bhaiyubhaiyu Posts: 2New Users

    BerndderHeld;281684 said:
    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
  • bhaiyubhaiyu Posts: 2New Users
    Here, try this

    resultName = [resultName stringByReplacingOccurrencesOfString:@" " withString:@""];
    resultName = [resultName stringByReplacingOccurrencesOfString:@"<" withString:@""];
    resultName = [resultName stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSMutableData *cipher= [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [resultName length]/2; i++) {
    byte_chars[0] = [resultName characterAtIndex:i*2];
    byte_chars[1] = [resultName characterAtIndex:i*2+1];
    whole_byte = strtol(byte_chars, NULL, 16);
    [cipher appendBytes:&whole_byte length:1];
    }

  • hiteshborse12hiteshborse12 mumbaiPosts: 2New Users Noob
    bhaiyu nice code but after ciphe how we can decrypt into orignl string plese send me code.
    email: hiteshborse12@gmail.com
  • amitchauhanamitchauhan Posts: 23Registered Users @
Sign In or Register to comment.