从模数和指数生成 public 密钥
Generating public key from modulus and exponent
我有模数
MjAxNzY5NTQ0ODc1ODUxMzcyOTQ3ODM5MjI1NzQ2MzQ1MTUzNjQxMjMxODE4NTE1NzQwODUwODI0NjYxMTA4NTA0MTc2ODU4MDUzMzE2ODIwMzI2NDcyMDI2NTkyMjc0MjgwMjE1NDg3MDEwNzU2NzA3OTU4NzQ2MTYxNzI5NDc5OTUzMzAwOTM0OTA4MTc4NDI2MTM5NDc3MDMxNzkxMzg0MDAyOTE5NjMyMTAyMDA5MjEwNDQ4MzU1MzYwOTY0ODkxODAxNjY5MTM4MTQ4NjU0Njg3OTA0NjUxNTUxOTIzMDU4MjQ0Njg2MTQ5NzkzMTQwNzYzODM2MjY1MjA2NjcyMjY4MjQzMDE2MjA4MTQ3NjAwMzIwNzI2MTIzNzQ4MjA5MzIwODIwODc2ODMxNzgzNzA4NTYyMzg5MzI2OTc2NTM2NjgyNzY0MDgwMTM3ODY1MjIyNjc5OTQ3NDMwMzIwMDE0NTAzMDE2ODQyMTQyNjgxNjMwNTA4OTQ1MDU3OTgzMDEzNDMwNDYxMDY5OTA3NjI0MzU3Mjc5MjU0MTQ5NDUzMzMyODUxNjkyOTc3OTIxMTUzNjIzOTg1Nzk0NzkyMzY2NDY2NDQwNTczMTQxMjc2ODAwOTU1MTU4NTQxOTk4ODM1MDIyMjk2NTE1ODU3ODI5Mjk2NzMzNjM1MjE3Mjk4ODYxNDYwODg3NjY4NDU4MjAzMzc3NjM5ODc2MTMyMTQ1MjczODAzOTAzMzU0ODA5MTYzNjEwNDE3ODgwNTAyNzA1NTM2NzMwNzQ1OTc1MzgwMjM2NjI5MjY3NjkzOTU2NTcyMDE=
和指数
65537
请帮助我生成 public 使用模数和指数使用 objective-C 的密钥。
正如 zaph 在 中所写,下面的代码应该可以满足您的要求:
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);
为此,请确保您已链接 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;
我有模数
MjAxNzY5NTQ0ODc1ODUxMzcyOTQ3ODM5MjI1NzQ2MzQ1MTUzNjQxMjMxODE4NTE1NzQwODUwODI0NjYxMTA4NTA0MTc2ODU4MDUzMzE2ODIwMzI2NDcyMDI2NTkyMjc0MjgwMjE1NDg3MDEwNzU2NzA3OTU4NzQ2MTYxNzI5NDc5OTUzMzAwOTM0OTA4MTc4NDI2MTM5NDc3MDMxNzkxMzg0MDAyOTE5NjMyMTAyMDA5MjEwNDQ4MzU1MzYwOTY0ODkxODAxNjY5MTM4MTQ4NjU0Njg3OTA0NjUxNTUxOTIzMDU4MjQ0Njg2MTQ5NzkzMTQwNzYzODM2MjY1MjA2NjcyMjY4MjQzMDE2MjA4MTQ3NjAwMzIwNzI2MTIzNzQ4MjA5MzIwODIwODc2ODMxNzgzNzA4NTYyMzg5MzI2OTc2NTM2NjgyNzY0MDgwMTM3ODY1MjIyNjc5OTQ3NDMwMzIwMDE0NTAzMDE2ODQyMTQyNjgxNjMwNTA4OTQ1MDU3OTgzMDEzNDMwNDYxMDY5OTA3NjI0MzU3Mjc5MjU0MTQ5NDUzMzMyODUxNjkyOTc3OTIxMTUzNjIzOTg1Nzk0NzkyMzY2NDY2NDQwNTczMTQxMjc2ODAwOTU1MTU4NTQxOTk4ODM1MDIyMjk2NTE1ODU3ODI5Mjk2NzMzNjM1MjE3Mjk4ODYxNDYwODg3NjY4NDU4MjAzMzc3NjM5ODc2MTMyMTQ1MjczODAzOTAzMzU0ODA5MTYzNjEwNDE3ODgwNTAyNzA1NTM2NzMwNzQ1OTc1MzgwMjM2NjI5MjY3NjkzOTU2NTcyMDE=
和指数
65537
请帮助我生成 public 使用模数和指数使用 objective-C 的密钥。
正如 zaph 在
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);
为此,请确保您已链接 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;