我有模数
MjAxNzY5NTQ0ODc1ODUxMzcyOTQ3ODM5MjI1NzQ2MzQ1MTUzNjQxMjMxODE4NTE1NzQwODUwODI0NjYxMTA4NTA0MTc2ODU4MDUzMzE2ODIwMzI2NDcyMDI2NTkyMjc0MjgwMjE1NDg3MDEwNzU2NzA3OTU4NzQ2MTYxNzI5NDc5OTUzMzAwOTM0OTA4MTc4NDI2MTM5NDc3MDMxNzkxMzg0MDAyOTE5NjMyMTAyMDA5MjEwNDQ4MzU1MzYwOTY0ODkxODAxNjY5MTM4MTQ4NjU0Njg3OTA0NjUxNTUxOTIzMDU4MjQ0Njg2MTQ5NzkzMTQwNzYzODM2MjY1MjA2NjcyMjY4MjQzMDE2MjA4MTQ3NjAwMzIwNzI2MTIzNzQ4MjA5MzIwODIwODc2ODMxNzgzNzA4NTYyMzg5MzI2OTc2NTM2NjgyNzY0MDgwMTM3ODY1MjIyNjc5OTQ3NDMwMzIwMDE0NTAzMDE2ODQyMTQyNjgxNjMwNTA4OTQ1MDU3OTgzMDEzNDMwNDYxMDY5OTA3NjI0MzU3Mjc5MjU0MTQ5NDUzMzMyODUxNjkyOTc3OTIxMTUzNjIzOTg1Nzk0NzkyMzY2NDY2NDQwNTczMTQxMjc2ODAwOTU1MTU4NTQxOTk4ODM1MDIyMjk2NTE1ODU3ODI5Mjk2NzMzNjM1MjE3Mjk4ODYxNDYwODg3NjY4NDU4MjAzMzc3NjM5ODc2MTMyMTQ1MjczODAzOTAzMzU0ODA5MTYzNjEwNDE3ODgwNTAyNzA1NTM2NzMwNzQ1OTc1MzgwMjM2NjI5MjY3NjkzOTU2NTcyMDE =
和指数
65537
请使用objective-C帮助我使用模数和指数生成公钥。
答案 0 :(得分:1)
正如zaph在{{3}}中所写,以下代码应该做你想做的事:
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);
答案 1 :(得分:0)
要执行此操作,请确保已链接OpenSSL库(此处为http://code.google.com/p/ios-static-libraries/说明)
链接后,您可以访问多个BIGNUM转换器。我使用BN_hex2bn方法将模数转换为十六进制,将十六进制字符串保存为'exponent'
然后创建BIGNUM结构并使用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;