使用RSA只使用模数和指数在iOS中encryptionstring

我知道这里有很多类似的问题和答案,但我已经search,找不到适合我的一个。

我有一个服务,我想在IOS(6)中使用,由我无法控制的第三方提供。

为了使用服务进行身份validation,我需要将我的用户凭据作为RSAencryptionstring发送,并使用RSA公钥进行encryption。

他们为我提供了一个XML格式的文件

<BitStrength>1024</BitStrength> <RSAKeyValue> <Modulus>xxxxxxxxxxxxxxxxxxxxx</Modulus> <Exponent>xxxx</Exponent> </RSAKeyValue> 

我需要做什么才能encryptionstring? 我是来自DOTNET的背景,所以到目前为止,我的大部分复杂性都被模糊了。

我已经尝试过这样的例子: Objective-C中的RSA实现,但没有办法从我拥有的对象中构build对象,他们似乎需要一个证书

我曾尝试使用此工具将其转换为PEM文件,但代码将不会生成证书对象。 https://superdry.apphb.com/tools/online-rsa-key-converter

在此先感谢您的帮助。

****编辑****这是我使用提供的示例创build的方法的一部分,它运行没有错误,但我不能解码输出:

  SStatus status = noErr; size_t cipherBufferSize; uint8_t *cipherBuffer; // [cipherBufferSize] size_t dataSize = [plainTextString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; const uint8_t* textData = [[plainTextString dataUsingEncoding:NSUTF8StringEncoding] bytes]; NSAssert(publicKey, @"The public key being referenced by tag must have been stored in the keychain before attempting to encrypt data using it!"); // Allocate a buffer cipherBufferSize = SecKeyGetBlockSize(publicKey); // plain text block size must be 11 less than cipher buffer size because of // the PKSC1 padding used: const size_t blockSizeMinusPadding = cipherBufferSize - 11; cipherBuffer = malloc(cipherBufferSize); NSMutableData* accumulatedEncryptedData = [NSMutableData dataWithCapacity:0]; for (int ii = 0; ii*blockSizeMinusPadding < dataSize; ii++) { const uint8_t* dataToEncrypt = (textData+(ii*blockSizeMinusPadding)); const size_t subsize = (((ii+1)*blockSizeMinusPadding) > dataSize) ? blockSizeMinusPadding-(((ii+1)*blockSizeMinusPadding) - dataSize) : blockSizeMinusPadding; // Encrypt using the public key. status = SecKeyEncrypt(publicKey, kSecPaddingOAEP, dataToEncrypt, subsize, cipherBuffer, &cipherBufferSize ); [accumulatedEncryptedData appendBytes:cipherBuffer length:cipherBufferSize]; } if (publicKey) CFRelease(publicKey); free(cipherBuffer); 

//返回cumulativeEncryptedData; 返回[cumulativeEncryptedData base64EncodedString];

使用您提到的转换器获取公钥的PEMstring,或者编写一些代码来自己完成转换。

编辑:对不起,我粘贴错误的链接。 现在改变它:然后,抓住在这里find的代码,并使用它将公钥添加到iOS钥匙串。

您可以使用以下代码从钥匙串获取公钥参考:

 + (SecKeyRef)copyPublicKeyForTag:(NSString*)tag { SecKeyRef publicKey = NULL; NSData * publicTag = [NSData dataWithBytes:[tag cStringUsingEncoding:NSUTF8StringEncoding] length:[tag length]]; NSMutableDictionary *queryPublicKey = [[NSMutableDictionary alloc] init]; [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass]; [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag]; [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType]; [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef]; OSStatus status = SecItemCopyMatching ((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKey); if (status != noErr) { return nil; } return publicKey; } 

现在您可以使用我写的代码来使用公钥在这里findencryption数据。 请注意,我正在使用PKCS1填充,您可能不会。 如果你是,那么取决于你想让你的代码适用于iOS 5还是仅适用于iOS 6,我刚刚链接的博客文章中的其他信息将是相关的。

最后,我们使用http://chilkatsoft.com/rsa-objc.asp,因为它是完全无缝的,而且在价格上我花了更多的钱试图让iOS在本地实现&#x3002;

它将采取模数和指数部分和或一个证书。 当我在IOS上encryption时,Mathews代码似乎工作,但从来没有成功解密服务。