目标C中的AES加密技术 [英] AES encryption technique in objective C
问题描述
我正在开展一个项目,我必须与.net团队协调..我需要在目标中使用下面提到的AES加密算法(用C#编写)的等效代码-C ..我已经尝试过使用AESCrypt和CommonCrypt,但它并没有很好地解决这两种语言的不同加密值。
I am working on a project where i have to coordinate with .net team.. I need the equivalent code for the below mentioned AES encryption algorithm(written in C#) in objective -C.. I have tried using AESCrypt and CommonCrypt but its not woking well..getting different encryption value in both languages.
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
我的尝试:
使用CCKeyDerivationPBKDF我能够加密关键部分,但我需要加密我的数据以及密钥。
我尝试加密我的数据使用RNCryptor,但在使用.net解密加密数据时,我得到一个例外输入数据不是一个完整的块。
What I have tried:
Using CCKeyDerivationPBKDF i was able to encrypt the key part,but i need to encrypt my data too along with key.
I tried encrypting my data using RNCryptor,but while decrypting the encrypted data using .net i get an exception of "The input data is not a complete block.
推荐答案
显而易见的是,你在每一侧使用的任何库的参数必须相同 - 这里有太多的名字,填充是通常的搞砸了(并且与你看到的错误一致)
这里是指某人说他们有工作代码的链接 - 希望有帮助
.Net与iPhone之间的互操作性| Auto-Magical [ ^ ]
The obvious thing is that the parameters for whatever libraries you're using on each side have to be the same - there are too many to name here, padding is the usual one that gets messed up (and would be consistent with the error you saw)
here's a link to someone who says they have working code - hope it helps
AES interoperability between .Net and iPhone | Auto-Magical[^]
这篇关于目标C中的AES加密技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!