我将实施RSA加密。我想知道。加密比RSA加密中的解密快多少倍。我通过使用System.currentTimeMillis()尝试在java中计算经过的时间;但是给我时间加密= 0.05毫秒,而时间解密0.55毫秒意味着从1:11。我认为这个结果不合理我的代码是以下
//here my key has 256 bits
for (;;) {
long begin = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
decrypt();
}
long end = System.currentTimeMillis();
long time = end - begin;
if (time >= 10000) {
System.out.printf("Average Encryption takes: %.2f ms\n",
(double) time / num);
break;
}
num *= 2;
}
p = BigInteger.probablePrime(128, random);
q = BigInteger.probablePrime(128, random);
N = (p.subtract(one)).multiply(q.subtract(one));
e = BigInteger.probablePrime(32, random);
d = e.modInverse(N);
private void encrypt()
{
C= M.modPow(e,N);
}
private void decrypt()
{
RM = C.modPow(d, N);
}
请对这些结果的任何解释
答案 0 :(得分:5)
请不要自己实施RSA,这样做很容易做错,编写版本需要几个月才能抵抗3-4次旧的加密攻击。
All the crypto code you’ve ever written is probably broken - Tony Arcieri
RSA encryption is more difficult. The 'best practice' in implementing RSA is: don't implement RSA. Other people have done it better than you can. - 马修格林(约翰霍普金斯大学)
Why Cryptography Is Harder Than It Looks - Bruce Schneier,1997:
大多数系统不是与密码学家一起设计和实现的,而是由将密码学视为另一个组件的工程师设计和实现的。不是。
在RSA的工业实施中,使用某人的公钥进行加密更快然后使用私钥进行解密,因为公钥通常具有短公共指数e
,通常65537(0x10001)。当使用快速取幂(名为Exponentiation_by_squaring的方法)时,情况确实如此。 此操作的时间线性地取决于位长度,线性取决于1
位计数指数值,长度和计数都小于65537(17位长度和2位处于状态{{ 1}})。
在类似RSA操作的伪代码中, 1
为32位且 通常短于e
,因此使用d
指数的操作比使用e
的操作更快。