changing output in objective-c app
- by Zack
//
// RC4.m
// Play5
//
// Created by svp on 24.05.10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "RC4.h"
@implementation RC4
@synthesize txtLyrics;
@synthesize sbox;
@synthesize mykey;
- (IBAction) clicked: (id) sender
{
NSData *asciidata1 = [@"4875" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciistr1 = [[NSString alloc] initWithData:asciidata1 encoding:NSASCIIStringEncoding]; //[txtLyrics setText:@"go"];
NSData *asciidata = [@"sdf883jsdf22" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciistr = [[NSString alloc] initWithData:asciidata encoding:NSASCIIStringEncoding]; //RC4 * x = [RC4 alloc];
[txtLyrics setText:[self decrypt:asciistr1 andKey:asciistr]];
}
- (NSMutableArray*) hexToChars: (NSString*) hex
{
NSMutableArray * arr = [[NSMutableArray alloc] init];
NSRange range;
range.length = 2;
for (int i = 0; i < [hex length]; i = i + 2)
{
range.location = 0;
NSString * str = [[hex substringWithRange:range] uppercaseString];
unsigned int value;
[[NSScanner scannerWithString:str] scanHexInt:&value];
[arr addObject:[[NSNumber alloc] initWithInt:(int)value]];
}
return arr;
}
- (NSString*) charsToStr: (NSMutableArray*) chars
{
NSString * str = @"";
for (int i = 0; i < [chars count]; i++)
{
str = [NSString stringWithFormat:@"%@%@",[NSString stringWithFormat:@"%c", [chars objectAtIndex:i]],str];
}
return str;
}
//perfect except memory leaks
- (NSMutableArray*) strToChars: (NSString*) str
{
NSData *asciidata = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciistr = [[NSString alloc] initWithData:asciidata encoding:NSASCIIStringEncoding];
NSMutableArray * arr = [[NSMutableArray alloc] init];
for (int i = 0; i < [str length]; i++)
{
[arr addObject:[[NSNumber alloc] initWithInt:(int)[asciistr characterAtIndex:i]]];
}
return arr;
}
- (void) initialize: (NSMutableArray*) pwd
{
sbox = [[NSMutableArray alloc] init];
mykey = [[NSMutableArray alloc] init];
int a = 0;
int b;
int c = [pwd count];
int d = 0;
while (d < 256)
{
[mykey addObject:[pwd objectAtIndex:(d % c)]];
[sbox addObject:[[NSNumber alloc] initWithInt:d]];
d++;
}
d = 0;
while (d < 256)
{
a = (a + [[sbox objectAtIndex:d] intValue] + [[mykey objectAtIndex:d] intValue]) % 256;
b = [[sbox objectAtIndex:d] intValue];
[sbox replaceObjectAtIndex:d withObject:[sbox objectAtIndex:a]];
[sbox replaceObjectAtIndex:a withObject:[[NSNumber alloc] initWithInt:b]];
d++;
}
}
- (NSMutableArray*) calculate: (NSMutableArray*) plaintxt andPsw: (NSMutableArray*) psw
{
[self initialize:psw];
int a = 0;
int b = 0;
NSMutableArray * c = [[NSMutableArray alloc] init];
int d;
int e;
int f;
int g = 0;
while (g < [plaintxt count])
{
a = (a + 1) % 256;
b = (b + [[sbox objectAtIndex:a] intValue]) % 256;
e = [[sbox objectAtIndex:a] intValue];
[sbox replaceObjectAtIndex:a withObject:[sbox objectAtIndex:b]];
[sbox replaceObjectAtIndex:b withObject:[[NSNumber alloc] initWithInt:e]];
int h = ([[sbox objectAtIndex:a]intValue] + [[sbox objectAtIndex:b]intValue]) % 256;
d = [[sbox objectAtIndex:h] intValue];
f = [[plaintxt objectAtIndex:g] intValue] ^ d;
[c addObject:[[NSNumber alloc] initWithInt:f]];
g++;
}
return c;
}
- (NSString*) decrypt: (NSString*) src andKey: (NSString*) key
{
NSMutableArray * plaintxt = [self hexToChars:src];
NSMutableArray * psw = [self strToChars:key];
NSMutableArray * chars = [self calculate:plaintxt andPsw:psw];
NSData *asciidata = [[self charsToStr:chars] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciistr = [[NSString alloc] initWithData:asciidata encoding:NSUTF8StringEncoding];
return asciistr;
}
@end
This is supposed to decrypt a hex string with an ascii string, using rc4 decryption. I'm converting my java application to objective-c. The output keeps changing, every time i run it.