How to encrypted the text

Discussion in 'iOS Programming' started by Binju, Jun 1, 2010.

  1. Binju macrumors member

    Jan 31, 2010
    I want to encrypted the password text and stored in to the database and also I want to compare the encrypted text with the text in the textbox.

    So I want to know , How to convert the normal text in to the encrypted text and encrypted text to the normal text
  2. Binju thread starter macrumors member

    Jan 31, 2010
    		NSString *password=@"mypassword" ;
    		NSString *str = txtPassword.text;
    	 // 1) Encrypt
    	 NSLog(@"encrypting string = %@",str);
    	 NSData *data = [str dataUsingEncoding: NSASCIIStringEncoding];
    	 NSData *encryptedData = [data AESEncryptWithPassphrase:password];
    	 // 2) Encode Base 64
    	 // If you need to send over internet, encode NSData -> Base64 encoded string
    	 [Base64 initialize];
    	 NSString *b64EncStr = [Base64 encode:encryptedData];
    	 NSLog(@"Base 64 encoded = %@",b64EncStr);
    	 //////////////////// Send this data over network /////////////////////////////////////	
    	 // 3) Decode Base 64
    	 // Then you can put that back like this
    	 NSData	*b64DecData = [Base64 decode:password1];
    	 // 4) Decrypt
    	 // This should be same before encode -> decode base 64
    	 //NSData *decryptedData = [encryptedData AESDecryptWithPassphrase:password];
    	 NSData *decryptedData = [b64DecData AESDecryptWithPassphrase:password];
    	 NSString* decryptedStr = [[NSString alloc] initWithData:decryptedData encoding:NSASCIIStringEncoding];
    	 NSLog(@"decrypted string = %@",decryptedStr);
    The above code converts to base64.Is it possible to convert to hexadecimal?

Share This Page