简体   繁体   English

使用.Net Framework进行AES文件加密并使用IOS对其进行解密

[英]AES file encryption with .Net Framework and decrypt it with IOS

We encrypt PDFs using AESManaged algorithm implemented in .NET framework. 我们使用.NET框架中实现的AESManaged算法对PDF进行加密。 I used the example explained in here to implement C# code to encrypt the file. 我使用此处说明的示例来实现C#代码来加密文件。 Now I need to decrypt that file using an iPhone application.(That is the requirement). 现在,我需要使用iPhone应用程序解密该文件。(这是必需的)。 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' '错误域= CommonCryptoErrorDomain代码= -4304“解码错误” UserInfo = 0x127356c0 {NSLocalizedFailureReason =输入数据未正确解码或解密,NSLocalizedDescription =解码错误'

Can some one help me to resolve this issue. 有人可以帮我解决这个问题吗?

We use 12345678 as encryption key. 我们使用12345678作为加密密钥。

最可能的问题是从密码派生实际密钥(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 然后使用RNCryptorNSData + 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. 由于您不是在处理字符串而是在直接处理字节,因此,只要确定能正常工作,就从此Objective-c示例和链接的c#示例中删除Base64和utf8编码/解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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