This is the droid you are looking for:
Editor > Syntax Coloring > Markdown
Post
Replies
Boosts
Views
Activity
Hi, thank you for making that script, however i still can't get it to work.
I created a key like mentioned in the script:
openssl ecparam -name prime256v1 -genkey -noout -out openssl.key -outform der
And converted it using the script:
./convert-ec-private-key-openssl-to-apple.rb openssl.key out.der
Private key start: 6, length: 32
Private key: 5...3
Public key start: 56, length: 66
Public key: 047481669ec2e9835109c55c574c78237b7a98e8743e0eec41e44ff3c496a648f7665bbdc0e1ff530a100796a3763a4b4768e6fec2538edb3ffe5ce5eeaa208cec
The filesize is 97bytes suggesting a correct conversion, and it starts with 0x04:
hexdump -Cv out.der
00000000 04 74 81 66 9e c2 e9 83 51 09 c5 5c 57 4c 78 23 |.t.f....Q..\WLx#|
00000010 7b 7a 98 e8 74 3e 0e ec 41 e4 4f f3 c4 96 a6 48 |{z..t>..A.O....H|
00000020 f7 66 5b bd c0 e1 ff 53 0a 10 07 96 a3 76 3a 4b |.f[....S.....v:K|
00000030 47 68 e6 fe c2 53 8e db 3f fe 5c e5 ee aa 20 8c |Gh...S..?.\... .|
00000040 ec 57 a4 64 ed dc a2 ee 13 ef 12 5f e2 2b 5a 22 |.W.d......._.+Z"|
00000050 c6 64 2b 9c 54 61 6f 1b 75 00 53 13 e7 90 71 6a |.d+.Tao.u.S...qj|
00000060 73 |s|
00000061
But when i try to load it, i still get the dreaded code -50 with "EC public key creation from data failed". What am i doing wrong?
Here is my loading code:
private static func loadPublicKey() -> SecKey? {
guard let publicKeyData = try? Data(contentsOf: Bundle.main.url(forResource: "out", withExtension: "der")!) else {
print("Failed to load public key data from file")
return nil
}
print("Public key data length: \(publicKeyData.count)")
let byteArray = publicKeyData.map { String(format: "%02x", $0) }
print("Public key data bytes (loaded in iOS): \(byteArray.joined(separator: " "))")
let attributes: [String: Any] = [
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeyClass as String: kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits as String: 256
]
var error: Unmanaged<CFError>?
guard let secKey = SecKeyCreateWithData(publicKeyData as CFData, attributes as CFDictionary, &error) else {
if let error = error {
print("SecKeyCreate init failed: \(error.takeRetainedValue() as Error)")
} else {
print("SecKeyCreate init failed with unknown error")
}
return nil
}
return secKey
}