Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RS256 failed when encoding #242

Open
3 tasks done
sambo-nbc opened this issue Jan 6, 2022 · 3 comments
Open
3 tasks done

RS256 failed when encoding #242

sambo-nbc opened this issue Jan 6, 2022 · 3 comments

Comments

@sambo-nbc
Copy link

sambo-nbc commented Jan 6, 2022

New Issue Checklist

Issue Info

Info Value
Platform Name e.g. ios
Platform Version e.g. 12.0.1
CocoaLumberjack Version e.g. 2.3.0
Integration Method cocoapods
Xcode Version e.g. Xcode 13.1
Repro rate e.g. all the time (100%) / sometimes x% / only once
Demo project link

Issue Description and Steps

Encode with RS256 using string data of a private key, but I got error
Error Domain=NSOSStatusErrorDomain Code=-50 "RSA private key creation from data failed" UserInfo={numberOfErrorsDeep=0, NSDescription=RSA private key creation from data failed}

Here is code sample:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"key"];
    NSString *key = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    NSDictionary *payload = @{@"payload" : @"hidden_information"};

    
    id <JWTAlgorithmDataHolderProtocol> signDataHolder = [JWTAlgorithmRSFamilyDataHolder new]
            .keyExtractorType([JWTCryptoKeyExtractor privateKeyWithPEMBase64].type)
            .privateKeyCertificatePassphrase(nil)
            .algorithmName(JWTAlgorithmNameRS256).secret(key);

    JWTCodingResultType *result = [JWTEncodingBuilder encodePayload:payload]
        .addHolder(signDataHolder)
        .result;

    NSString *token = result.successResult.encoded;
    NSError *error = result.errorResult.error;

    NSLog(@"%@", token);
    NSLog(@"%@", error);```

Private key: 

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtW3q34xSvWw/v0icR5B4SX9BxBOt7oiY7TZSVVAhOQE5juSM
dTH0rzrVKRIk3AC602WD4wAi2x/vt2zGCLgzlhdUbqohXTX2BAlQ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DKezfrL/sxSpnlFNMLTx88HOewD/SAOa4GbHWHIrf0IN
60qZP+Oj/a+86xQm37hq4QKBgHyJ0IxLjn5HIsE1LzWAhRWYj2XEm3CtFzHFhF+H
E0ZJGl3JVlqJURHEQTWq0M5ogKphh6j/bA0GVS72EQIZtm8vZpD5Bxt99QkIt3VJ
wRJQp5hMoQ6Toti6iRPE4ughaCUBKMHOHCzu0ErFLPpBJuNwQUXtXEmxl0adJZCZ
G6xrAoGBANMEeeNMy37InmFXZtWI4V/Vo5jCva4YkgDCWbemmXV6nttACKk2WGiY
uuXiBGdcshk0gb5Lb96VAgUV9CwAiP319C3D/fL2At8hxpR9tIOXLT/K+yhuMiSj
8DbsRTtONC4MgrJUskm1BEtocoKdKk/ipN6BQ9lwzQDq+43uQjHJ
-----END RSA PRIVATE KEY-----

Could anyone here help me regard this issue, please? 🙏 
@lolgear
Copy link
Collaborator

lolgear commented Jan 6, 2022

Are you sure that this is a valid RSA key?

OpenSSL can't extract public key from it.

openssl rsa -in private.pem -pubout > public.pem

@sambo-nbc
Copy link
Author

@lolgear
I generate RSA keypair with command below:
openssl req -newkey rsa:2048 -nodes -keyout private.key -x509 -days 365 -out public.crt

@lolgear
Copy link
Collaborator

lolgear commented Jan 12, 2022

@sambo-nbc
Try to regenerate key and extract its public part.

openssl req -newkey rsa:2048 -nodes -keyout private.key -x509 -days 365 -out public.crt
openssl rsa -in private.pem -pubout > public.pem

It should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants