简体   繁体   中英

AES file encryption with .Net Framework and decrypt it with IOS

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.

最可能的问题是从密码派生实际密钥(12345678不能直接是AES密钥-只有8个字节)。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM