hohl / MIHCrypto

OpenSSL wrapper for Objective-C [cryptography]
MIT License
340 stars 68 forks source link

Get RSA public key string #20

Closed yuval-netanel closed 9 years ago

yuval-netanel commented 9 years ago

I want to send the public key to the server. This is how I tried to get the public key string, but it returns null:

 MIHRSAKeyFactory *factory = [[MIHRSAKeyFactory alloc] init];
 factory.preferedKeySize = MIHRSAKey512;
 MIHKeyPair *keyPair = [factory generateKeyPair];

 NSString *publicKeyStr = [keyPair.public description];
 NSLog(@"%@", publicKeyStr);// logs null

 NSData *data = [keyPair.public dataValue];
 publicKeyStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 NSLog(@"%@", publicKeyStr);// logs null

Any suggestions?

hohl commented 9 years ago

Thats quite strange. Your code snippet seems okay. Any functionality of MIHRSAKeyFactory should become automatically tested in MIHRSAKeyFactoryTests, so there shouldn't be any issues with it (since all tests run without problems, tested locally on my machine and on Travis continuos integration server).

I'm sorry, but I guess I can't help you.

askxlogic commented 9 years ago

@hohl I have same issue. I found that reason of it is wrong encoding of NSData that return method

[keyPair.public dataValue];

I think that this issue and this issue is same.

hohl commented 9 years ago

You mean, using NSDataBase64DecodingIgnoreUnknownCharacters fixes this issue?

askxlogic commented 9 years ago

For fixing this issue change method

- (NSString *)description
{
    return [[NSString alloc] initWithData:[self dataValue] encoding:NSUTF8StringEncoding];
}

to

- (NSString *)description
{
    return [[self dataValue] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}

in MIHRSAPublicKey.m

askxlogic commented 9 years ago

I am sorry. It just looks like similar bugs. Everything working for me without NSDataBase64DecodingIgnoreUnknownCharacters

hohl commented 9 years ago

description is for debugging anyway. However I thought this issue is about not being able to generate public keys? If it's about the description output I may need to have second look at it.

yuval-netanel commented 9 years ago

Sorry if it wasn't clear, but it's about the description output.

hohl commented 9 years ago

Oh dear. Since the description field has been added for testing purposes I've forgotten about the unit tests for, I'm sorry.

I'll fix it immeditley, this should do the job:

- (NSString *)description
{
    return [[self dataValue] MIH_base64EncodedString];
}