简体   繁体   English

目标c。128加密.net aes

[英]objective c aes 128 encryption for .net aes

So I finally found something a little more understable in implementing a AES 128 encryption for a .Net Wcf Service with the same encryption. 因此,我终于找到了一些更加明智的东西,在使用相同加密的.Net Wcf服务实现AES 128加密时更加明显。 My problem with it now is that whenever it would try to decrypt the string it would have a step where it does a FromBase64String convert which will give me an error: 我现在的问题是,每当它试图解密字符串时,它会有一个步骤,它会执行FromBase64String转换,这会给我一个错误:

static public string DecryptString(string message, string key)
{
    string output = "";
    Rijndael aes = new RijndaelManaged();

    try
    {
        byte[] encrypted = Convert.FromBase64String(message);
        byte[] cipherText = GetCipherText(encrypted);

        aes.Key = Convert.FromBase64String(key);
        aes.Mode = CipherMode.CBC;

        aes.IV = GetIV(encrypted);

        using (MemoryStream ms = new MemoryStream())
        {
            using (ICryptoTransform decryptor = aes.CreateDecryptor())
            {
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                {
                    cs.Write(cipherText, 0, cipherText.Length);
                    cs.FlushFinalBlock();

                    byte[] decrypted = ms.ToArray();
                    output = Encoding.UTF8.GetString(decrypted);
                }
            }
        }
    }

The error is: 错误是:

Index was outside the bounds of the array. 指数数组的边界之外。

and it happens on the 它发生在

cs.FlushFinalBlock();

This is the encryption that it produced for the message "heythere" and a key of "25f9e794323b453885f5181f1b624d0b" 这是它为消息“heythere”和“25f9e794323b453885f5181f1b624d0b”的密钥生成的加密

0suql40BUGiDoFA4SdXJAA== 0suql40BUGiDoFA4SdXJAA ==

This is coming from my .Net encryption: 这来自我的.Net加密:

unNWQfm9RaU/HgKlDNEmoXZaTzsuBoTNsA2UvDKZhc4= unNWQfm9RaU / HgKlDNEmoXZaTzsuBoTNsA2UvDKZhc4 =

PS For the iPhone encryption of AES 128, this is where I got the codes from: PS对于AES 128的iPhone加密,这是我从以下代码获得的代码:

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

Look at AES Keeping your documents secure article on MSDN and scroll down to the section entitled "Using the AES Class", "Figure 15 Using AES" has a nice and simple example of encryption / decryption. 查看AES在MSDN上保存文档安全文章并向下滚动到标题为“使用AES类”的部分,“图15使用AES”有一个很好的简单加密/解密示例。

PS Be aware of padding that is used in certain block cypher (like AES) modes (like CBC), these can be a pain when different systems are talking to each other as padding / block sizes can vary from one system to another... see padding fun PS注意在某些块密码(如AES)模式(如CBC)中使用的填充,当不同的系统相互通信时,这些可能会很麻烦,因为填充/块大小可能因系统而异...看到填充乐趣

EDIT: Just spotted this little gem... simple-2-way-encryption-for-c-sharp 编辑:刚发现这个小宝石...... 简单的2路加密换c-sharp

You can find a ready sample for both iPhone and .NET here 您可以在此处找到适用于iPhone和.NET的现成示例

In general, cipher algorithm are universal and should work across different languages just you need to ensure that both algorithms (ie in .NET and iPhone): 一般来说,密码算法是通用的,应该可以在不同的语言中工作,只需要确保两种算法(即在.NET和iPhone中):

  • Using exactly the the same key that is encoded in the same way for storage/transmission (typically base64) 完全使用以相同方式编码的相同密钥进行存储/传输(通常为base64)
  • Have exactly same parameters including padding schema, mode of operation (eg CBC/ECB) 具有完全相同的参数,包括填充模式,操作模式(例如CBC / ECB)

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

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