.Net 和 iPhone 之间的 AES 互操作性? [英] AES interoperability between .Net and iPhone?

查看:23
本文介绍了.Net 和 iPhone 之间的 AES 互操作性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 iPhone 上加密一个字符串并将其发送到 .Net 网络服务进行解密.我可以在 iPhone 和 .Net 上加密/解密,但来自 iPhone 的加密字符串不能被 .Net 解密.我得到的错误是填充无效,无法删除."

I need to encrypt a string on the iPhone and send it to a .Net web service for decryption. I am able to encrypt/decrypt on the iPhone and with .Net, but the encrypted strings from the iPhone cannot be decrypted by .Net. The error I get is "Padding is invalid and cannot be removed."

.Net 代码来自:http://blog.realcoderscoding.com/index.php/2008/07/dot-net-encryption-simple-aes-wrapper/

iPhone 代码使用以下示例代码:http://nootech.wordpress.com/2009/01/17/symmetric-encryption-with-the-iphone-sdk/

The iPhone code uses the sample code from: http://nootech.wordpress.com/2009/01/17/symmetric-encryption-with-the-iphone-sdk/

AFAIK 我的密钥设置是一样的:

AFAIK my key settings are the same:

result.BlockSize = 128; // iPhone: kCCBlockSizeAES128
result.KeySize = 128; // kCCBlockSizeAES128
result.Mode = CipherMode.CBC;
result.Padding = PaddingMode.PKCS7; // kCCOptionPKCS7Padding

我尝试了不同的生成密文的方法.你好/你好是:

I tried different ways of generating ciphertext. hello/hello is:

.Net 中的 e0PnmbTg/3cT3W+92CDw1Q==

e0PnmbTg/3cT3W+92CDw1Q== in .Net

yrKe5Z7p7MNqx9+CbBvNqQ== 在 iPhone 上

yrKe5Z7p7MNqx9+CbBvNqQ== on iPhone

和openssl enc -aes-128-cbc -nosalt -a -in hello.txt -pass pass:hello"生成:QA+Ul+r6Zmr7yHipMcHSbQ==

and "openssl enc -aes-128-cbc -nosalt -a -in hello.txt -pass pass:hello" generates: QA+Ul+r6Zmr7yHipMcHSbQ==

更新:我已经发布了工作代码为此.

推荐答案

至少,您使用的是不同的初始化向量 (IV).

At the very least, you are using differing initialization vectors (IV).

  • .Net 代码使用 IV 的密钥.

  • The .Net code uses the key for IV.

private static AesCryptoServiceProvider GetProvider(byte[] key)
{
    //Set up the encryption objects
    AesCryptoServiceProvider result = new AesCryptoServiceProvider();
    byte[] RealKey = Encryptor.GetKey(key, result);
    result.Key = RealKey;
    result.IV = RealKey;
    return result;
}

private static byte[] GetKey(byte[] suggestedKey, AesCryptoServiceProvider p)
{
    byte[] kRaw = suggestedKey;
    List kList = new List();
    for (int i = 0; i < p.LegalKeySizes[0].MinSize; i += 8 )
    {
        kList.Add(kRaw[i % kRaw.Length]);
    }
    byte[] k = kList.ToArray();
    return k;
}

可能应该是:kList.Add(kRaw[(i/8) % kRaw.Length]);.否则长度为 % 8 == 0 的键将重复使用相同的字母,哦!

which should probably be: kList.Add(kRaw[(i / 8) % kRaw.Length]);. Otherwise a key whose length % 8 == 0 will use the same letter repeatedly, doh!

因此,.Net 使用的 IV(和密钥)是:hleolhleolhleolh.这不是 API 的一部分,而是由于您指向的包装器代码(其中有一个严重的错误......).

Thus the IV (and key) used by .Net is: hleolhleolhleolh. This is not part of the API, but rather due to the wrapper code that you pointed at (which has a serious bug in it...).

iPhone 代码使用 0 表示 IV.

The iPhone code uses 0 for IV.

// Initialization vector; dummy in this case 0's.
uint8_t iv[kChosenCipherBlockSize];
memset((void *) iv, 0x0, (size_t) sizeof(iv));

  • 默认情况下,openssl 会预先添加随机生成的盐(这就是输出更长的原因!).

  • openssl by default prepends a randomly generated salt (which is why the output is longer!).

    openssl 输出更安全,因为它预先添加了一个随机初始化向量.看起来 base64 解码字符串的前几个字节是Salted__".您还可以要求 openssl 不使用盐 (-nosalt) 和/或提供 IV (-iv).

    The openssl output is more secure since it is prepending a random initialization vector. It looks like the first few bytes of the base64 decoded string is "Salted__". You can also ask openssl to not use a salt (-nosalt) and / or provide an IV (-iv).

    本质上,openssl、.Net 和 iPhone 使用相同的加密,您只需要注意如何使用加密密钥和初始化向量来初始化 API.

    Essentially, openssl, .Net, and the iPhone are using the same encryption, you just need to be careful how you initialize the APIs with the encryption key and the initialization vector.

    这篇关于.Net 和 iPhone 之间的 AES 互操作性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆