Note that there are some explanatory texts on larger screens.

plurals
  1. PORSA encryption/decryption in objective c
    primarykey
    data
    text
    <p>I am tring to implement RSA encryption in my app using SecKeyEncrypt and SecKeyDecrypt. The issue is that only when the ciphertext is long as KeyblockSize (128,256 etc), I get it decrypted correctly. Also, for each build and run, I get different results (encrypted data and cipher text length) keeping input plain text the same. And on clean and build several times, once in a while cipher text length becomes keyblocksize and so decrypts properly. Pls help to understand where the problem is. Thanks in advance</p> <p>Note: From SecKeyEncrypt's definition got to know that the last parameter gives back the no. of bytes written, i.e. ciphertextlength in bytes. This parameter always returns back my keyblocksize for any input string. But many times the actual ciphertext length varies with this parameter. And whenever ciphertextlength is same as the parameter, gets decrypted correctly.</p> <p>Posting encrypt and decrypt portions of my code :</p> <pre><code>- (void)encryptWithPublicKey:(uint8_t *)plainBuffer cipherBuffer:(uint8_t *)cipherBuffer { OSStatus status = noErr; NSLog(@"** original plain text 0: %s", plainBuffer); size_t plainBufferSize = strlen((char *)plainBuffer);// size_t cipherBufferSize = CIPHER_BUFFER_SIZE;// its 1024 SecKeyRef key=[self getPublicKeyRef]; NSLog(@"SecKeyGetBlockSize() public = %lu", SecKeyGetBlockSize(key));//it returns 256 // Error handling // Encrypt using the public. status = SecKeyEncrypt([self getPublicKeyRef], kSecPaddingNone, plainBuffer, plainBufferSize, &amp;cipherBuffer[0], &amp;cipherBufferSize ); NSLog(@"encryption result code: %ld (size: %lu)", 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); // DECRYPTION size_t plainBufferSize = BUFFER_SIZE;//its 256 // Error handling status = SecKeyDecrypt([self getPrivateKeyRef], kSecPaddingNone, &amp;cipherBuffer[0], cipherBufferSize, &amp;plainBuffer[0], &amp;plainBufferSize ); NSLog(@"decryption result code: %ld (size: %lu)", status, plainBufferSize); NSLog(@"FINAL decrypted text: %s", plainBuffer); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload