Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We encrypt PDFs using AESManaged algorithm implemented in .NET framework. I used the example explained in here to implement C# code to encrypt the file. Now I need to decrypt that file using an iPhone application.(That is the requirement). So I use the this code to do that but decryption failed by returning an error.

'Error Domain=CommonCryptoErrorDomain Code=-4304 "Decode Error" UserInfo=0x127356c0 {NSLocalizedFailureReason=Input data did not decode or decrypt correctly, NSLocalizedDescription=Decode Error'

Can some one help me to resolve this issue.

We use 12345678 as encryption key.

share|improve this question
So which error it returns? – Nickolay Olshevsky May 6 at 11:56
It says 'Error Domain=CommonCryptoErrorDomain Code=-4304 "Decode Error" UserInfo=0x127356c0 {NSLocalizedFailureReason=Input data did not decode or decrypt correctly, NSLocalizedDescription=Decode Error' – ganuke May 6 at 12:17
@ganuke Don't post important information as a comment. Please edit your question and add the error. – Duncan Jones May 6 at 12:47
You can't use 12345678 as encryption key. AES takes a 16, 24 or 32 byte sequence, but that is a string. – CodesInChaos May 6 at 15:58

2 Answers

Most likely the problem is in the deriving actual key from the password (12345678 cannot be the AES key directly - it is only 8 bytes).

share|improve this answer

Technically this should work though I've never tested it, both methods uses the same ad-hoc format.

Encrypt using my authenticated encryption example.

//use your secret data you want to encrypt instead.
String secretMessage = "Message";

var rnCryptorHeader = new Byte[]{
                            2, //RNCryptor Format version 2
                            0  //RNCryptor Uses password
                        };

//encryptedString is base64 encoded
var encryptedString = AESThenHMAC.SimpleEncryptWithPassword(secretMessage, 
                                                            password:"1234567891011",      
                                                            nonSecretPayload:rnCryptorHeader);

Then Decrypt using RNCryptor and NSData+Base64 for IOS

//This is the encrypted data passed from .net
NSString *encryptedString = @"AgE8C9E7gsfyOAmSotIOgyLQ0O6mdcuMXXjN/iZa3azym4KVWZAkfykIP6mqMt/qkpfftdB3XQhMkoxtQEM+rA0iHxOvZiNlmA2KJtg6BOnmlg==";

NSData *encryptedData = [NSData dataFromBase64String: encryptedString];
NSError *error;
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
                                    withPassword:@"1234567891011"
                                           error:&error];
NSString *secretMessage = [[[NSString alloc] initWithData:decryptedData
                                                 encoding:NSUTF8StringEncoding] autorelease];

Since you aren't dealing with strings and are dealing with bytes directly, just remove the Base64 and utf8 encoding/decoding from this objective-c example and the linked c# example, once you are sure this is working.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.