CryptoExcercise Encryption/Decryption Problem

Posted by venkat on Stack Overflow See other posts from Stack Overflow or by venkat
Published on 2010-05-13T04:25:56Z Indexed on 2010/05/13 4:34 UTC
Read the original article Hit count: 393

Filed under:
|
       I am using apples "cryptoexcercise" (Security.Framework) in my application to encrypt and decrypt  a data of numeric value. When I give the input 950,128 the values got encrypted, but it is not getting decrypted and exists with the encrypted value only. This happens only with the mentioned numeric values.  Could you please check this issue and give the solution to solve this problem?

here is my code


  (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; }

© Stack Overflow or respective owner

Related posts about iphone

Related posts about encryption