Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-15-2011, 07:11 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up symmetric key algorithm

Hi,

i am generating the key and encryting the data using 3DES algorithm,

My code work for only particular string "this is some secret labs" or "this is some secret test".
If i change the string it does not work throught the exception "'Problem with encipherment ccStatus == -4301'"

my code is here please anyone tell me what is the problem and it is catching exception in ccStatus = CCCryptorFinal(thisEncipher,
ptr,
remainingBytes,
&movedBytes
);


please help me following is my code

- (NSData *)doCipher: (NSData * )plainText key: (NSData * )aSymmetricKey
context: (CCOperation)encryptOrDecrypt padding: (CCOptions *)pkcs7 {
CCCryptorStatus ccStatus = kCCSuccess;
// Symmetric crypto reference.
CCCryptorRef thisEncipher = NULL;
// Cipher Text container.
NSData * cipherOrPlainText = nil;
// Pointer to output buffer.
uint8_t * bufferPtr = NULL;
// Total size of the buffer.
size_t bufferPtrSize = 0;
// Remaining bytes to be performed on.
size_t remainingBytes = 0;
// Number of bytes moved to buffer.
size_t movedBytes = 0;
// Length of plainText buffer.
size_t plainTextBufferSize = 0;
// Placeholder for total written.
size_t totalBytesWritten = 0;
// A friendly helper pointer.
uint8_t * ptr;

// Initialization vector; dummy in this case 0's.
uint8_t iv[kChosenCipherBlockSize];
memset((void *) iv, 0x0, (size_t) sizeof(iv));

NSLog(@"doCipher: plaintext: %@", [[NSString alloc]initWithDatalainText encoding:NSASCIIStringEncoding]);
NSLog(@"doCipher: key length: %d", [aSymmetricKey length]);

LOGGING_FACILITY(plainText != nil, @"PlainText object cannot be nil." );
LOGGING_FACILITY(aSymmetricKey != nil, @"Symmetric key object cannot be nil." );
LOGGING_FACILITY(pkcs7 != NULL, @"CCOptions * pkcs7 cannot be NULL." );
// LOGGING_FACILITY([aSymmetricKey length] == kChosenCipherKeySize, @"Disjoint choices for key size." );// prathibha since error was coming

plainTextBufferSize = [plainText length];

LOGGING_FACILITY(plainTextBufferSize > 0, @"Empty plaintext passed in." );

NSLog(@"pkcs7: %d", *pkcs7);
// We don't want to toss padding on if we don't need to
if(encryptOrDecrypt == kCCEncrypt) {
if(*pkcs7 != kCCOptionECBMode) {
if((plainTextBufferSize % kChosenCipherBlockSize) == 0) {
*pkcs7 = 0x0000;
} else {
*pkcs7 = kCCAlgorithm3DES;
}
}
} else if(encryptOrDecrypt != kCCDecrypt) {
LOGGING_FACILITY1( 0, @"Invalid CCOperation parameter [%d] for cipher context.", *pkcs7 );
}

// Create and Initialize the crypto reference.
ccStatus = CCCryptorCreate(encryptOrDecrypt,
kCCAlgorithm3DES,
*pkcs7,
(const void *)[aSymmetricKey bytes],
kChosenCipherKeySize * 3,
(const void * )iv,
&thisEncipher
);


NSLog(@"1111 %d",ccStatus);

LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem creating the context, ccStatus == %d.", ccStatus );

// Calculate byte block alignment for all calls through to and including final.
bufferPtrSize = CCCryptorGetOutputLength(thisEncipher, plainTextBufferSize, true);
// bufferPtrSize = ([plainText length] + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);

NSLog(@"size is %ld",bufferPtrSize);

// Allocate buffer.
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t) );

// Zero out buffer.
memset((void *)bufferPtr, 0x0, bufferPtrSize);

// Initialize some necessary book keeping.

ptr = bufferPtr;

// Set up initial size.
remainingBytes = bufferPtrSize;

NSLog(@"value of ptr is %lu",ptr);

NSLog(@"value of remaining byte is %lu",remainingBytes);

NSLog(@"value of movedbyte is %lu",movedBytes);
// Actually perform the encryption or decryption.
ccStatus = CCCryptorUpdate(thisEncipher,
(const void *) [plainText bytes],
plainTextBufferSize,
ptr,
remainingBytes,
&movedBytes
);

NSLog(@"2222 %d",ccStatus);
NSLog(@"value of movedbyte is %lu",movedBytes);

LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem with CCCryptorUpdate, ccStatus == %d.", ccStatus );

// Handle book keeping.
ptr += movedBytes;//16 = MovedBytes
NSLog(@"value of ptr is %lu",ptr);
NSLog(@"value of movedbytes 1111 is %lu",movedBytes);
remainingBytes -= movedBytes;
NSLog(@"value of remaining byte is %lu",remainingBytes);
NSLog(@"value of movedbytes 2222 is %lu",movedBytes);

totalBytesWritten += movedBytes;
NSLog(@"value of totoalbytes is %lu",totalBytesWritten);
NSLog(@"value of movedbytes 333 is %lu",movedBytes);


/* From CommonCryptor.h:

@enum CCCryptorStatus
@abstract Return values from CommonCryptor operations.

@constant kCCSuccess Operation completed normally.
@constant kCCParamError Illegal parameter value.
@constant kCCBufferTooSmall Insufficent buffer provided for specified operation.
@constant kCCMemoryFailure Memory allocation failure.
@constant kCCAlignmentError Input size was not aligned properly.
@constant kCCDecodeError Input data did not decode or decrypt properly.
@constant kCCUnimplemented Function not implemented for the current algorithm.

enum {
kCCSuccess = 0,
kCCParamError = -4300,
kCCBufferTooSmall = -4301,
kCCMemoryFailure = -4302,
kCCAlignmentError = -4303,
kCCDecodeError = -4304,
kCCUnimplemented = -4305
};
typedef int32_t CCCryptorStatus;
*/

// Finalize everything to the output buffer.
ccStatus = CCCryptorFinal(thisEncipher,
ptr,
remainingBytes,
&movedBytes
);
NSLog(@"3333 %d",ccStatus);

// totalBytesWritten += movedBytes;
NSLog(@"11111111111 %lu",totalBytesWritten);
NSLog(@"222222 %lu",movedBytes);
if(thisEncipher) {
(void) CCCryptorRelease(thisEncipher);
thisEncipher = NULL;
}

LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem with encipherment ccStatus == %d", ccStatus );//here crashing

if (ccStatus == kCCSuccess)
cipherOrPlainText = [NSData dataWithBytesconst void *)bufferPtr lengthNSUInteger)totalBytesWritten];
else
cipherOrPlainText = nil;

if(bufferPtr) free(bufferPtr);

return cipherOrPlainText;

/*
Or the corresponding one-shot call:

ccStatus = CCCrypt( encryptOrDecrypt,
kCCAlgorithmAES128,
typeOfSymmetricOpts,
(const void *)[self getSymmetricKeyBytes],
kChosenCipherKeySize,
iv,
(const void *) [plainText bytes],
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes
);
*/
}
prathibha is offline   Reply With Quote
Old 09-15-2011, 03:53 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

I'm still googling for the answer, but do you know if 3Des supports variable length data? I say that because I notice that both of your strings are 24 characters in length, you haven't posted what strings don't work, I wonder if a 32 character length string works for you?
QuantumDoja is offline   Reply With Quote
Old 09-18-2011, 11:56 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 84
prathibha is on a distinguished road
Thumbs up

Quote:
Originally Posted by QuantumDoja View Post
I'm still googling for the answer, but do you know if 3Des supports variable length data? I say that because I notice that both of your strings are 24 characters in length, you haven't posted what strings don't work, I wonder if a 32 character length string works for you?
ya i checked it does not work for 32 character string, Is there any method where i can change the string length in3des algorithms?
prathibha is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, objective c

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 407
16 members and 391 guests
7twenty7, Eclectic, eski, EvilElf, fiftysixty, HemiMG, iOS.Lover, JackReidy, jarv, Pudding, sacha1996, teebee74, tim0504, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0