Recently, we have a project that need to do RSA 1024 encryption in IOS. We use this helpful library SSCrypto framework to do RSA encryption.
With this library and with private key, you can encrypt the key by doing this. The private will then generate the public key to use to RSA encryption.
NSData *privateKeyData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rsaprivatekey" ofType:@"pem"]];
// generate a public key from the private key data
NSData *publicKeyData = [SSCrypto generateRSAPublicKeyFromPrivateKey:privateKeyData];
SSCrypto *rsa = [[SSCrypto alloc] initWithPublicKey:publicKeyData privateKey:privateKeyData];
// set input that you want to do RSA encryption
[rsa setClearTextWithString:input];
NSData *dataEncrypt = [rsa encrypt];
//return final encrypted RSA 1024 password
NSString *rsaPassword = [NSString stringWithFormat:@"%@", [dataEncrypt hexval]];
Problem with our project is that we doesn't have a private Key! Based on the security diagram, private key should stored in server side and only give out public key.
So we need to decrypt the public key from server. The server is writing in JAVA, and they NSDATA as bytes Array. What we are going to do is to translate the byte Array in JAVA to IOS.
1) First, I combined all the bytes into a array format in char value
char rsaKeys[[publicKeys count]];
for(int i=0; i<[publicKeys count];i++){
rsaKeys[i] = [(NSString *)[publicKeys objectAtIndex:i] intValue];
}
2) By doing this, you are combining the byte Array into the format like this.
char rsaKeys[] = {48, -127, -97, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 3, -127, ....}
3) Use this method to encode it into based 64, Please becareful you must use encodeBase64, if not the whole thing will not work.
[[NSData dataWithBytes:rsaKeys length:[publicKeys count]] encodeBase64]
4) This will help you to generate something like this:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmYQ+xuFlZSvt/9qiTClnEBro/
2i1WsAhozr99wreVxzWy1HPlIrjUsOS8oNeF4Nw3CQ/TpPkEFzxGEwqa7LypDB5U
cD8OghTaXp/xQqcyg2LqmhQXha6LMiMWhPL/7vR56p7yoGWm4zaNJm9mNyIFVeLk
cel/VNU24B+C4m9oYwIDAQAB
5) the pem file usually start with header and footer, you also need to add this two line.
Header: -----BEGIN PUBLIC KEY-----
Footer: -----END PUBLIC KEY-----
After append this two file, you should get a completed public key PEM file.
6) with the public key PEM file, you can do RSA1024 with just the public key.
NSData *key1 = [pemkey dataUsingEncoding:NSASCIIStringEncoding];
SSCrypto *rsa = [[SSCrypto alloc] initWithPublicKey:key1];
7) and you should able to use rsa1024 to encrypt:
[rsa setClearTextWithString:input];
NSData *dataEncrypt = [rsa encrypt];
The full code is as below:
char rsaKeys[[publicKeys count]];
for(int i=0; i<[publicKeys count];i++){
rsaKeys[i] = [(NSString *)[publicKeys objectAtIndex:i] intValue];
}
NSString *pemkey = [NSString stringWithFormat:@"%@\n%@%@\n",@"-----BEGIN PUBLIC KEY-----", [[NSData dataWithBytes:rsaKeys length:[publicKeys count]] encodeBase64], @"-----END PUBLIC KEY-----"];
//Convert to NSData
NSData *key1 = [pemkey dataUsingEncoding:NSASCIIStringEncoding];
SSCrypto *rsa = [[SSCrypto alloc] initWithPublicKey:key1];
[rsa setClearTextWithString:input];
NSData *dataEncrypt = [rsa encrypt];
//return final encrypted RSA 1024 password
NSString *rsaPassword = [NSString stringWithFormat:@"%@", [dataEncrypt hexval]];
By using the example above, you should able to do RSA1024 with just public key in IOS.