- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 Swift 中,我创建了一个 SecKeyRef
通过调用对象 SecTrustCopyPublicKey
在一些原始 X509 证书数据上。这就是这个 SecKeyRef
对象的样子。
Optional(<SecKeyRef algorithm id: 1,
key type: RSAPublicKey,
version: 3, block size: 2048 bits,
exponent: {hex: 10001, decimal: 65537},
modulus: <omitted a bunch of hex data>,
addr: 0xsomeaddresshere>)
基本上,这个SecKeyRef
对象保存了一大堆关于 公钥的信息,但似乎没有办法实际转换这个SecKeyRef
转换成字符串、NSData
或其他任何东西(这是我的目标,只是为了获得一个 base64 公钥)。
但是,我有一个函数,我可以给出一个模数
和一个指数
,它只会计算公钥是什么。我通过传递从上面的 SecKeyRef
记录的数据对其进行了测试。
但不知何故,我无法从 SecKeyRef
对象访问这些属性(我只能在控制台中看到整个对象;例如,我无法执行 SecKeyRef.modulus
或任何类似的东西)。
我的问题:如何访问 SecKeyRef.modulus
,或者将此 SecKeyRef
转换为 NSData
或类似的东西? 谢谢
(更多信息)
我正在动态创建我的SecKeyRef
,通过这个函数我有:
func bytesToPublicKey(certData: NSData) -> SecKeyRef? {
guard let certRef = SecCertificateCreateWithData(nil, certData) else { return nil }
var secTrust: SecTrustRef?
let secTrustStatus = SecTrustCreateWithCertificates(certRef, nil, &secTrust)
if secTrustStatus != errSecSuccess { return nil }
var resultType: SecTrustResultType = UInt32(0) // result will be ignored.
let evaluateStatus = SecTrustEvaluate(secTrust!, &resultType)
if evaluateStatus != errSecSuccess { return nil }
let publicKeyRef = SecTrustCopyPublicKey(secTrust!)
return publicKeyRef
}
它所做的是从证书中获取原始字节流(可以从使用 PKI 的硬件中广播),然后将其转换为 SecKeyRef
。
(截至 2015 年 1 月 7 日对现有答案的评论)
这不起作用:
let mirror = Mirror(reflecting: mySecKeyObject)
for case let (label?, value) in mirror.children {
print (label, value)
}
这会在控制台中产生以下输出:
Some <Raw SecKeyRef object>
不确定字符串“Some”是什么意思。
此外,mirror.descendant("exponent")
(或“modulus”)导致 nil
,即使在控制台中打印原始对象时,我可以清楚地看到这些属性存在,并且它们实际上已被填充。
此外,如果可能的话,我想避免必须保存到钥匙串(keychain),读取为 NSData
,然后从钥匙串(keychain)中删除。如赏金描述中所述,如果这是唯一可能的方法,请引用权威引用。感谢您迄今为止提供的所有答案。
最佳答案
确实可以使用既不使用 key 链也不使用 私有(private) API 来提取模数和指数。
有 ( public but undocumented ) 函数 SecKeyCopyAttributes
提取 CFDictionary
来自SecKey
.属性键的一个有用来源是 SecItemConstants.c
检查这本词典的内容,我们找到一个条目 "v_Data" : <binary>
.它的内容是DER-encoded ASN对于
SEQUENCE {
modulus INTEGER,
publicExponent INTEGER
}
请注意,如果整数是正数并且有一个前导 1 位(以免将它们与二补负数混淆),则整数将用零字节填充,因此您可能会发现比预期多一个字节.如果发生这种情况,请将其切掉。
您可以为此格式实现解析器,或者在知道 key 大小的情况下对提取进行硬编码。对于 2048 位 key (和 3 字节指数),格式为:
30|82010(a|0) # Sequence of length 0x010(a|0)
02|82010(1|0) # Integer of length 0x010(1|0)
(00)?<modulus>
02|03 # Integer of length 0x03
<exponent>
总共 10 + 1? + 256 + 3 = 269 或 270 字节。
import Foundation
extension String: Error {}
func parsePublicSecKey(publicKey: SecKey) -> (mod: Data, exp: Data) {
let pubAttributes = SecKeyCopyAttributes(publicKey) as! [String: Any]
// Check that this is really an RSA key
guard Int(pubAttributes[kSecAttrKeyType as String] as! String)
== Int(kSecAttrKeyTypeRSA as String) else {
throw "Tried to parse non-RSA key as RSA key"
}
// Check that this is really a public key
guard Int(pubAttributes[kSecAttrKeyClass as String] as! String)
== Int(kSecAttrKeyClassPublic as String)
else {
throw "Tried to parse non-public key as public key"
}
let keySize = pubAttributes[kSecAttrKeySizeInBits as String] as! Int
// Extract values
let pubData = pubAttributes[kSecValueData as String] as! Data
var modulus = pubData.subdata(in: 8..<(pubData.count - 5))
let exponent = pubData.subdata(in: (pubData.count - 3)..<pubData.count)
if modulus.count > keySize / 8 { // --> 257 bytes
modulus.removeFirst(1)
}
return (mod: modulus, exp: exponent)
}
(我最终编写了一个完整的 ASN 解析器,所以这段代码没有经过测试,请注意!)
请注意,您可以用非常相似的方式提取私钥 的详细信息。使用 DER 术语,这是 v_Data
的格式:
PrivateKey ::= SEQUENCE {
version INTEGER,
modulus INTEGER, -- n
publicExponent INTEGER, -- e
privateExponent INTEGER, -- d
prime1 INTEGER, -- p
prime2 INTEGER, -- q
exponent1 INTEGER, -- d mod (p-1) (dmp1)
exponent2 INTEGER, -- d mod (q-1) (dmq1)
coefficient INTEGER, -- (inverse of q) mod p (coeff)
otherPrimeInfos OtherPrimeInfos OPTIONAL
}
手动解析这个可能是不明智的,因为任何整数都可能已被填充。
注意事项:如果 key 是在 macOS 上生成的,则公钥的格式会有所不同;上面给出的结构是这样包装的:
SEQUENCE {
id OBJECTID,
PublicKey BITSTRING
}
位串是上述形式的 DER 编码 ASN。
关于ios - 我可以从 Swift 中的 SecKeyRef 对象获取模数或指数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408825/
是否有更快的算法来计算 (n! modulo m)。在每个乘法步骤都比减少更快。并且有没有比左右二元法更快的算法来计算 (a^p modulo m)。 这是我的代码:n!模数m ans=1 for(i
我有非常简单的代码循环遍历数组中的元素并检查是否index % 2 == 0。如果是这样,它应该改变颜色。 var e = document.getElementById("list").childN
让我简短一点。我正在计算 alert((Math.pow(7,35))%71) 但它给了我 61,而结果必须是 70。怎么了? 最佳答案 正如其他人之前提到的关于使用 Math.pow(7,35) 的
我试图弄清楚如何在汇编中计算模 10,所以我在 gcc 中编译了以下 c 代码,看看它想出了什么。 unsigned int i=999; unsigned int j=i%10; 令我惊讶的是我得到
例如使用以下输入: int num = -100 int divisor = 10 => -100 mod 10 = 0 (Edge-case: negative numbers as inpu
这个问题在这里已经有了答案: Random float number generation (14 个答案) 关闭 9 年前。 在 C++ 中,我希望得到一个随机 float 。据我所知,典型的随机
我试图找到潜在阶乘素数的除数(n!+-1 形式的数),因为我最近购买了 Skylake-X 工作站,我认为我可以使用 AVX512 指令提高一些速度。 算法简单,主要步骤是对同一个除数重复取模。主要是
我有一个保存角度(以度为单位)的变量,该角度可以是正值也可以是负值。我现在需要确保该数字仅在 0 到 360 之间。该数字是 double 。 执行此操作的好算法是什么?简单地执行角度 % 360 是
我有一个 UInt8 数组,我想计算 CheckSum8 模 256。如果字节总和小于 255,checkSum 函数返回正确的值。 例如 let bytes1 : [UInt8] = [1, 0xa
使用海湾合作委员会: printf("%i \n", -1 % (int)4); printf("%u \n", -1 % (unsigned int)4); 输出: -1 3 我可以跨平台依赖这种行
我无法理解代码中几行的含义。我最近开始学习 C++,并阅读了 Bjarne Stroustrup 的“编程:使用 C++ 的原理和实践”。第四章有个问题让我很困惑,所以我在网上搜索了一个解决方案以供引
我试图解决一个涉及大阶乘模质数的问题,并在另一个人的解决方案中发现了以下算法: long long factMod (long long n, long long p) { long long
我正在尝试计算 𝐹𝑛 模 𝑚,其中 𝑛 可能非常大:高达 10^18,𝐹𝑛 是第 n 个斐波那契数这是我的代码,它适用于小数字,但对于大数字,它会抛出 OutOfMemoryError 或
我有两个以 16 为模的循环整数,因此它们的值介于 0 和 15 之间。 我需要比较两个数字以确定 n_1 是否大于 n_0 n_1 > n_0 很明显,这个没有准确定义,所以我定义n_1如果小于前面
我一直在尝试使用 Java 处理一些更大的值,但遇到了一些我不理解的问题。出于某种原因,Java 似乎喜欢给我垃圾数据(尽管,我更可能告诉它给我垃圾数据) 这是一个片段,为清楚起见进行了编辑:
好吧,我今天做了一个小函数,它应该会生成一个随机字符串。 std::string randString(size_t length) { std::string randStr; fo
Ruby 的负数取模规则不明确。在 IRB 中: -7 % 3 == 2 应该是1!为什么? 最佳答案 因为 -7/3 在 Ruby 的整数除法语义下是 -3。 3*-3 是 -9,所以会留下 2
这个问题在这里已经有了答案: Calculating pow(a,b) mod n (14 个答案) 关闭 6 年前。 在 Javascript 中是否有获取大数模数的技巧。我用 modulo(7,
此代码使用公式 (a^x) % 101 检查值 a 是否唯一映射到值 1 到 100 local function f(a) found = {} bijective = true
在《Core Java Volume1》一书中有一条警告: CAUTION: The right-hand side argument of the shift operators is reduce
我是一名优秀的程序员,十分优秀!