Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES encryption for .NET, Java (android) and iOS

Using the example from this post Encryption compatible between Android and C#, I have successfully implemented AES encryption between a .NET application that provides XML feeds to my Android application.

Now, I am trying to use this same implementation for the iOS version of that app. I have found some really good examples of AES for iOS, but so far, none seem to match the scheme that I am currently using. From what I can tell, the problem is the 16-byte key that is shared between C# and Java (rawSecretKey). In the iOS examples, I've not been able to find a similar key to set with this same byte array. It has the passPhrase, but not the byte array.

If anyone knows of a good example that illustrates this type of implementation, it would be very helpful. One iOS example I found was http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/, but again, I don't see how to include the 16-byte array as referenced in the first link at the top of my post.

like image 310
Kyle Avatar asked Sep 21 '11 14:09

Kyle


People also ask

Does Android use AES 256?

AES-256 is supported by android.

What is AES in IOS?

AES is a standard that encrypts data given a key. The same key used to encrypt the data is used to decrypt the data. There are different key sizes, and AES256 (256 bits) is the preferred length to be used with sensitive data.

What is AES encryption in Java?

AES is an Advanced Encryption Standard algorithm. It is a type of symmetric, block cipher encryption and decryption algorithm. It works with key size 128, 192, and 256 bits. It uses a valid and similar secret key for both encryption and decryption.

Which AES encryption is best?

The AES algorithm is the industry-standard encryption protocol that protects sensitive information from traditional brute-force attacks. The two most common versions are 256-bit AES (providing greater security) and 128-bit AES (providing better performance during the encryption and decryption process).


1 Answers

.Net and IOS both support PKCS7Padding, but Java doesn't (unless use some third-party library)

.Net and Java both support ISO10126Padding, but IOS doesn't (unless use some third-patry library)

So I think you need to have separate .net encryption code for IOS and Java.

Here are some code examples:

for IOS:

+ (NSData*)encryptData:(NSData*)data :(NSData*)key :(NSData*)iv
{    
    size_t bufferSize = [data length]*2;
    void *buffer = malloc(bufferSize);
    size_t encryptedSize = 0;    
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,                                          
                                          [key bytes], [key length], [iv bytes], [data bytes], [data length],                                          
                                          buffer, bufferSize, &encryptedSize);  
    if (cryptStatus == kCCSuccess)      
        return [NSData dataWithBytesNoCopy:buffer length:encryptedSize];    
    else
        free(buffer);
    return NULL;
}

for .NET:

public static byte[] AesEncrypt(byte[] bytes, byte[] key, byte[] iv)
{
    if (bytes == null || bytes.Length == 0 || key == null || key.Length == 0 || iv == null || iv.Length == 0)
        throw new ArgumentNullException();

    using (var memoryStream = new MemoryStream())
    {
        using (var rijndaelManaged = new RijndaelManaged { Key = key, IV = iv, Padding = PaddingMode.PKCS7, Mode = CipherMode.CBC })
        {
            using (var cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                cryptoStream.Write(bytes, 0, bytes.Length);
            }
        }
        return memoryStream.ToArray();
    }
}

for Java:

public static byte[] encrypt(byte[] bytes, byte[] key, byte[] iv)
        throws Exception
{
    Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),
            new IvParameterSpec(iv));
    return cipher.doFinal(bytes);
}

I only provide the code for encryption because decryption code is very similar and you can figure it out easily.

Any more question please leave comments.

like image 148
Tyler Liu Avatar answered Oct 23 '22 17:10

Tyler Liu