Hello There, Guest!
View New Posts  |  View Today's Posts
[Objective-C] Cyclical String Encryption App

  • 0 Vote(s) - 0 Average


01-18-2014, 12:13 AM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

[Objective-C] Cyclical String Encryption App
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...



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.

cheers
This post was last modified: 01-18-2014, 12:21 AM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-20-2014, 02:35 PM #2
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: [Objective-C] Cyclical String Encryption App
Man, seems like a lot to deal with for something simple. :|

01-23-2014, 08:11 PM #3
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: [Objective-C] Cyclical String Encryption App
(01-20-2014, 02:35 PM)KoBE Wrote:  Man, seems like a lot to deal with for something simple. :|

Haha, just like C++, except a bit more chaotic IMO. If you ask me, from my short dealings with Objective-C, it's pretty ugly, and the documentation isn't as great as it should be too.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  Case Insensitive String's C++ AceInfinity 0 923 03-30-2015, 02:11 AM
Last Post: AceInfinity
   C Style String Encoding & Decoding AceInfinity 0 2,106 05-07-2013, 12:10 AM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)