-
Notifications
You must be signed in to change notification settings - Fork 2
/
RSAKeyGenerator.m
153 lines (106 loc) · 4.5 KB
/
RSAKeyGenerator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// RSAKeyGenerator.m
// RSA
//
// Created by Marcel Boersma on 6/11/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "RSAKeyGenerator.h"
#import "math.h"
@implementation RSAKeyGenerator
@synthesize primeA, primeB, n, z, publicKey, privateKey;
- (id)init
{
self = [super init];
if (self) {
NSLog(@"Init RSAKeyGenerator");
// Initialization code here.
self.primeA = [NSDecimalNumber decimalNumberWithString:@"1"];
self.primeB = [NSDecimalNumber decimalNumberWithString:@"1"];
self.n = [NSDecimalNumber decimalNumberWithString:@"1"];
self.z = [NSDecimalNumber decimalNumberWithString:@"2"];
self.publicKey = [NSDecimalNumber decimalNumberWithString:@"1"];
self.privateKey = [NSDecimalNumber decimalNumberWithString:@"1"];
}
return self;
}
-(void)calculateKeyWithPrimeA:(NSDecimalNumber*)aPrime andPrimeB:(NSDecimalNumber*)bPrime
{
NSLog(@"Do some calculations!!!!!");
self.primeA = aPrime;
self.primeB = bPrime;
//set public key
self.n = [aPrime decimalNumberByMultiplyingBy:bPrime];
//calculate z
self.z = [[aPrime decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:@"-1"]] decimalNumberByMultiplyingBy:[bPrime decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:@"-1"]]];
//calculate e such that gcd(e,z) = 1
for(int i = 0; i < [self.n intValue]; i++)
{
if([self GCDWithIntA:i andIntB:[self.z intValue]] == 1)
{
self.publicKey = [NSDecimalNumber decimalNumberWithString:[[NSNumber numberWithInt:i] stringValue]];
}
}
for(int i = 2; i < [self.n intValue]; i++)
{
int ed = [[self.publicKey decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:[[NSNumber numberWithInt:i] stringValue]]] intValue];
if([self.z intValue] != 0){
if( (ed % [self.z intValue]) == 1)
{
//found the number i such that e*i mod z = 1 (the inverse of e)
self.privateKey = [NSDecimalNumber decimalNumberWithString:[[NSNumber numberWithInt:i] stringValue]];
}
}
}
}
-(int) GCDWithIntA:(int)a andIntB:(int) b
{
if (b==0) return a;
return [self GCDWithIntA:b andIntB:a%b];
}
-(NSString*)decrypteMessage:(NSString*)message
{
#warning CAN'T CALCULATE NUMBERS OF THAT SIZE (SO RSA WON'T WORK!!!)
NSString *decryptedMessage = @"";
NSArray *chars = [message componentsSeparatedByString:@" "];
for(int i = 0; i < [chars count]; i++)
{
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:[chars objectAtIndex:i]];
[f release];
int charToDecrypt = [myNumber intValue];
NSDecimalNumber *bigNumber = [[[NSDecimalNumber alloc] initWithInteger:charToDecrypt] autorelease];
NSDecimalNumber *powOfBigNum = [[bigNumber decimalNumberByRaisingToPower:[self.privateKey intValue]] autorelease];
int decryptedChar = [powOfBigNum integerValue] % [self.n integerValue];
NSLog(@"self.n %i self.privateKey %i", [self.n intValue], [self.privateKey intValue]);
NSLog(@"Char to decrypt %i and decrypted char %i", [myNumber intValue], decryptedChar);
decryptedMessage = [decryptedMessage stringByAppendingString:[NSString stringWithFormat:@"%c ", decryptedChar]];
}
return decryptedMessage;
}
-(NSString*)encrypteMessage:(NSString*)message
{
#warning CAN'T CALCULATE NUMBERS OF THAT SIZE (SO RSA WON'T WORK!!!)
NSString *encryptedMessage = @"";
for(int i = 0; i < [message length]; i++)
{
char aCharFromMessage = [message characterAtIndex:i];
NSDecimalNumber *bigNumber = [[[NSDecimalNumber alloc] initWithInteger:aCharFromMessage] autorelease];
NSDecimalNumber *powOfBigNum = [[bigNumber decimalNumberByRaisingToPower:[self.publicKey intValue]] autorelease];
int encryptedChar = [powOfBigNum integerValue] % [self.n integerValue];
encryptedMessage = [encryptedMessage stringByAppendingString:[NSString stringWithFormat:@"%i ", encryptedChar]];
}
return encryptedMessage;
}
-(void)dealloc
{
[primeA release];
[primeB release];
[n release];
[z release];
[publicKey release];
[privateKey release];
[super dealloc];
}
@end