gpt4 book ai didi

ios - 从 iOS 中的 kSecAttrApplicationTag 中提取模数

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:03:55 24 4
gpt4 key购买 nike

我正在尝试从 kSecAttrApplicationTag(我从 URL 获取公钥信息)中提取模数。我想比较来自服务器和手动输入的公钥。如果两者相同,那么我们将允许用户继续。否则,它将取消用户的请求。这是一段代码:

SecTrustRef trust = [protectionSpace serverTrust];
SecKeyRef publicKey = SecTrustCopyPublicKey(trust); // here i'm getting complete SecKeyRef info
NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
[queryPublicKey setObject:(__bridge id)publicKey forKey:(__bridge id)kSecAttrApplicationTag];

我也尝试使用 kSecAttrApplicationTagpublickey 获取模数,但没有成功。

kSecAttrApplicationTag 键的输出是:

{
atag = "<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 2, block size: 2048 bits, exponent: {hex: 10001, decimal: 65537}, modulus: 98A8819BAEA1361029F78D1FA35B4A39A9C6D017501BD56C8D656CAC0800DE90BCF93D465CC7CAFB697841B3DCF47CE4F35E00CC3CC163, addr: 0xa065400>";}

publicKey 的输出与 kSecAttrApplicationTag 相同,但没有 {atag =}(它包含其余内容)。

有人可以帮我解决如何提取模数的问题吗?

最佳答案

看起来 atag 是 XML,所以有几种方法。

  1. 使用 XML 解析器:NSXMLParser
  2. 使用正则表达式:NSString 方法 rangeOfString:options: 和选项 NSRegularExpressionSearch
  3. 使用 NSScanner

使用正则表达式:

NSString *atag = @"<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 2, block size: 2048 bits, exponent: {hex: 10001, decimal: 65537}, modulus: 98A8819BAEA1361029F78D1FA35B4A39A9C6D017501BD56C8D656CAC0800DE90BCF93D465CC7CAFB697841B3DCF47CE4F35E00CC3CC163, addr: 0xa065400>";

NSRange range;

NSString *blockSizeRegex = @"(?<=block size:\\s?)[0-9]+";
range = [atag rangeOfString:blockSizeRegex options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
NSString *blockSize = [atag substringWithRange:range];
NSLog(@"blockSize: %@", blockSize);
}

NSString *modulusRegex = @"(?<=modulus:\\s?)[0-9A-Z]+";
range = [atag rangeOfString:modulusRegex options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
NSString *modulus = [atag substringWithRange:range];
NSLog(@"modulus: %@", modulus);
}

NSString *exponentRegex = @"(?<=decimal:\\s?)[0-9]+";
range = [atag rangeOfString:exponentRegex options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
NSString *exponent = [atag substringWithRange:range];
NSLog(@"exponent: %@", exponent);
}

输出:

blockSize: 2048
modulus: 98A8819BAEA1361029F78D1FA35B4A39A9C6D017501BD56C8D656CAC0800DE90BCF93D465CC7CAFB697841B3DCF47CE4F35E00CC3CC163
exponent: 65537

ICU 用户指南 Regular Expressions

关于ios - 从 iOS 中的 kSecAttrApplicationTag 中提取模数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com