iphone - CryptoExcercise 加密/解密问题

标签 iphone encryption

我在我的应用程序中使用苹果“cryptoexcercise”(Security.Framework)来加密和解密数值数据。当我输入 950,128 时,值被加密,但它没有被解密,并且仅以加密值存在。这种情况仅发生在提到的数值上。您能检查一下这个问题并给出解决这个问题的方案吗?

这是我的代码

  (void)testAsymmetricEncryptionAndDecryption {
  uint8_t *plainBuffer; uint8_t *cipherBuffer; uint8_t *decryptedBuffer;

  const char inputString[] = "950"; int len = strlen(inputString);

  if (len > BUFFER_SIZE) len = BUFFER_SIZE-1;

  plainBuffer = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t)); cipherBuffer = (uint8_t *)calloc(CIPHER_BUFFER_SIZE, sizeof(uint8_t)); decryptedBuffer = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t));

  strncpy( (char *)plainBuffer, inputString, len);

  NSLog(@"plain text : %s", plainBuffer);

  [self encryptWithPublicKey:(UInt8 *)plainBuffer cipherBuffer:cipherBuffer];

  NSLog(@"encrypted data: %s", cipherBuffer);

  [self decryptWithPrivateKey:cipherBuffer plainBuffer:decryptedBuffer];

  NSLog(@"decrypted data: %s", decryptedBuffer);

  free(plainBuffer); free(cipherBuffer); free(decryptedBuffer); }


  (void)encryptWithPublicKey:(uint8_t *)plainBuffer cipherBuffer:(uint8_t *)cipherBuffer {
  OSStatus status = noErr;

  size_t plainBufferSize = strlen((char *)plainBuffer); size_t cipherBufferSize = CIPHER_BUFFER_SIZE;

  NSLog(@"SecKeyGetBlockSize() public = %d", SecKeyGetBlockSize([self getPublicKeyRef])); // Error handling // Encrypt using the public. status = SecKeyEncrypt([self getPublicKeyRef], PADDING, plainBuffer, plainBufferSize, &cipherBuffer[0], &cipherBufferSize ); NSLog(@"encryption result code: %d (size: %d)", status, cipherBufferSize); NSLog(@"encrypted text: %s", cipherBuffer); }


  (void)decryptWithPrivateKey:(uint8_t *)cipherBuffer plainBuffer:(uint8_t *)plainBuffer { OSStatus status = noErr;

  size_t cipherBufferSize = strlen((char *)cipherBuffer);

  NSLog(@"decryptWithPrivateKey: length of buffer: %d", BUFFER_SIZE); NSLog(@"decryptWithPrivateKey: length of input: %d", cipherBufferSize);

  // DECRYPTION size_t plainBufferSize = BUFFER_SIZE;

  // Error handling status = SecKeyDecrypt([self getPrivateKeyRef], PADDING, &cipherBuffer[0], cipherBufferSize, &plainBuffer[0], &plainBufferSize ); NSLog(@"decryption result code: %d (size: %d)", status, plainBufferSize); NSLog(@"FINAL decrypted text: %s", plainBuffer);

}

  (SecKeyRef)getPublicKeyRef { OSStatus sanityCheck = noErr; SecKeyRef publicKeyReference = NULL;

  if (publicKeyRef == NULL) { NSMutableDictionary *queryPublicKey = [[NSMutableDictionary alloc] init];

  // Set the public key query dictionary.
  [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];


  // Get the key.
  sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);


  if (sanityCheck != noErr)
  {
      publicKeyReference = NULL;
  }


  [queryPublicKey release];

  } else { publicKeyReference = publicKeyRef; }

  return publicKeyReference; }


  (SecKeyRef)getPrivateKeyRef { OSStatus resultCode = noErr; SecKeyRef privateKeyReference = NULL;

  if(privateKeyRef == NULL) { NSMutableDictionary * queryPrivateKey = [[NSMutableDictionary alloc] init];

  // Set the private key query dictionary.
  [queryPrivateKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
  [queryPrivateKey setObject:privateTag forKey:(id)kSecAttrApplicationTag];
  [queryPrivateKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
  [queryPrivateKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];


  // Get the key.
  resultCode = SecItemCopyMatching((CFDictionaryRef)queryPrivateKey, (CFTypeRef *)&privateKeyReference);


  NSLog(@"getPrivateKey: result code: %d", resultCode);


  if(resultCode != noErr)
  {
      privateKeyReference = NULL;
  }


  [queryPrivateKey release];

  } else { privateKeyReference = privateKeyRef; }

  return privateKeyReference; }

最佳答案

我很抱歉回复晚了...但是你的例子对我来说非常有效,除了事实之外,

1) 对于 privateTag 和 publicTag 我必须声明

NSData *privateTag = [NSData dataWithBytes:privateKeyIdentifier length:strlen((const char *)privateKeyIdentifier)]; 

NSData *publicTag = [NSData dataWithBytes:publicKeyIdentifier length:strlen((const char *)privateKeyIdentifier)]; 

2) 并在 key 引用 IF 条件中更改 privateKey == NULL 而不是 privateKeyRef ==NULL ..

关于iphone - CryptoExcercise 加密/解密问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2824402/

相关文章:

iphone - 像在 iPhone SMS 应用程序中一样实现添加联系人

python - 如何在 Python 中进行 PGP(生成 key 、加密/解密)

ruby-on-rails - RoR 教程,第 7 章 - rspec 测试失败 : NoMethodError 'encrypt'

python-3.x - "PDF File has not been decrypted"问题在 PyPDF2 中仍然存在

c++ - 从文件末尾检索字符串时失败

iphone - 在 UIWebView 中隐藏键盘

iphone - 如何从 iPhone 上的用户相册加载图像?

iphone - 普通样式的 UINavigationBar BarButtonItem

iphone - 使用UINib时看不到

php - 为多个用户加密/解密数据库中的内容