使用 MIHCrypto 解密 RSA(iOS 上的 OpenSSL-Universal)

Decrypting RSA with MIHCrypto (OpenSSL-Universal on iOS)

您好,我遇到了使用 MIHCrypto v0.3.2 解密的问题。这些是我的代码行:

NSString *encrypted_text = @"BdhFH0sd7e9DExiCd50Ykh4spm2BX126skjJ1o8HHjKsN+J7r9IoI9kbB9AAacEpJsAfyesiJsq5gDBhQtcNbB6l88aSgPrEoVwR9ilzuzVcv1q3J1dxs4uIEMuhzoWT+R8//dD2jDdXPyFsdGWJc10CEizPFKpmy2jWhvU8CVs=";
NSBundle *myBundle = [NSBundle mainBundle];
NSString *privateKeyPath= [myBundle pathForResource:@"rsa_1024_priv" ofType:@"pem"];    
NSData *privateKeyData = [[NSFileManager defaultManager] contentsAtPath:privateKeyPath];
MIHRSAPrivateKey *privateKey = [[MIHRSAPrivateKey alloc] initWithData:privateKeyData];
NSError *decryptionError = nil;

// decryption
NSData *encData = [encrypted_text dataUsingEncoding:NSUTF8StringEncoding];
NSData *decryptedEncData = [privateKey decrypt:encData error:&decryptionError];
NSString* decryptedText = [[NSString alloc] initWithData:decryptedEncData encoding:NSUTF8StringEncoding]; // iOS 7+, by iOS Core API

if(decryptionError){
    DDLogDebug(@"error: %@",[encryptionError localizedDescription]);
}
DDLogDebug(@"decrypted: %@",decryptedEncData);

问题调试到这里:

错误:OpenSLL 内部错误! (Code=67522668,Description=error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data 大于 mod len)

你有什么想法吗?

我终于找到了解决办法:

使用更短的数据块!

背景(由 Hohl 发布 - here):

Using RSA with large blocks of data seems to be a common issue. Some wrappers handle this by splitting the data into smaller blocks and encrypting every block separately. But since RSA isn't intended to encrypt large blocks of data this won't be implemented in this wrapper. (Better combine RSA with something like AES if you need features of both worlds.)