1

So I'm trying to figure out how to make an objective c version of this as it's part of a wcf service security that was implemented. My main issue here is Idk where to begin as some articles/tutorials would automatically pad 0 in the beginning (not sure if that's their IV) like so:

(a couple of replies down)

http://iphonedevsdk.com/forum/iphone-sdk-development/60926-aesencryption-in-objective-c-and-php.html

compared to this which doesn't use any padding:

Objective-C decrypt AES 128 cbc hex string

Anyway, this is how the wcf encrypts using AES 128. Any advice or nudge to the right direction is very much appreciated. Thanks!!!.

public class EncryptionAES128
    {
     static public string EncryptString(string inputString, string key)
            {
                string output = "";


                Rijndael encryption = new RijndaelManaged();

                try
                {
                    encryption.IV = GenerateIV();
                    encryption.Key = StringToByte(key);

                    ICryptoTransform encryptor = encryption.CreateEncryptor(encryption.Key, encryption.IV);

                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(inputString);
                            }

                            byte[] cipherText = msEncrypt.ToArray();
                            byte[] encrypted = new byte[cipherText.Length + encryption.IV.Length];

                            encryption.IV.CopyTo(encrypted, 0);
                            cipherText.CopyTo(encrypted, IV_LENGTH);

                            output = ByteToString(encrypted);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }


                return output;

}

Community
  • 1
  • 1
gdubs
  • 2,724
  • 9
  • 55
  • 102
  • What does your CommonCrypto code look like? You can set a padding in the CommonCrypt functions: http://developer.apple.com/library/mac/#DOCUMENTATION/Darwin/Reference/ManPages/man3/CCCrypt.3cc.html – codingFriend1 Jan 28 '13 at 14:34
  • hmm.. well it's the regular commoncrypto header. I gotta check when I get home later. – gdubs Jan 28 '13 at 15:00
  • Would it make sense to use SSL and not implement your own crypto? – bmm6o Jan 29 '13 at 01:29
  • well the encryption on the web service are already set up I just need to recreate it on the iphone app. I think it's easier. – gdubs Jan 29 '13 at 03:18
  • If I get you right, the encryption output of your Objective-C code does not match the encryption created by WCF. You should post the Objective-C code you have written, so we can tell you what part might make the difference. – codingFriend1 Jan 29 '13 at 08:29
  • well it's more like idk where to start cuz the two links that i posted have different approaches. i dont have any idea on which one i should be following. – gdubs Jan 30 '13 at 03:09
  • Did you ever have any luck with this? I'm facing the same problem. – Ben Williams May 22 '13 at 23:43

0 Answers0