Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么C#在生成RSA密钥方面比Python快得多?_C#_Python_.net_Rsa_Rsacryptoserviceprovider - Fatal编程技术网

为什么C#在生成RSA密钥方面比Python快得多?

为什么C#在生成RSA密钥方面比Python快得多?,c#,python,.net,rsa,rsacryptoserviceprovider,C#,Python,.net,Rsa,Rsacryptoserviceprovider,我需要在我的程序中使用基本的RSA加密,所以我使用 例如,我注意到它生成密钥的速度比PyCrypto(Python)快得多 以下是我制定的一些基准: 执行时间(.NET与rsacyptoserviceprovider) 1024位密钥对的平均生成时间:111,2毫秒 2048位密钥对的平均生成时间:394,8毫秒 3072位密钥对的平均生成时间:594毫秒 4096位密钥对的平均生成时间:872,4毫秒 5120位密钥对的平均生成时间:3351,4毫秒 6144位密钥对的平均生成时间:4644,

我需要在我的程序中使用基本的RSA加密,所以我使用

例如,我注意到它生成密钥的速度比PyCrypto(Python)快得多

以下是我制定的一些基准:

执行时间(.NET与rsacyptoserviceprovider)

1024位密钥对的平均生成时间:111,2毫秒
2048位密钥对的平均生成时间:394,8毫秒
3072位密钥对的平均生成时间:594毫秒
4096位密钥对的平均生成时间:872,4毫秒
5120位密钥对的平均生成时间:3351,4毫秒
6144位密钥对的平均生成时间:4644,2毫秒
7168位密钥对的平均生成时间:10052,8毫秒
8192位密钥对的平均生成时间:16105,8毫秒

执行时间(Python和PyCrypto)

1024位密钥对的平均生成时间:1.19569948638 s
2048位密钥对的平均生成时间:2.43145552844秒
3072位密钥对的平均生成时间:12.8203488201秒
4096位密钥对的平均生成时间:16.4437521276 s
5120位密钥对的平均生成时间:58.6469382781秒
6144位密钥对的平均生成时间:166.814121751秒
7168位密钥对的平均生成时间:132.13567301 s
8192位密钥对的平均生成时间:218.305013463秒

比率(Python/.NET):

1024位密钥对:10,75
2048位密钥对:6,16
3072位密钥对:21,58
4096位密钥对:18,85
5120位密钥对:17,50
6144位密钥对:35,92
7168位密钥对:13,14
8192位密钥对:13,55

如您所见,Python的速度大约是.NET的17倍。 Python真的很慢还是.NET使用某种优化来生成密钥

测试是在Windows 7 Ultimate 64位和Intel Core i7 2600上运行的

我使用以下代码获得这些结果:

C#

为什么C#在生成密钥方面比Python快得多?

.NET在windows附带的本机实现上使用了一个包装器。@CodesInChaos:我猜Python实现也是本机代码。但是,即使是纯托管C#代码也应该比Python快得多。我在C与C中使用优化的bigint的经验是,如果本机代码使用128位整数,则等效代码与因子3之间的系数小于1.5,因为这些整数在.net中不可用。@GeorgeStocker Python version:C#version:@GuiTeK代码应该在您的问题中;不是在垃圾桶里。
List<List<long>> executionTimes = new List<List<long>>();
int step = 1024;

for (int bits = 1024; bits <= 8192; bits += step) // 1024-bit to 8192-bit key pair
{
    executionTimes.Add(new List<long>());

    for (int i = 0; i < 5; ++i) // Run the test 10 times to compute an average
    {
        Console.WriteLine("Generating " + bits + "-bit key pair...");

        Stopwatch s = Stopwatch.StartNew();
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(bits); // Doesn't actually generate the key pair
        rsa.ExportParameters(true); // Force generation of the key pair
        s.Stop();

        executionTimes[executionTimes.Count - 1].Add(s.ElapsedMilliseconds);

        Console.WriteLine("Key pair generated. Time: " + s.ElapsedMilliseconds.ToString() + " ms\r\n");
    }
}

for (int i = 1; i <= executionTimes.Count; ++i)
    Console.WriteLine("Average generation time for " + (i * step).ToString() + "-bit key pair: " + executionTimes[i - 1].Average().ToString() + " ms");


Console.ReadLine();
import time
from Crypto.PublicKey import RSA

execution_times = list()
step = 1024

for bits in range(1024, 8192 + 1, step): # 1024-bit to 8192-bit key pair
        execution_times.append(list())

        for i in range(5):
                print("Generating " + str(bits) + "-bit key pair...")

                t = time.clock()
                RSA.generate(bits) # Generate key pair
                t = time.clock() - t

                execution_times[len(execution_times) - 1].append(t)

                print("Key pair generated. Time: " + str(t) + " s\r\n");


for i in range(1, len(execution_times) + 1, 1):
        print("Average generation time for " + str(i * step) + "-bit key pair: " + str(sum(execution_times[i - 1]) / len(execution_times[i - 1])) + " s")