Hi,
i am encrypting the data using the pkcs7 padding, if i encrypt the string , length = multipla of 8, then it is working fine. If i ssend the string whose length is not multiple of 8, then it is encrypting the data,but while decrypting it is showing error as kCCDecodeError. Please can any one what is the problem my code is same for encryption and decryption as below:
- (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];//block size is 8
memset((void *) iv, 0x0, (size_t) sizeof(iv));
NSLog(@"doCipher: plaintext: %@", [[NSString alloc]initWithData:plainText 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) {
NSLog(@"value of plainTextBufferSize kChosenCipherBlockSize) %i",(plainTextBufferSize % kChosenCipherBlockSize));
if((plainTextBufferSize % kChosenCipherBlockSize) == 0) {
*pkcs7 = 0x0000;
} else {
*pkcs7 = kCCOptionPKCS7Padding;
}
}
} else if(encryptOrDecrypt != kCCDecrypt) {
LOGGING_FACILITY1( 0, @"Invalid CCOperation parameter [%d] for cipher context.", *pkcs7 );
}
NSLog(@"plain text buffer size is %lu",plainTextBufferSize);
// Create and Initialize the crypto reference.
ccStatus = CCCryptorCreate(encryptOrDecrypt,
kCCAlgorithm3DES,
*pkcs7,
(const void *)[aSymmetricKey bytes],
kChosenCipherKeySize,
(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);
NSLog(@"plaintext buffer size is %ld",plainTextBufferSize);
// 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 );
if (ccStatus == kCCSuccess)
cipherOrPlainText = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)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
);
*/
}
Replies
totalBytesWritten += movedBytes;
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome