0

Numbers are entered into text fields and are then stored as integers. The user then enters numbers into a text view. Every number is put into an element of an array. Each element of the array is modified (decrypts to find ascii number value and then changes the number to a character). The newly modified number (to character) is added into the array by replacing the element it had previously modified. Code for this can be shown here:

NSString *n_string = _n_key_textfield.text;
    int n = [n_string intValue];
    NSString *d_string = _d_key_textfield.text;
    int d = [d_string intValue];
    NSString *toDecryptMessage = _messageToDecrypt.text;
    NSArray *words = [toDecryptMessage componentsSeparatedByString: @","];
    NSMutableArray *mutableWords = [NSMutableArray arrayWithCapacity:[words count]];
    [mutableWords addObjectsFromArray:words];
    for (int i = 0; i <[words count]; i++){
        //get value at index i
        //decrypt it
        //then becomees ascii number
        //convert ascii number to character
        //replace in array
        //print array
        NSString *string = mutableWords[i];
        NSLog(@"%@", string);
        NSInteger x = [string intValue];
        NSLog(@"%ld",(long)x);

        NSInteger encrypted_power = pow(x,d);
        //problem here
        NSInteger decrypted_number = encrypted_power % n;
        NSLog(@"decrypted number @%ld", (long)decrypted_number);
        NSString* charString = [NSString stringWithFormat:@"%ld" , (long)decrypted_number];
        [mutableWords replaceObjectAtIndex:i withObject:charString];
    }
    NSString *string = [mutableWords componentsJoinedByString:@" "];
    _finalDecryptedMessage.text = string;

When the program is ran with N = 4307, d = 3341, and with a breakpoint at:

NSInteger decrypted_number = encrypted_power % n;

...

n_string    NSTaggedPointerString * @"4307" 0xa000000373033344
n   int 4307
d_string    NSTaggedPointerString * @"3341" 0xa000000313433334
d   int 3341
toDecryptMessage    __NSCFString *  @"3442,3121"    0x000060800005f0e0
words   __NSArrayM *    @"2 elements"   0x000060800005ed20
mutableWords    __NSArrayM *    @"2 elements"   0x0000608000057a60
string  NSString *  0x1 0x0000000000000001
i   int 0
string  NSTaggedPointerString * @"3442" 0xa000000323434334
x   NSInteger   3442
encrypted_power NSInteger = (NSInteger) -9223372036854775808
decrypted_number    NSInteger   0
charString  NSString *  nil 0x0000000000000000

why is encrypted_power NSInteger equal to -9223372036854775808 and how do I solve this?

As always, any help is greatly appreciated.

Jordan Savage
  • 196
  • 2
  • 16
  • 2
    Wait…aren't you getting warnings when you compile this? `pow` doesn't return an integer; it returns a double. And 4307^3341 is enormous; astronomically larger than the largest 64-bit int (2^64). You can't do this with simple integer math. – Rob Napier Oct 13 '17 at 20:21
  • I expect `encrypted_power` to be the value at i to the power of d (3341) which would create a number larger than that. – Jordan Savage Oct 13 '17 at 20:21
  • 1
    Yeah; even pretty small values raised to the 3341th power are going to massively overflow integers. You can't just call pow() here. You'll need to work with a bigint system of some sort. – Rob Napier Oct 13 '17 at 20:22
  • I do not get any warnings, in my python script it works fine: for i in range(0,len(encr_mess)): i = 0 while (i < len(encr_mess)): c = encr_mess[i] m = (c**d)%n #RSA decryption algorithm o = chr(m) mess.append(o) i +=1 break #stop searching once every index is decrypted print(mess) #prints the decrypted message – Jordan Savage Oct 13 '17 at 20:23
  • 1
    Python has bigint support built-in. It doesn't optimize integers for speed like ObjC; it optimizes them to be convenient. Python is the ideal language for doing crypto work in. Nothing else is close in my experience. – Rob Napier Oct 13 '17 at 20:23
  • See https://stackoverflow.com/questions/1226949/biginteger-on-objective-c – Rob Napier Oct 13 '17 at 20:25
  • 2
    Instead of raising to a power and then mod, `NSInteger encrypted_power = pow(x,d); NSInteger decrypted_number = encrypted_power % n;`, mod as you go. [C example](https://stackoverflow.com/a/33311698/2410359) – chux - Reinstate Monica Oct 13 '17 at 20:28
  • @chux brilliant it works, thank you very much, to be honest I have no idea what 1u and >>= means as I have just stuck to very basic programming, thank you! – Jordan Savage Oct 13 '17 at 20:44
  • @JordanSavage `expo >>= 1u;` same as `expo = expo/2;`. `1u` insures unsigned math rather than signed math. Yet I am not versed in coding this in objective C. See https://en.wikipedia.org/wiki/Modular_exponentiation – chux - Reinstate Monica Oct 13 '17 at 21:18

0 Answers0