ret = SecItemAdd((__bridge CFDictionaryRef) publicKeyDict, (CFTypeRef*)&pubKeyRef);在iOS8系统下下面的上面这句代码没问题,ret = 0,pubKeyRef的值是:
po pubKeyRef
<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 3, block size: 1024 bits, exponent: {hex: 10001, decimal: 65537}, modulus: B5CE747C46781C81488F169C72828B72233E6E3B70525D2CE088665EC1B61F3F5FC7FE96CFB8BFFA699D61D0316ACC8C021A03ABF703C75510990F95008E4A3303ACF424B3E7EFEA001D0499CE00EB293A79B2054D11852E66D81EABF9B714A0013611059810C4CD670B50AC5D2E6B743049A5297AD38690C25BB3BBF95A331D, addr: 0x15e0baa00>在ios9下,这句返回值ret = 0,但是pubKeyRef得到的值为空。
具体的代码在下面。
跪求大神+(NSData*)rsaEncrypt:(NSData*)publicKey  rawData:(NSData*)rawData
{
    SecKeyRef pubKeyRef = NULL;
    
    CFTypeRef pubKeyRefTmp = NULL;
    OSStatus ret;
    
    //这里的公钥部分的数据是按BER格式保存,module在前,exponent部分在后,其具体的格式为:
    // 序列类型 | 2部分的总长度 |[ [数值类型 | module的长度 | module的内容] | [数值类型 | exponent的长度 | exponent的内容] ]
    NSData *publicKeyData = publicKey;
    
    
    NSData *publicKeyTag = [@"xxxxxxxx " dataUsingEncoding:NSUTF8StringEncoding];
    
    
    NSMutableDictionary *publicKeyDict = [[NSMutableDictionary alloc] init];
    [publicKeyDict setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
    [publicKeyDict setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
    [publicKeyDict setObject:publicKeyTag forKey:(__bridge id)kSecAttrApplicationTag];
    
    
    //从钥匙串中删除已经存在的密码
    ret = SecItemDelete((__bridge CFDictionaryRef)publicKeyDict);
    
    //将密码添加入钥匙串中。
    [publicKeyDict setObject:publicKeyData forKey:(__bridge id)kSecValueData];
    [publicKeyDict setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)kSecAttrKeyClass];
    [publicKeyDict setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
    
    ret = SecItemAdd((__bridge CFDictionaryRef) publicKeyDict, (CFTypeRef*)&pubKeyRef);
    
    if (ret != 0 || pubKeyRef == NULL)
    {
        return nil;
    }
    
    
    //用生成的公钥加密数据,RSA加密算法对要加密的数字进行分块处理,每一块的长度 = 公钥长度 - 11(这里是对其字节数)
    //而对空出的11个字节采用了随机数的填充方法,所以每次用公钥加密出来的密文都是不一样的。
    NSMutableData *encryptedData = [[NSMutableData alloc] init];
    
    size_t cipherBufferSize = SecKeyGetBlockSize(pubKeyRef);
    uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
    memset((void *)cipherBuffer, 0x0, cipherBufferSize);
    
    
    NSData *plainTextBytes = rawData;
    int blockSize = __ISIOS5 ?  cipherBufferSize - 12 : cipherBufferSize -11;
    int numBlock = (int)ceil([plainTextBytes length] / (double)blockSize);
    for (int i=0; i<numBlock; i++) {
        
        int bufferSize = MIN(blockSize, [plainTextBytes length] - i * blockSize);
        NSData *buffer = [plainTextBytes subdataWithRange:NSMakeRange(i * blockSize, bufferSize)];
        OSStatus status = SecKeyEncrypt(pubKeyRef, kSecPaddingPKCS1,
                                        (const uint8_t *)[buffer bytes],
                                        [buffer length],
                                        cipherBuffer,
                                        &cipherBufferSize);
        
        if (status == noErr)
        {
            [encryptedData appendBytes:cipherBuffer length:cipherBufferSize];
            
        }
        else
        {
            break;
        }
        
    }
    
    if (cipherBuffer) {
        free(cipherBuffer);
    }
    
    CFRelease(pubKeyRef);
    
    return encryptedData;}

解决方案 »

  1.   

    有没有大神帮忙看看呀。或者是否有其他的方法把已经获得的NSData类型的公钥转换成要用的SecKeyRef类型?
      

  2.   

    大神们 你们解决了吗 ? RSA加密 我在iOS9真机 也出现问题了 模拟器没问题的 
      

  3.   

    在网上找了那些方法 在iOS9上加密都有问题
      

  4.   

    目前有一个解决方案,你看看ok不?
    http://blog.csdn.net/suusatoshigi/article/details/49535391
      

  5.   

    http://blog.csdn.net/suusatoshigi/article/details/49535391
      

  6.   

    我写了blog投机取巧地解决了这个问题,如果觉得有用的话,点个赞吧~
    http://blog.csdn.net/suusatoshigi/article/details/49535391
      

  7.   

    看看我找的这个方法能够帮到你们吗?
    http://blog.csdn.net/ylgwhyh/article/details/49756209
      

  8.   

    通过这个方法解决了
    http://www.jianshu.com/p/f43fc057fba4