从模数和指数生成 RSA 公钥 [英] Generate RSA Public Key from Modulus and Exponent

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

问题描述

我希望从 Objective-C 中的模数和指数生成一个 rsa 公钥 (pem).这个功能可以在Java中通过以下方式完成;

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent));

尽管我在为 iOS 平台编写此代码时遇到了一些麻烦.知道 OpenSSL,但我找不到成功的方法,但仍然无法生成此密钥.还查看了 SCZ-BasicEncodingRules-iOS 但这返回一个 NSData 值,我一直在想如何尝试将它转换为 NSString.

解决方案

如果您想创建 .PEM 格式,您需要对 berData 方法的输出进行 base64 编码.您还需要添加页眉和页脚行.

测试数据和代码,见bytesFromHexString答案的结尾:

 <代码>的NSString * modulusString = @ c19bccae1e67743fab1c978f03122fb1a78ef05d565a2964728062ad0365e4751b8253df5fd13ab4ecb95c81ff17b91f969e4fb3d8274c30533338684278f6e5548027df775c055943a24a4117b0274c296c68b722c71670d4b21489a3da05d37ba06f2fb771b671a2c746bae4a049dc718fba19a75f1fb8ae1dd715b33d66a3";NSString *exponentString = @"010001";NSData *pubKeyModData = bytesFromHexString(modulusString);NSData *pubKeyExpData = bytesFromHexString(exponentString);NSArray *keyArray = @[pubKeyModData, pubKeyExpData];//假设您使用的是 SCZ-BasicEncodingRules-iOS:NSData *berData = [keyArray berData];NSLog(@"berData:
%@", berData);NSString *berBase64 = [berData base64EncodedStringWithOptions:0];NSString *preamble = @"-----BEGIN CERTIFICATE REQUEST-----";NSString *postamble = @"-----END CERTIFICATE REQUEST-----";NSString *pem = [NSString stringWithFormat:@"%@
%@
%@", preamble, berBase64, postamble];NSLog(@"pem:
%@", pem);

输出测试数据:

<前>数据:30818802 8180c19b ccae1e67 743fab1c 978f0312 2fb1a78e f05d565a 29647280 62ad0365 e4751b82 53df5fd1 3ab4ecb9 5c81ff17 b91f969e 4fb3d827 4c305333 38684278 f6e55480 27df775c 055943a2 4a4117b0 274c296c 68b722c7 1670d4b2 1489a3da 05d37ba0 6f2fb771 b671a2c7 46bae4a0 49dc718f ba19a75f 1fb8ae1d d715b33d 66a30203 010001佩姆:-----开始申请证书-----MIGIAoGAwZvMrh5ndD + rHJePAxIvsaeO8F1WWilkcoBirQNl5HUbglPfX9E6tOy5XIH/F7kflp5Ps9gnTDBTMzhoQnj25VSAJ993XAVZQ6JKQRewJ0wpbGi3IscWcNSyFImj2gXTe6BvL7dxtnGix0a65KBJ3HGPuhmnXx + 4rh3XFbM9ZqMCAwEAAQ ==-----结束证书申请-----

将 hex-ascii 转换为数据:

NSData* bytesFromHexString(NSString * aString) {NSString *theString = [[aString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:nil];NSMutableData* 数据 = [NSMutableData 数据];国际idx;for (idx = 0; idx+2 <= theString.length; idx+=2) {NSRange 范围 = NSMakeRange(idx, 2);NSString* hexStr = [theString substringWithRange:range];NSScanner* 扫描仪 = [NSScanner 扫描仪WithString:hexStr];无符号整数 intValue;if ([扫描仪 scanHexInt:&intValue])[数据 appendBytes:&intValue 长度:1];}返回数据;}

I'm looking to generate a rsa public key (pem) from both the modulus and exponent in Objective-C. This function can be done in Java by the following;

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent));

Although I'm having some trouble writing this for the iOS platform. Am aware of OpenSSL, but I couldn't find a successful method and am still unable to generate this key. Also took a look at SCZ-BasicEncodingRules-iOS but this returns a NSData value and I'm stuck trying to figure out how to try convert it to a NSString.

解决方案

If you want to create a .PEM format you need to base64 encode the output from the berData method. You also need to add the header and footer lines.

Test data and code, see the end of the answer for bytesFromHexString:

NSString *modulusString =  @"c19bccae1e67743fab1c978f03122fb1a78ef05d565a2964728062ad0365e4751b8253df5fd13ab4ecb95c81ff17b91f969e4fb3d8274c30533338684278f6e5548027df775c055943a24a4117b0274c296c68b722c71670d4b21489a3da05d37ba06f2fb771b671a2c746bae4a049dc718fba19a75f1fb8ae1dd715b33d66a3";
NSString *exponentString = @"010001";

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:
%@", berData);

NSString *berBase64 = [berData base64EncodedStringWithOptions:0];
NSString *preamble = @"-----BEGIN CERTIFICATE REQUEST-----";
NSString *postamble = @"-----END CERTIFICATE REQUEST-----";
NSString *pem = [NSString stringWithFormat:@"%@
%@
%@", preamble, berBase64, postamble];
NSLog(@"pem:
%@", pem);

Output with test data:

berData:
30818802 8180c19b ccae1e67 743fab1c 978f0312 2fb1a78e f05d565a 29647280 62ad0365 e4751b82 53df5fd1 3ab4ecb9 5c81ff17 b91f969e 4fb3d827 4c305333 38684278 f6e55480 27df775c 055943a2 4a4117b0 274c296c 68b722c7 1670d4b2 1489a3da 05d37ba0 6f2fb771 b671a2c7 46bae4a0 49dc718f ba19a75f 1fb8ae1d d715b33d 66a30203 010001

pem:
-----BEGIN CERTIFICATE REQUEST-----
  MIGIAoGAwZvMrh5ndD+rHJePAxIvsaeO8F1WWilkcoBirQNl5HUbglPfX9E6tOy5XIH/F7kflp5Ps9gnTDBTMzhoQnj25VSAJ993XAVZQ6JKQRewJ0wpbGi3IscWcNSyFImj2gXTe6BvL7dxtnGix0a65KBJ3HGPuhmnXx+4rh3XFbM9ZqMCAwEAAQ==
-----END CERTIFICATE REQUEST-----

Convert hex-ascii to data:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆