如何使用Elliptic Curves Diffie-Hellman和JS中的SJCL以及Ruby中的OpenSSL [英] How to use Elliptic Curves Diffie-Hellman with SJCL in JS and OpenSSL in Ruby

查看:126
本文介绍了如何使用Elliptic Curves Diffie-Hellman和JS中的SJCL以及Ruby中的OpenSSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Elliptic-Curves Diffie-Hellman,我想连接SLCL - JS (文档)在客户端和OpenSSL上 - Ruby (文档)

Using Elliptic-Curves Diffie-Hellman, I want to connect SLCL - JS (documentation) on client and OpenSSL - Ruby (documentation) on server.

我发现了一个类似的问题这里但它没有得到真正的回答,它也不是我真正想要的,因为它使用 sjcl.ecc.elGamal.generateKeys(384,10)而我希望使用 sjcl.ecc.curves ['c384'] < - NIST

I found a similar question here but it wasn't really answered properly and it was also not what I am really looking for since it uses sjcl.ecc.elGamal.generateKeys(384, 10) whereas I am hoping of using sjcl.ecc.curves['c384'] <- NIST

尽管如此,我仍然使用并修改了他的代码进行测试,因为我在使用 sjcl.ecc.curves时遇到了问题[ 'c384'] 生成一个公共点密钥,这就是我想出来的。

Nevertheless, I still used and modified his code to test because I had problems using sjcl.ecc.curves['c384'] producing a single public point-key, and this is what I came up with.

//Javascript
keypair = sjcl.ecc.elGamal.generateKeys(384, 10);
console.log(keypair.pub._point.toBits()); //Changed from his serialize()

这输出到

[-1992414123, 638637875, 1917312913, 73389700, -425224557, 743777818, 970253455, 723842951, -1751664279, 982132367, -1949786746, 1067402923, -869929568, 157928816, 1651634060, 1968161300, -216192372, -1858642177, -1345910998, -2128793177, -1325754797, 143080818, 1868787479, -484135391]

将输出用于ruby:

#Ruby
pointArr = [-1992414123, 638637875, 1917312913, 73389700, -425224557, 743777818, 970253455, 723842951, -1751664279, 982132367, -1949786746, 1067402923, -869929568, 157928816, 1651634060, 1968161300, -216192372, -1858642177, -1345910998, -2128793177, -1325754797, 143080818, 1868787479, -484135391]

# ugly bit magic to somehow convert the above array into a proper byte array (in form of a string)
pointStr = [(pointArr.map { |i| (i>=0)?('0'*(8-i.to_s(16).length)+i.to_s(16)):("%08X" % (2**32-1+i+1)) }*'').upcase].pack("H*")

#My modified code
pointInt = pointStr.unpack('B*').first.to_i(2) #Convert BitStr to integer
pointBN = OpenSSL::BN.new(pointInt.to_s, 10) #Int to BigNumber (to be used as param below)

group = OpenSSL::PKey::EC::Group.new('secp384r1') #EC Group to be used

client_pub_point = OpenSSL::PKey::EC::Point.new(group, pointBN)
# ^
# ^ ABOVE'S MY PROBLEM -> OpenSSL::PKey::EC::Point::Error: invalid encoding
# ^

#Server EC: code taken and modified from https://www.ruby-forum.com/topic/3966195 
ec = OpenSSL::PKey::EC.new(group)
ec.generate_key

pub = OpenSSL::PKey::EC.new(group)
pub.public_key = client_pub_point

#Compute Shared Key
shared_key = ec.dh_compute_key(pub.public_key)

puts shared_key.unpack('I>*')

当来自[(link)]的原始代码时,此'put'如下所示(上面 https://www.ruby-forum.com/topic/3966195 )使用

This 'puts' something like below when original code from [(link)] (https://www.ruby-forum.com/topic/3966195) above is used

3747233514
2683763564
475565567
1087119841
857380668
2490387914
3548975947
2348082236
2093543365
1477205987
4289120093
3330807042

这应该是它,但以防万一这是我的测试

That should be it, but just in case here's my test

irb(main):113:0> ec = OpenSSL::PKey::EC.new(group)
=> #<OpenSSL::PKey::EC:0x37f4250>
irb(main):114:0> ec.generate_key
=> #<OpenSSL::PKey::EC:0x37f4250>

irb(main):115:0> pub = OpenSSL::PKey::EC.new(group)
=> #<OpenSSL::PKey::EC:0x374f070>
irb(main):116:0> pub.public_key = ec.public_key
=> #<OpenSSL::PKey::EC::Point:0x37f8090>

irb(main):117:0> pub.public_key.to_bn
=> 7699789176960498967958014210931326569901199635665512831714857096185925821659134057981449113945854620725216613989823482205311316333140754760317456176281271361802541262755346331375041208726203461213190230560617504850860621520632944763
irb(main):119:0> OpenSSL::PKey::EC::Point.new(group, pub.public_key.to_bn)
=> #<OpenSSL::PKey::EC:0x4029f48>

#The ABOVE FORMAT works, unlike the error I got like the following

irb(main):122:0> pointBN
=> 832312614609895991150696681555479456971598284480953722479085426901428295415600048953528780331647571635767075686130334170313461289491500162782258792834115040597490936949579748064005380309022482780162833924377801386781542770068991521
irb(main):123:0> OpenSSL::PKey::EC::Point.new(group, pointBN)
OpenSSL::PKey::EC::Point::Error: invalid encoding

但是比较上面的工作和不工作,似乎总的小数位数是相同的,所以我觉得我有点在右边跟踪,但我真的无法解决这个问题。

But comparing the working and the not-working just above, it seems that total number of decimal digits are the same, so I think I'm somewhat on the right track, but I just couldn't really work it out.

对于那些可能遇到此类问题的人来说,这些是我的参考代码(1) (2) (3) (4) (5)

For those who might run into this kind of problem, these are my reference codes (1) (2) (3) (4) (5)

我坚持这个现在两天似乎没有太多关于这个在网上写的,我找不到任何其他支持椭圆曲线的JS库。任何帮助将不胜感激。

I'm stuck on this for two days now and there seems to be not much written about this on the net, and I couldn't find any other JS library that supports elliptic curve. Any help would be much appreciated.

推荐答案

我终于解决了这个问题! :)

I've finally solved this problem! :)

我使用了不同的Javascript库。如果您正在寻找解决类似问题,请参阅我的另一个问题(链接)

I used a different Javascript library. If you're looking on solving similar problem like this, please refer to the other question of mine (link)

这篇关于如何使用Elliptic Curves Diffie-Hellman和JS中的SJCL以及Ruby中的OpenSSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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