SwiftyRSA icon indicating copy to clipboard operation
SwiftyRSA copied to clipboard

Error creating private key object

Open JoanaDFerreira opened this issue 6 years ago • 4 comments

Hello,

I updated my project to run with swift 4.2 and with SwiftyRSA 1.5.0, but I got an error because the private key wasn't being created. I tested the private key and it was generating a valid one. Before updating, I was runing the same code with swift 4.0 and SwiftyRSA 1.4.0 and I got no problem.

I overcame this issue by using version 1.4.0 with swift 4.0, replacing the PublickKey.swift line: let range = Range<String.Index>(start..<end) for: let range = start..<end

  • Version of SwiftyRSA: 1.5.0
  • Code used to create the key:

open static func decryptWithPrivateKey(data: Data, privkey: Data) throws -> String { do { let encrypted = EncryptedMessage(data: data) let privateKey = try PrivateKey(data: privkey) let dclear = try encrypted.decrypted(with: privateKey, padding: .PKCS1) let dstr = try dclear.string(encoding: .utf8) return dstr } catch let error { logger("Problem decrypting data with RSA private key", error: error) throw CryptoError(.failDecryptingRSA) } }

  • Error in line: let privateKey = try PrivateKey(data: privkey)

JoanaDFerreira avatar Nov 29 '18 10:11 JoanaDFerreira

@JoanaDFerreira Thanks for opening an issue! What's the exact error that you're getting? Would you have an example of private key that you could share here so we can reproduce on our end?

ldiqual avatar Dec 03 '18 20:12 ldiqual

I don't know if it's related but in my case I'm looking for generate the pair of keys throught the Mac OS console with ssh-keygen -t rsa -f mykey -N ''. After this step both private and public keys are converted to .pem format, so I can get the String of just load them.

public.pem ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbG4edEk+29WzzDvbDw+uEMPCSJC1AJLeMpsQujeBomGvxdLnTOpjnd31OyxNr9OFhP2s3w9XkSOotBwPtz5QdwHo7Jb1dPV2iA0vM5FwY45fa7JlzKDo9E5MK/meoNRa2udG3+w4es890omKov8BGrUVHDF8NldQ8ASunUY7rCZxW/iBz8tcMJ7TlQtEZSOK00oGt03C6kna3mQWR3Ble5GwKg8olJ9CY2+km9toIZemBUMx6UIIg8ErOHJ6cnLdQWhIKiIAtSkqd6feLi3QbeHgD6Nx5J5PmHKT5BnTz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eGQ3quts6+XWiruAdgrxYw+APjiEmavbAY5cxgsqC9YMeC/kP5DzNSvtn8X2+zcdcdiHsl0NAneyZ+//CFdp40FNmoo4mqPNAVsRZ7F0DUVRFic5LxRzQcejcRAguiVOa0xCYCMWFBAAAAgBIJ6z8A3o0DXFKDXd36avaVtAuAlQ/4u/9CmQs/ctZUM4F54B4SlSgBemXIMn3W9doxL3cgXHosIsxkiEeNtkaT3DxE0N+M4dlaf0H7IXRZIlc2h+vk66KSY/guSFfTWdfsjVGAcW96NEAOcysDocGMW0Di1/05GWmdgkh1Ev3IAAAAgQDtd8TOL1+Y5mEykle0UnkfW4ualpV7N8gGE1BjXqoiiIRX/Y0ekp4TPGLn7J3PsVkbMajA2CSgEYt2o4BM7wsGY1NvGQQ66wq2D8m0V8ZmG0cnMGlY3LLzTJoYl76ulISGZSw110lSxQ2iCynsqPg2OP9MKOofYgtfXmXbDXDkAwAAAIEA7DTze0EdaJYKvvqwycvVdN5WZceAlSflTpPlFoz/uzxN9aDip1KCucMK8ZzRT7Peiv2IVobgEe3lzoG7fjyvrH2/PaochmIcnneJOZoXmU7zxyFZakMeA/RvhutnYGp/fxt1IE79dDIbaGJdAFAJryJSNg+TnbCXN74LRI+bmNEAAAAWaWFubWFnYXJ6b0BtZWdhLW1hZ2ZlcgECAwQF

Here the key files

The thing is that I'm capable to create the publicKey instance but not the privateKey

let publicKey = try PublicKey(pemNamed: "Keys/public") ✅
let privateKey = try PrivateKey(pemNamed: "Keys/private") ❌

I also tried to convert the strings to base64 and use the object initialiser for this unsuccessfuly

PrivateKey(base64Encoded: privateKeyStringBase64)

Any idea of what I'm making wrong? Did I forgot any step?

MagFer avatar Jan 22 '19 18:01 MagFer

I am also running into this same problem. @MagFer Did you find any solution to this? When I try to catch the error I am getting the following error asn1ParsingFailed

prakashRavi1990 avatar Oct 01 '19 07:10 prakashRavi1990

@MagFer @prakashRavi1990 ssh-kengen has its own private key format , you should create keypair with openssl, more info on here and here

wzio avatar May 28 '20 06:05 wzio