AES 256 Symmetric Cryptography

I am unable to encrypt or decrypt using aes 256 block mode cbc padding pkcs5

please convert this c# to swift

````public class EncDec
{

       static string key = ""; // your key
       public static string Encrypt(string data)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC; //remember this parameter
            rijndaelCipher.Padding = PaddingMode.PKCS7; //remember this parameter

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;

            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }

            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] plainText = Encoding.UTF8.GetBytes(data);

            return Convert.ToBase64String
            (transform.TransformFinalBlock(plainText, 0, plainText.Length));
        }

        public static string Decrypt(string data)
        {
            if (data != null)
            {
                RijndaelManaged rijndaelCipher = new RijndaelManaged();
                rijndaelCipher.Mode = CipherMode.CBC;
                rijndaelCipher.Padding = PaddingMode.PKCS7;

                rijndaelCipher.KeySize = 0x80;
                rijndaelCipher.BlockSize = 0x80;
                byte[] encryptedData = Convert.FromBase64String(data);
                byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
                byte[] keyBytes = new byte[0x10];
                int len = pwdBytes.Length;

                if (len > keyBytes.Length)
                {
                    len = keyBytes.Length;
                }

                Array.Copy(pwdBytes, keyBytes, len);
                rijndaelCipher.Key = keyBytes;
                rijndaelCipher.IV = keyBytes;
                byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock
                            (encryptedData, 0, encryptedData.Length);

                return Encoding.UTF8.GetString(plainText);
            }
            else
            {
                return null;
            }

        }
    }````
  • Please provide me documentation for the above question as soon as possible Request to apple support

Add a Comment

Replies

Request to Apple support

I want to be crystal clear that DevForums is not a formal support channel. If you want formal support, open a DTS tech support incident.

Having said that, DTS will not accept tech support requests like this:

please convert this C# to Swift

We provide guidance on how to use Apple APIs; we will not write your code for you.


With that out of the way, let’s return to your main issue. You wrote:

I am unable to encrypt or decrypt using aes 256 block mode cbc padding pkcs5

Apple’s latest crypto API, CryptoKit, supports AES-GCM because that provides authenticated encryption and we strongly encourage that. AES-CBC provides encryption without authentication, which makes it very easy to create products with security vulnerabilities. If you’re using AES-CBC and you haven’t already had a security expert review your security design, I encourage you to do that now.

Apple’s API for AES-CBC is CommonCrypto. For an example of how to use that in a way that’s compatible with various third-party security toolkits, see the CryptoCompatibility sample code.

ps While I’m not a C# expert, the code you posted looks like it implements AES-128, not AES-256 as per the subject line of your post.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"