开发者

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

开发者 https://www.devze.com 2023-04-06 05:29 出处:网络
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 applicati

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.rationalmi开发者_运维技巧nd.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.


.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号