Wstunes / SwiftyJWT

A library to generate JWT with Swift
MIT License
48 stars 30 forks source link

let privateKey = try! RSAKey.init(base64String: "keyString", keyType: .PRIVATE) exception on this line always no matter what you do #4

Closed Abhijitg11 closed 6 years ago

Abhijitg11 commented 6 years ago

let publicKeyAttr: [NSObject: NSObject] = [ kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"com.xeoscript.app.RsaFromScrach.public".data(using: String.Encoding.utf8)! as NSObject, kSecClass: kSecClassKey, // added this value kSecReturnData: kCFBooleanTrue] // added this value let privateKeyAttr: [NSObject: NSObject] = [ kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"comxeoscriptappRsaFromScrachprivate".data(using: String.Encoding.utf8)! as NSObject, kSecClass: kSecClassKey, // added this value kSecReturnData: kCFBooleanTrue] // added this value

    var keyPairAttr = [NSObject: NSObject]()
    keyPairAttr[kSecAttrKeyType] = kSecAttrKeyTypeRSA
    keyPairAttr[kSecAttrKeySizeInBits] = 2048 as NSObject
    keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttr as NSObject
    keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAttr as NSObject

    let statusCode:OSStatus = SecKeyGeneratePair(keyPairAttr as CFDictionary, &publicKey, &privateKey)

    if statusCode == noErr && publicKey != nil && privateKey != nil {
        print("Key pair generated OK")
        var resultPublicKey: AnyObject?
        var resultPrivateKey: AnyObject?
        let statusPublicKey = SecItemCopyMatching(publicKeyAttr as CFDictionary, &resultPublicKey)
        let statusPrivateKey = SecItemCopyMatching(privateKeyAttr as CFDictionary, &resultPrivateKey)

        if statusPublicKey == noErr {
            if let publicKey = resultPublicKey as? Data {
                print("Public Key: \((publicKey.base64EncodedString()))")
            }
        }

        if statusPrivateKey == noErr {
            if let privateKey = resultPrivateKey as? Data {
                print("Private Key: \((privateKey.base64EncodedString()))")

                let privateKey = try! RSAKey.init(base64String: (privateKey.base64EncodedString()), keyType: .PRIVATE)

            }
        }
    } else {
        print("Error generating key pair: \(String(describing: statusCode))")
    }

let privateKey = try! RSAKey.init(base64String: (privateKey.base64EncodedString()), keyType: .PRIVATE) Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: SwiftyCrypto.SwiftyCryptoError.keyCreateFailed(Optional(Error Domain=NSOSStatusErrorDomain Code=-50 "Unsupported key type: (null)" UserInfo={NSDescription=Unsupported key type: (null)}))

let privateKey = try! RSAKey.init(base64String: "keyString", keyType: .PRIVATE) Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: SwiftyCrypto.SwiftyCryptoError.invalidBase64String

Wstunes commented 6 years ago

The exception is raised by the invalid private key string. "keyString" appears to be an invalid string. You can try to set an valid base64String parameter to RSAKey.init method to see if the privateKey is generated properly.

Abhijitg11 commented 6 years ago

let longstring = "test123" let data = (longstring).data(using: String.Encoding.utf8) let base64 = data!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) let privateKeyC = try! RSAKey.init(base64String: base64, keyType: .PRIVATE)

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: SwiftyCrypto.SwiftyCryptoError.asn1ParsingFailed

Wstunes commented 6 years ago

"test123" appears to be an invalid private key string. Try to use a valid string. You can use an online RSA Key generator to test the function.

Abhijitg11 commented 6 years ago

yes thanks . It works fine i was passing a wrong key string