2

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.

jbtule
  • 31,383
  • 12
  • 95
  • 128
nath
  • 2,848
  • 12
  • 45
  • 75

2 Answers2

0

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).

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
0

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.

Community
  • 1
  • 1
jbtule
  • 31,383
  • 12
  • 95
  • 128