title: Convert NSString to SecKeyRef to verify RSA in objective-C
body: <p>I want to verify a data on iOS 7. Now I have a function: </p>
  
  <pre><code>-(BOOL) PKCSVerifyBytesSHA256withRSA:(NSData*) plainData
                              withSign:(NSData*) signature
                                  withKey:(SecKeyRef) public_Key{
          size_t signedHashBytesSize = SecKeyGetBlockSize(public_Key);
          const void* signedHashBytes = [signature bytes];
          size_t hashBytesSize = CC_SHA256_DIGEST_LENGTH;
          uint8_t* hashBytes = malloc(hashBytesSize);
          if (!CC_SHA256([plainData bytes], (CC_LONG)[plainData length], hashBytes)){
              return nil;
          }
          OSStatus status = SecKeyRawVerify(publicKey,
                                            kSecPaddingPKCS1SHA256,
                                            hashBytes,
                                            hashBytesSize,
                                            signedHashBytes,
                                            signedHashBytesSize);
          return status == errSecSuccess;
  }
  </code></pre>
  
  <p>and a key: </p>
  
  <pre><code>NSString *public_key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCT2eIq8i/uAHzvwOWhFU9AuRHjPnGq8wD461ZH7N4/LjoRwPaPF3meyyENJgtDr4EyhV9KN77/3VT+87ZpT6QH9w6Q5XwDmM3jhU6bUhWyIPLzrd5XE2rQKRIMXixflz/8Q327VHKsLoKu44HuCh6XrB+uMWUjwVLCLOi2U0sYdQIDAQAB"; 
  </code></pre>
  
  <p>I was suggest a link <a href="https://stackoverflow.com/questions/24612507/nsstring-to-seckeyref">here</a>
  so I add the <code>- (SecKeyRef)getPublicKeyRef</code> and <code>PKCSVerifyBytesSHA256withRSA</code> to a new class and call  </p>
  
  <pre>RSA1 *rsa1 =[[RSA1 alloc]init];
      if([rsa1 PKCSVerifyBytesSHA256withRSA:Ddata withSign:Dsig withKey:[rsa1 getPublicKeyRef]]){
          NSLog(@"True");}
      else{
          NSLog(@"nil");}</pre>
  
  <p>but I got an error at the line 
  <pre>size_t signedHashBytesSize = SecKeyGetBlockSize(public_Key);</pre> in the <code>PKCSVerifyBytesSHA256withRSA</code> function with the note: <code>Thread 1: EXC_BAD_ACCESS(code=1, address=0x10)</code>
  Is there any way to solve this problems? </p>