views:

16

answers:

1

Hi, I'm developing an application on iphone. I had an application on Java using encryption (RSA) and I created a Private Key and Public Key. I want use the Public Key in Java application on iphone. For Ex: My Public Key is byte[] publicKey = {0x01,0x02}; How can I use my publicKey to encrypt data on iphone? I saw CryptoExercise, but i cannot build it (function: SecKeyEncrypt err:EXC_BAD_ACCESS ). Can i use getPublicKeyBits() or getPublicKeyRef()?

Here is my code:

* (NSData *)getPublicKeyBits {

OSStatus sanityCheck = noErr;

//
const char myByteArray[] = {
0x00, -0x79, 0x7C, 0x34,
0x5C, -0x36, 0x36, 0x75,
0x0E, 0x7F, -0x21, -0x05,
0x41, 0x21, 0x4F, -0x30,
0x2D, 0x5F, 0x08, -0x25,
0x07, -0x08, 0x22, -0x09,
0x32, -0x6C, 0x10, 0x1E, 0x5A,
-0x59, -0x14, -0x55, -0x73, 0x21,
0x5E, -0x54, -0x5E, -0x72, 0x37,
-0x31, -0x25, -0x45, 0x3B, 0x7D, -0x3C,
-0x6F, -0x40, -0x7E, 0x74, -0x68, -0x23,
0x42, 0x12, -0x62, -0x66, 0x4D, 0x20, -0x69,
0x28, -0x28, -0x36, -0x71, 0x21, 0x02, -0x32,
-0x19, 0x66, 0x7D, 0x3E, 0x03, 0x49, -0x66, 0x1F,
-0x38, 0x3C, 0x0A, 0x5F, 0x60, 0x1B, -0x75, 0x41, 0x48,
-0x5F, 0x1F, -0x34, -0x31, -0x09, 0x17, 0x23, 0x11, 0x1E,
-0x68, 0x0B, -0x4D, 0x69, -0x3F, -0x27, 0x13, -0x71, -0x6D,
-0x7A, 0x3A, 0x64, 0x2A, 0x6A, -0x6E, 0x3C, 0x04, -0x70, -0x1C};

NSData *publicKeyBits = NSData dataWithBytes: myByteArray length: sizeof(myByteArray);
//
//NSData * publicKeyBits = {1};

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

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

if (sanityCheck != noErr)
{
printf("sanitycheck error@@@@@");
publicKeyBits = nil;
}

NSLog(@"** public key bits: %s", &publicKeyBits);

queryPublicKey release;


return publicKeyBits;
}

// encrypt message

    * (void)encryptWithPublicKey:(uint8_t *)plainBuffer cipherBuffer:(uint8_t *)cipherBuffer

{
NSLog(@"== encryptWithPublicKey()");
NSData* publicKey= [AppController sharedWrapper] getPublicKeyBits;

OSStatus status = noErr;

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

size_t cipherBufferSize = 1;
uint8_t *pPlainText = (uint8_t*)"This is a test";
uint8_t *aCipherText;
size_t *iCipherLength = (size_t*)"1024";

// Error handling
// Encrypt using the public.
printf("begin ecrypt !!!!");
// status = SecKeyEncrypt(publicKey,
// kSecPaddingNone,
// plainBuffer,
// plainBufferSize,
// &cipherBuffer[0],
// &cipherBufferSize
// );

status = SecKeyEncrypt(publicKey,
kSecPaddingNone,
pPlainText,
strlen( (char*)pPlainText) + 1,
aCipherText,
iCipherLength);
printf("end encrypt !!!!");
NSLog(@"encryption result code: %d (size: %d)", status, cipherBufferSize);
NSLog(@"encrypted text: %s", cipherBuffer);
}

Please Help me! Thank you very much

A: 

I'm not 100% following it, but I think your issue could be as follows:

Public/Private key pairs are not used to encrypt data. They are used only to encrypt fixed-size blocks of data, which are short - on the order of the size of the public/private key themselves.

So they way these are used is that the public/private key is used to encrypt [something like] an AES Key - which in-turn is used with a mode (like EBC, etc) to encrypt a stream, or irregularly-sized block of data.

I think you are trying to use the Public/Private keypair to encrypt your user-data, which is likely not the correct size for the public/private key operations - thus your leaking and getting an BAD_ACCESS_ERROR.

Brad