I've Modulus
MjAxNzY5NTQ0ODc1ODUxMzcyOTQ3ODM5MjI1NzQ2MzQ1MTUzNjQxMjMxODE4NTE1NzQwODUwODI0NjYxMTA4NTA0MTc2ODU4MDUzMzE2ODIwMzI2NDcyMDI2NTkyMjc0MjgwMjE1NDg3MDEwNzU2NzA3OTU4NzQ2MTYxNzI5NDc5OTUzMzAwOTM0OTA4MTc4NDI2MTM5NDc3MDMxNzkxMzg0MDAyOTE5NjMyMTAyMDA5MjEwNDQ4MzU1MzYwOTY0ODkxODAxNjY5MTM4MTQ4NjU0Njg3OTA0NjUxNTUxOTIzMDU4MjQ0Njg2MTQ5NzkzMTQwNzYzODM2MjY1MjA2NjcyMjY4MjQzMDE2MjA4MTQ3NjAwMzIwNzI2MTIzNzQ4MjA5MzIwODIwODc2ODMxNzgzNzA4NTYyMzg5MzI2OTc2NTM2NjgyNzY0MDgwMTM3ODY1MjIyNjc5OTQ3NDMwMzIwMDE0NTAzMDE2ODQyMTQyNjgxNjMwNTA4OTQ1MDU3OTgzMDEzNDMwNDYxMDY5OTA3NjI0MzU3Mjc5MjU0MTQ5NDUzMzMyODUxNjkyOTc3OTIxMTUzNjIzOTg1Nzk0NzkyMzY2NDY2NDQwNTczMTQxMjc2ODAwOTU1MTU4NTQxOTk4ODM1MDIyMjk2NTE1ODU3ODI5Mjk2NzMzNjM1MjE3Mjk4ODYxNDYwODg3NjY4NDU4MjAzMzc3NjM5ODc2MTMyMTQ1MjczODAzOTAzMzU0ODA5MTYzNjEwNDE3ODgwNTAyNzA1NTM2NzMwNzQ1OTc1MzgwMjM2NjI5MjY3NjkzOTU2NTcyMDE=
and Exponent
65537
Please help me on generating public key using modulus and exponent using objective-C.
As written by zaph in this answer, the following code should do what you want :
NSData* bytesFromHexString(NSString * aString) {
NSString *theString = [[aString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:nil];
NSMutableData* data = [NSMutableData data];
int idx;
for (idx = 0; idx+2 <= theString.length; idx+=2) {
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [theString substringWithRange:range];
NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
if ([scanner scanHexInt:&intValue])
[data appendBytes:&intValue length:1];
}
return data;
}
NSString *modulusString = @"...";
NSString *exponentString = @"65537";
NSData *pubKeyModData = bytesFromHexString(modulusString);
NSData *pubKeyExpData = bytesFromHexString(exponentString);
NSArray *keyArray = @[pubKeyModData, pubKeyExpData];
//Given that you are using SCZ-BasicEncodingRules-iOS:
NSData *berData = [keyArray berData];
NSLog(@"berData:\n%@", berData);
NSString *berBase64 = [berData base64EncodedStringWithOptions:0];
NSString *preamble = @"-----BEGIN CERTIFICATE REQUEST-----";
NSString *postamble = @"-----END CERTIFICATE REQUEST-----";
NSString *pem = [NSString stringWithFormat:@"%@\n%@\n%@", preamble, berBase64, postamble];
NSLog(@"pem:\n%@", pem);
To do this make sure you have the OpenSSL library linked (instructions here http://code.google.com/p/ios-static-libraries/)
Once linked you'll have access to several BIGNUM converters. I turned the modulus into hex using the BN_hex2bn method saving the hex string into 'exponent'
Then create the BIGNUM struct and encrypt using RSA_public_encrypt
RSA *rsa = NULL;
rsa->n = BN_new();
BN_copy(rsa->n,modulus);
rsa->e = BN_new();
BN_copy(rsa->e,exponent);
rsa->iqmp=NULL;
rsa->d=NULL;
rsa->p=NULL;
rsa->q=NULL;