本文介绍了从模数和指数生成公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模数

MjAxNzY5NTQ0ODc1ODUxMzcyOTQ3ODM5MjI1NzQ2MzQ1MTUzNjQxMjMxODE4NTE1NzQwODUwODI0NjYxMTA4NTA0MTc2ODU4MDUzMzE2ODIwMzI2NDcyMDI2NTkyMjc0MjgwMjE1NDg3MDEwNzU2NzA3OTU4NzQ2MTYxNzI5NDc5OTUzMzAwOTM0OTA4MTc4NDI2MTM5NDc3MDMxNzkxMzg0MDAyOTE5NjMyMTAyMDA5MjEwNDQ4MzU1MzYwOTY0ODkxODAxNjY5MTM4MTQ4NjU0Njg3OTA0NjUxNTUxOTIzMDU4MjQ0Njg2MTQ5NzkzMTQwNzYzODM2MjY1MjA2NjcyMjY4MjQzMDE2MjA4MTQ3NjAwMzIwNzI2MTIzNzQ4MjA5MzIwODIwODc2ODMxNzgzNzA4NTYyMzg5MzI2OTc2NTM2NjgyNzY0MDgwMTM3ODY1MjIyNjc5OTQ3NDMwMzIwMDE0NTAzMDE2ODQyMTQyNjgxNjMwNTA4OTQ1MDU3OTgzMDEzNDMwNDYxMDY5OTA3NjI0MzU3Mjc5MjU0MTQ5NDUzMzMyODUxNjkyOTc3OTIxMTUzNjIzOTg1Nzk0NzkyMzY2NDY2NDQwNTczMTQxMjc2ODAwOTU1MTU4NTQxOTk4ODM1MDIyMjk2NTE1ODU3ODI5Mjk2NzMzNjM1MjE3Mjk4ODYxNDYwODg3NjY4NDU4MjAzMzc3NjM5ODc2MTMyMTQ1MjczODAzOTAzMzU0ODA5MTYzNjEwNDE3ODgwNTAyNzA1NTM2NzMwNzQ1OTc1MzgwMjM2NjI5MjY3NjkzOTU2NTcyMDE =

和指数

65537

请帮助我使用 Objective-C 使用模数和指数生成公钥.

Please help me on generating public key using modulus and exponent using objective-C.

推荐答案

正如 zaph 在这个答案中所写的那样,以下代码应该做你想做的:

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);

这篇关于从模数和指数生成公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 17:10