简体   繁体   English

适用于.NET,Java(Android)和iOS的AES加密

[英]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. 使用此帖子中的示例在Android和C#之间兼容加密 ,我已成功在.NET应用程序之间实现AES加密,该应用程序为我的Android应用程序提供XML提要。

Now, I am trying to use this same implementation for the iOS version of that app. 现在,我正在尝试对该应用程序的iOS版本使用相同的实现。 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. 我已经找到了一些非常好的AES for iOS的例子,但到目前为止,似乎没有一个与我目前正在使用的方案相匹配。 From what I can tell, the problem is the 16-byte key that is shared between C# and Java (rawSecretKey). 据我所知,问题是C#和Java(rawSecretKey)之间共享的16字节密钥。 In the iOS examples, I've not been able to find a similar key to set with this same byte array. 在iOS示例中,我无法找到使用相同字节数组设置的类似密钥。 It has the passPhrase, but not the byte array. 它有passPhrase,但不是字节数组。

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. 我找到的一个iOS示例是http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/ ,但同样,我没有看到如何将16字节数组包含为在我的帖子顶部的第一个链接中引用。

.Net and IOS both support PKCS7Padding, but Java doesn't (unless use some third-party library) .Net和IOS都支持PKCS7Padding,但Java不支持(除非使用某些第三方库)

.Net and Java both support ISO10126Padding, but IOS doesn't (unless use some third-patry library) .Net和Java都支持ISO10126Padding,但IOS没有(除非使用一些第三个patry库)

So I think you need to have separate .net encryption code for IOS and Java. 所以我认为你需要为IOS和Java提供单独的.net加密代码。

Here are some code examples: 以下是一些代码示例:

for IOS: 对于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: 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: 对于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. 还有其他问题请留言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM