Filling PKCS5 in Objective-C

Is there a way to get the actual PKCS5 padding in Cocoa Touch? Although I am well aware that PKCS7 and PKCS5 are compatible for decryption purposes, I need to match the exact encryption method that the server uses, since the encrypted password is hashed and used as the decryption key for the encrypted data. This is pretty confusing, but it's pretty safe. Unfortunately, I do not think that PKCS7 and PKCS5 can be used interchangeably if after that you will hash the filled lines. Can someone help me? Bonus points if it works well with NSData + CommonCrypto or RNCryptor libraries.

+4
source share
1 answer

Here is my solution. Worked like a charm.

NSString *password = @"YOUR PASSWORD HERE"; NSMutableData *passwordData = [[NSMutableData alloc] initWithData:[password dataUsingEncoding:NSUTF8StringEncoding]]; int blockSize = 16; int charDiv = blockSize - ((passwordData.length + 1) % blockSize); //PKCS5 Padding NSMutableString *padding = [[NSMutableString alloc] initWithFormat:@"%c",(unichar)10]; for (int c = 0; c <charDiv; c++) { [padding appendFormat:@"%c",(unichar)charDiv]; } [passwordData appendData:[padding dataUsingEncoding:NSUTF8StringEncoding]]; 

... and your populated data will be in passwordData.

+3
source

All Articles