[Objective-C] Cyclical String Encryption App

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
I've been trying out IOS app development lately, meaning, getting introduced to Objective-C over the past few days. I wrote a basic hello world as a test for UI interaction and took it a step further with incrementing a counter based on certain touch gestures. Here's an app that I've started which encrypts a string with a key...

bYSj9xy.png


TPViewController.h
Code:
#import <UIKit/UIKit.h>

@interface TPViewController : UIViewController

@property(nonatomic, retain) IBOutlet UIButton *btnEncrypt;
@property(nonatomic, retain) IBOutlet UIButton *btnDecrypt;
@property(nonatomic, retain) IBOutlet UITextView *textInput;
@property(nonatomic, retain) IBOutlet UITextField *textKey;
@property(nonatomic, retain) IBOutlet UITextView *textOutput;

- (IBAction)showEncryptedOutput:(id)sender;
- (IBAction)showDecryptedOutput:(id)sender;

@end

TPViewController.m
Code:
#import "TPViewController.h"

@interface TPViewController ()

NSMutableString * EncryptString(NSString *plainText, NSString *encryptKey);
NSMutableString * DecryptString(NSString *encryptedText, NSString *decryptKey);

@end

@implementation TPViewController

@synthesize btnEncrypt;
@synthesize btnDecrypt;
@synthesize textInput;
@synthesize textKey;
@synthesize textOutput;

NSMutableString * EncryptString(NSString *plainText, NSString *encryptKey)
{
	int cycleIndex = 0;
	NSMutableString *result = [[NSMutableString alloc] initWithCapacity:plainText.length];
	for (int i = 0; i < plainText.length; ++i) {
		int rotate = [encryptKey characterAtIndex:cycleIndex];
		[result appendFormat:@"%c", [plainText characterAtIndex:i] + rotate];
		cycleIndex = (cycleIndex + 1) % encryptKey.length;
	}
	return result;
}

NSMutableString * DecryptString(NSString *encryptedText, NSString *decryptKey)
{
	int cycleIndex = 0;
	NSMutableString *result = [[NSMutableString alloc] initWithCapacity:encryptedText.length];
	for (int i = 0; i < encryptedText.length; ++i) {
		int rotate = [decryptKey characterAtIndex:cycleIndex];
		[result appendFormat:@"%c", [encryptedText characterAtIndex:i] - rotate];
		cycleIndex = (cycleIndex + 1) % decryptKey.length;
	}
	return result;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (IBAction)showEncryptedOutput:(id)sender
{
	[textKey resignFirstResponder];
	NSMutableString *encryptedMutable = EncryptString(textInput.text, textKey.text);
	textOutput.text = [NSString stringWithString:encryptedMutable];
}

- (IBAction)showDecryptedOutput:(id)sender
{
	[textKey resignFirstResponder];
	NSMutableString *decryptedMutable = DecryptString(textInput.text, textKey.text);
	textOutput.text = [NSString stringWithString:decryptedMutable];
}

@end

I still have to deal with the keyboard and other things with setting proper first responders, application states, etc, but the functionality is in place, and a basic UI.

Going from C++ to Objective-C is a strange transition however regardless of how skilled you are in any other C based language I must admit...

:beerchug2:
 
That looks very nice Ace!

I've always been a bit leery of learning objective-C. It just seems so...weird TBH. Although I've never actually done much app development, just a little Windows Phone stuff. I've always been focused on the Desktop primarily, but it is very interesting to see how it's done.

How did you make that app? Do you have a mac, or did you make it on Windows (or Linux, etc.)? Did you need to pay their developer charge, or is that only for publishing on the store?

Thank you.
 
I have a Mac Mini that I got from one of my boss' at work. You don't need to pay for anything to develop apps because Xcode is free, however there is a $99 SDK that comes with the license I believe, needed in order to release to the app store. I think it's just too weird too, I've got a big book on Windows Phone 8 App development that I need to get into... I'm always curious about the bigger picture though, and seeing what everything is like. :)

I picked this up pretty quickly, but IMO, Objective-C is still just odd. Things are based on messages, so the function calls are just strange. Whoever came up with Objective-C had an odd view on structuring the language. I've spent the last week just reading about it and developing small apps like this, and even with a better grasp, as you say and can probably see, even the syntax is strange.

Coming from a C++ background, you'll have trouble with Objective-C. There's small similarities...
 
Last edited:
It looks very strange, according to Wikipedia, the message syntax is based on Smalltalk.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top