与Java相比,Rijndael管理了糟糕的性能。 [英] RijndaelManaged terrible performance when compared to Java.

查看:79
本文介绍了与Java相比,Rijndael管理了糟糕的性能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究性能瓶颈,并发现"RijndaelManaged"和是造成它。我有一个应用程序解密由Java加密的数据。 Java密码使用以下参数进行实例化:
" AES / CFB8 / NoPadding"我发现"RijndaelManaged"应该用来解密c#这个。

I have been currently investigating a performance bottleneck and found out that "RijndaelManaged" is causing it. I have an application that decrypts data that is encrypted by Java. The Java cipher is instantiated with the following parameters: "AES/CFB8/NoPadding" and I have found that "RijndaelManaged" is supposed to be used to decrypt this on c#'s side.

这是我的C#加密类代码:

    public ICryptoTransform enc;
    public ICryptoTransform dec;

    public AesCrypto(byte[] key)
    {
        enc = Generate(key).CreateEncryptor();
        dec = Generate(key).CreateDecryptor();
    }

    private SymmetricAlgorithm Generate(byte[] key) {
        RijndaelManaged cipher = new RijndaelManaged(); 
        cipher.Mode = CipherMode.CFB;
        cipher.Padding = PaddingMode.None;
        cipher.KeySize = 128;
        cipher.FeedbackSize = 8;
        cipher.Key = key;
        cipher.IV = key;
        return cipher;
    }

    public byte[] Crypt(byte[] buffer, int offset, int count) {
        return enc.TransformFinalBlock(buffer, offset, count); 
    }




C#测试代码:

 static void Test() {
        // Init
        var AesCrypto = new AesCrypto(Encoding.UTF8.GetBytes("aaabbbccaaabbbcc"));
        var testData = Encoding.UTF8.GetBytes(createDataSize(9000000)); // 9mb test.

        // Timer
        var stopWatch = new Stopwatch();
        stopWatch.Start();
        AesCrypto.Crypt(testData, 0, testData.Length);
        stopWatch.Stop();
        Console.WriteLine("AesCrypto.Crypt took: " + stopWatch.ElapsedMilliseconds);
 }
 static string createDataSize(int msgSize)
    {
        StringBuilder sb = new StringBuilder(msgSize);
        for (int i = 0; i < msgSize; i++)
        {
            sb.Append('a');
        }
        return sb.ToString();
    }

结果:"AesCrypto.Crypt拍摄:3626"

Result: "AesCrypto.Crypt took: 3626"

正如你所看到的,解码9mb的数据需要3.63秒,与Java相比这是疯狂的。 java等价花了大约0.406秒。这是Java端加密代码:

As you can see to decode 9mb of data it took 3.63 seconds, which is insane when compared to Java. The java equivalence took about 0.406 seconds. Here is the Java side crypto code:

public Cryptor(boolean reader) throws CryptingException {

        keySpec = new SecretKeySpec(secretKey.getBytes(CHARSET), "AES");
        ivSpec = new IvParameterSpec(iv.getBytes(CHARSET));
        try {
            cipher = Cipher.getInstance("AES/CFB8/NoPadding");
            if (reader) cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            else cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        } catch (NoSuchAlgorithmException e) {
            throw new SecurityException(e);
        } catch (NoSuchPaddingException e) {
            throw new SecurityException(e);
        }catch (InvalidKeyException e) {
            throw new SecurityException(e);
        } catch (InvalidAlgorithmParameterException e) {
            throw new SecurityException(e);
        }
    }

    public byte[] decrypt(byte[] input) throws CryptingException {
            return cipher.update(input);
    }

    public byte[] encrypt(String input) throws CryptingException {
        return cipher.update(input.getBytes());
    }

Java加密速度比C#等效速度快10倍左右。我究竟做错了什么?甚至还有解决方案吗?

Java crypto is around 10 times faster than the C# equivalence. What am I doing wrong? Is there even a solution to this?

* Side节点,我尝试使用`AesCryptoServiceProvider`,但它需要数据大小因为我不允许使用任何填充,所以是16的倍数。 "AesCryptoServiceProvider"的性能略高于Java,并且是一个完美的
解决方案,但与Java的解决方案不同,它要求数据的大小为16的倍数。

* Side node, I have tried to use `AesCryptoServiceProvider` instead, however it requires the data size to be a multiple of 16 as I am not allowed to use any padding. `AesCryptoServiceProvider` has slightly faster performance than Java, and would be a perfect solution, however unlike Java's solution it requires the data's size to be a multiple of 16.

推荐答案

嗨  Rijndael管理与Java的糟糕表现,

感谢您在此发帖。

对于您的问题,通常,t .Net Framework API的加密速度几乎是Java API速度的两倍。但是,在 Rijndael算法中,jave比C#更快。

For your question, normally, the encryption speed of the .Net Framework APIs are almost twice as the speed of those in the Java APIs. However, in Rijndael algorithm, jave is more quick than C#.

请检查以下链接。

https://www.tandfonline.com/doi/full /10.1080/10658980701784602

最好的问候,

Wendy

< span style ="">注意:此响应包含对第三方万维网站点的引用。 Microsoft提供此信息是为了方便您使用。 

Microsoft不控制这些网站,也未测试在这些网站上找到的任何软件或信息;因此,Microsoft不能就其中发现的任何软件或信息的质量,安全性或适用性做出任何陈述。

使用互联网上的任何软件都存在固有的危险,微软提醒您在从互联网上检索任何软件之前确保您完全了解风险。 


这篇关于与Java相比,Rijndael管理了糟糕的性能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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