仅使用iOS中的模数和指数使用RSA加密字符串

时间:2013-01-30 15:31:38

标签: ios rsa xcode4.5

我知道这里有很多问题和答案,但我搜索过,找不到适合我的问题。

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

为了通过服务进行身份验证,我需要将用户凭据作为RSA加密字符串发送,并使用其RSA公钥进行加密。

他们为我提供了一个格式为

的XML文件
<BitStrength>1024</BitStrength>
<RSAKeyValue>
<Modulus>xxxxxxxxxxxxxxxxxxxxx</Modulus>
<Exponent>xxxx</Exponent>
</RSAKeyValue>

为了加密字符串,我需要做什么?我来自DOTNET背景,所以到目前为止我的大部分复杂性都被模糊了。

我尝试过这样的例子: RSA implementations in Objective C 但是没有办法从我拥有的东西构建对象,他们似乎需要一个证书

我尝试使用此工具将其转换为PEM文件,但代码将不再构建cert对象。 https://superdry.apphb.com/tools/online-rsa-key-converter

提前感谢您的帮助。

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

   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);

// return cumulativeEncryptedData;  return [cumulativeEncryptedData base64EncodedString];

2 个答案:

答案 0 :(得分:1)

使用您提到的转换器获取公钥的PEM字符串,或者自己编写一些代码来进行转换。

编辑:抱歉,我粘贴了错误的链接。现在改了: 然后,抓取找到的代码here并使用它将公钥添加到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;
}

现在,您可以使用我写的代码使用公钥here加密数据。请注意,我使用PKCS1填充,您可能不会。如果您是,那么根据您是希望您的代码适用于iOS 5还是仅适用于iOS 6,我刚刚链接到的博客文章中的其他信息是否相关。

答案 1 :(得分:0)

最后我们选择了http://chilkatsoft.com/rsa-objc.asp,因为它是完全无缝的,而且为了价格,我正在花更多的钱试图让iOS本地化。

它将采用模数和指数部分和/或证书。当我在IOS上加密但是从未让它成功解密服务时,Mathews代码确实有效。