简体   繁体   English

椭圆曲线密码学限制为20字节

[英]20 Byte limit with Elliptic Curve Cryptography

I've been trying to switch from RSA to ECC in my code. 我一直在尝试从代码中将RSA切换为ECC。 At first, I tried using Bouncycastle, but I was having difficulties getting named curves to work. 最初,我尝试使用Bouncycastle,但在使命名曲线起作用时遇到了困难。 None of the sample code I found was working. 我发现没有示例代码在工作。

So I tried JECC and it worked just fine, except for one problem. 所以我尝试了JECC,除了一个问题,它工作得很好。 When using the "secp256r1" curve, I could only encode 20 bytes, 160 bits, before it gave me an "index out of range" error. 使用“ secp256r1”曲线时,在给我“索引超出范围”错误之前,我只能编码20个字节(160位)。

Is this a limitation with JECC, Elliptic Curves in general, or maybe my own code? 这是JECC,椭圆曲线的一般限制,还是我自己的代码? I tried researching the problem online and can't find any reference to how much data a 256 bit ECC key can encode. 我尝试过在线研究问题,但是找不到关于256位ECC密钥可以编码多少数据的任何参考。 With RSA, I could encode any data smaller then the key used. 使用RSA,我可以对任何小于使用的密钥的数据进行编码。

Additionally, how important is data padding with ECC? 此外,使用ECC进行数据填充有多重要? I couldn't find any information on standard padding practices with ECC. 我找不到有关ECC的标准填充做法的任何信息。

Thank you in advance for your help. 预先感谢您的帮助。

EDIT: Here is my code in case you were wondering. 编辑:这是我的代码,以防万一。 I slightly modified the original JACC code so there aren't any typecasts. 我稍微修改了原始JACC代码,所以没有任何类型转换。

ECCryptoSystem cs = new ECCryptoSystem(new EllipticCurve(new secp256r1()));

t1=System.currentTimeMillis();
ECKey sk = cs.generateKey(); // secure key
ECKey pk = sk.getPublic(); // public key
t2=System.currentTimeMillis();
System.out.println("Generated keys in "+(t2-t1)+"ms.");

for(int c=0;c<10;c++){
    t1=System.currentTimeMillis();
    byte[] s1=args[0].getBytes();
    byte[] s2=cs.encrypt(s1,args[0].length(),pk);
    byte[] s3=cs.decrypt(s2,sk);
    t2=System.currentTimeMillis();

    if(Arrays.equals(s1,s2)){System.out.println("Bad encryption!");}
    if(!Arrays.equals(s1,s3)){System.out.println("Bad decryption!");}

    String decoded = new String(s3, "UTF-8");
    System.out.println("loop "+(c+1)+": \""+decoded+"\" ("+decoded.length()+" Characters) in "+(t2-t1)+"ms.");
}

And here's how I ran it: 这是我的运行方式:

$ java Mecc "This is a good test."
Generated keys in 397ms.
loop 1: "This is a good test." (20 Characters) in 208ms.
loop 2: "This is a good test." (20 Characters) in 107ms.
loop 3: "This is a good test." (20 Characters) in 69ms.
loop 4: "This is a good test." (20 Characters) in 68ms.
loop 5: "This is a good test." (20 Characters) in 73ms.
loop 6: "This is a good test." (20 Characters) in 59ms.
loop 7: "This is a good test." (20 Characters) in 64ms.
loop 8: "This is a good test." (20 Characters) in 58ms.
loop 9: "This is a good test." (20 Characters) in 60ms.
loop 10: "This is a good test." (20 Characters) in 60ms.
$ java Mecc "This is a good test.."
Generated keys in 555ms.
Error: java.lang.ArrayIndexOutOfBoundsException: 20

FYI: You can see how the JIT compiler speeds things up after a few loops. 仅供参考:您可以看到JIT编译器在几次循环后如何加快处理速度。

Another EDIT: I just stepped through the JECC code and found something interesting: 另一个编辑:我刚刚浏览了JECC代码,发现了一些有趣的东西:

hash = MessageDigest.getInstance("SHA-1");
...
byte[] digest = hash.digest();
for(int j = 0; j < numbytes; j++) {
    res[j+ek.mother.getPCS()]=(byte) (input[j]^digest[j]);
}

It seems the data is xored against the hash. 似乎数据已针对哈希进行了异化。 If I change the "SHA-1" to "SHA-256", I no longer get the error. 如果将“ SHA-1”更改为“ SHA-256”,则不会再出现此错误。

Now I'm not a cryptographer, and I'd rather not change the core functionality of JECC, but is this a valid solution? 现在我不是密码学家,并且我不想更改JECC的核心功能,但这是有效的解决方案吗?

The reason why JECC only encrypts 20 bytes of plaintext is that during encryption process it tries to XOR the input with digest value. JECC仅加密20个字节的纯文本的原因是,在加密过程中,JECC尝试对输入与摘要值进行XOR。 The digest length of SHA-1 used by JECC is 160 bits(20 bytes). JECC使用的SHA-1的摘要长度为160位(20字节)。

Also JECC doesn't encrypt the data using ECC, rather it's a prototype of ECIES, which involves generating symmetric key using ECC Diffie Hellman key exchange and than using any custom symmetric encryption algorithm. 同样,JECC不会使用ECC加密数据,而是ECIES的原型,它涉及使用ECC Diffie Hellman密钥交换而不是使用任何自定义对称加密算法来生成对称密钥。

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

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