hohl / MIHCrypto

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

How do I initialize MIHRSAPublicKey using the public key string returned by the server? #53

Closed Kejiasir closed 6 years ago

Kejiasir commented 6 years ago

@hohl hey! my server returns a public key string that needs to be decrypted through it, but it crashes when it is initialized by the following method

// The public key string returned by the Java server                
// MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsD2JQlxPCHm1+g1faPDKAdf9bOL4DmeO2YRdT/UCAGESXortYb7TyPtUI9s3gQZG3QsgP1Op59OlVTqrIbeUJ0rHl1q3As7Tm064IuaVL2ocvIkIW2Dx5bSqA2wKwvoRs7O0cAFxspDA8xZesp8DRNZ28LQdF/s8+TMQYecOcGQIDAQAB

NSString *webPublicKey = [self getTokenInfo].rsaPublicKey;
NSData *publicData = [webPublicKey dataUsingEncoding:NSUTF8StringEncoding];
MIHRSAPublicKey *rsaPublicKey = [[MIHRSAPublicKey alloc] initWithData:publicData];

qq20170914-100354

Hope to get your help, be grateful, wish you a happy life

Kejiasir commented 6 years ago

Oh, I found a solution, thank you

hohl commented 6 years ago

initWithData expects the binary data not a BASE64 encoded string of the data. But there is a helper included NSData *publicKeyData = [yourPublicKeyString MIH_dataByBase64DecodingString]. However, you don't even need that helper anymore since In more recent iOS and macOS versions a base64 encoding helper is included on the NSString class already.