Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)

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.



C#
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;
}


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.
Posted
Updated 28-May-16 21:27pm
Comments
Sergey Alexandrovich Kryukov 28-May-16 9:47am    
Did you consider using Mono on your Mac, which could run .NET code?
—SA

1 solution

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900