How can I use a PKCS8 RSA DER Private Key in iOS?

后端 未结 2 1686
忘掉有多难
忘掉有多难 2021-01-14 10:46

At run time, my iOS application receives a file with a public-private RSA key-pair, generated by someone else\'s Java:

KeyPairGenerator keygenerator;
keygene         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 11:08

    You can see how a der key looks in ASN1 structure in this webpage: https://lapo.it/asn1js/

    Here is a code from the SwCrypt library, which strips the PKCS8 header from a private key. This is Swift, but you can rewrite it any other language easily.

        static private func stripHeaderIfAny(keyData: NSData) throws -> NSData {
            var bytes = keyData.arrayOfBytes()
    
            var offset = 0
            guard bytes[offset] == 0x30 else {
                throw SwError.ASN1Parse
            }
            offset += 1
    
            if bytes[offset] > 0x80 {
                offset += Int(bytes[offset]) - 0x80
            }
            offset += 1
    
            guard bytes[offset] == 0x02 else {
                throw SwError.ASN1Parse
            }
            offset += 3
    
            //without PKCS8 header
            if bytes[offset] == 0x02 {
                return keyData
            }
    
            let OID: [UInt8] = [0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
                                0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00]
            let slice: [UInt8] = Array(bytes[offset..<(offset + OID.count)])
    
            guard slice == OID else {
                throw SwError.ASN1Parse
            }
    
            offset += OID.count
            guard bytes[offset] == 0x04 else {
                throw SwError.ASN1Parse
            }
    
            offset += 1
            if bytes[offset] > 0x80 {
                offset += Int(bytes[offset]) - 0x80
            }
            offset += 1
    
            guard bytes[offset] == 0x30 else {
                throw SwError.ASN1Parse
            }
    
            return keyData.subdataWithRange(NSRange(location: offset, length: keyData.length - offset))
        }
    

提交回复
热议问题