Hi,
I have to exchange keys with the server (in JAVA using bouncycastle) and my iOS app. So the server has the key size issue and send me the key in blocks (each one encrypted with my public key). The problem is that when I want to decrypt one of these blocks I get the error
"Size of data to encrypt must not exceed size of RSA key. ...."
But I don't really understand why because each block is smaller that the size of my key. Here is my code:
MIHRSAKeyFactory* factory = [[MIHRSAKeyFactory alloc] init];
factory.preferedKeySize = MIHRSAKey1024;
MIHKeyPair *keyPair = [factory generateKeyPair];
clientPrivateKey = keyPair.private;
clientPublicKey = keyPair.public;
...
[self postOperation:@"exchangekey" params:params onCompletion:^(NSDictionary *json)
{
NSError *error = nil;
NSString* dataStr = [[[json objectForKey:@"chains"] objectAtIndex:0] objectForKey:@"chain"];
NSData* data2 = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
[self log:[NSString stringWithFormat:@"Server response (%ld): %@",data2.length,data2]];
//NSData* data = [NSData dataWithData:[[[json objectForKey:@"chains"] objectAtIndex:0] objectForKey:@"chain"]];
[clientPrivateKey decrypt:data2 error:&error];
[self log:[NSString stringWithFormat:@"Decript error: %@",error.description]];
}];
And the log shows this
Server response (256): <37653734 38363964 65386134 36663930 66656530 35356230 65393937 64626432 66343032 65333061 35373437 62626361 34316133 61643262 39373037 36353230 65343765 66656266 33666433 63323537 62303634 39613461 30613034 37666161 32303862 34313331 30353239 30323163 35303966 39333833 30383332 64366662 65323433 33623862 33326532 61616330 62613239 63653161 33356230 39373831 65316430 33373961 33623838 30633532 37393732 39663235 63363332 34653732 32333031 64326662 35366264 33633835 61323533 37363235 35613836 30613863 65396533 33633835 61363330 35666333 33386263 63383962 30636632 30336535>
Decript error: Error Domain=MIHOpenSSLErrorDomain Code=67522668 "Size of data to encrypt must not exceed size of RSA key. If you want to securly encrypt large blocks of data combine RSA with AES. (See #24 for more details about that topic.)" UserInfo=0x7fb4edb8f390 {NSLocalizedDescription=Size of data to encrypt must not exceed size of RSA key. If you want to securly encrypt large blocks of data combine RSA with AES. (See #24 for more details about that topic.)}
Any ideas on what I'm doing wrong?
Is my first try with this lib and this kind of encryption so apologies in advance if this is a simple newbie error.
Hi,
I have to exchange keys with the server (in JAVA using bouncycastle) and my iOS app. So the server has the key size issue and send me the key in blocks (each one encrypted with my public key). The problem is that when I want to decrypt one of these blocks I get the error
"Size of data to encrypt must not exceed size of RSA key. ...."
But I don't really understand why because each block is smaller that the size of my key. Here is my code:
And the log shows this
Server response (256): <37653734 38363964 65386134 36663930 66656530 35356230 65393937 64626432 66343032 65333061 35373437 62626361 34316133 61643262 39373037 36353230 65343765 66656266 33666433 63323537 62303634 39613461 30613034 37666161 32303862 34313331 30353239 30323163 35303966 39333833 30383332 64366662 65323433 33623862 33326532 61616330 62613239 63653161 33356230 39373831 65316430 33373961 33623838 30633532 37393732 39663235 63363332 34653732 32333031 64326662 35366264 33633835 61323533 37363235 35613836 30613863 65396533 33633835 61363330 35666333 33386263 63383962 30636632 30336535>
Decript error: Error Domain=MIHOpenSSLErrorDomain Code=67522668 "Size of data to encrypt must not exceed size of RSA key. If you want to securly encrypt large blocks of data combine RSA with AES. (See #24 for more details about that topic.)" UserInfo=0x7fb4edb8f390 {NSLocalizedDescription=Size of data to encrypt must not exceed size of RSA key. If you want to securly encrypt large blocks of data combine RSA with AES. (See #24 for more details about that topic.)}
Any ideas on what I'm doing wrong?
Is my first try with this lib and this kind of encryption so apologies in advance if this is a simple newbie error.