小编gsx*_*022的帖子

使用JS中的SJCL和Ruby中的OpenSSL进行椭圆曲线加密

我正在开发一个Web应用程序,它必须能够在服务器端使用ECC加密数据并在浏览器中解密.我发现在JS中唯一可以使用的库是SJCL.但是,由于目前SJCL中的ECC支持似乎有点放弃,我使用了fork,它具有密钥序列化支持和演示,以便于理解.

首先,我在JS中生成一个ECC密钥对:

keypair = sjcl.ecc.elGamal.generateKeys(384, 10);
document.writeln(JSON.stringify(keypair.pub.serialize()));
Run Code Online (Sandbox Code Playgroud)

这输出如下:

{"point":[1110230655,241884220,775655552,-849225963,-883815628,-1984298210,-736346431,1387519594,-1810604283,-1235638489,1333314084,-1219216530,614640565,-1148742381,1038670260,1013716131,758346573,1162278003,1232401864,-1948620456,533899535,-1478577959,1853846180,-1553049184],"curve":384}
Run Code Online (Sandbox Code Playgroud)

然后我尝试将此公钥转换为OpenSSL可理解的格式.

ar = [1110230655,241884220,775655552,-849225963,-883815628,-1984298210,-736346431,1387519594,-1810604283,-1235638489,1333314084,-1219216530,614640565,-1148742381,1038670260,1013716131,758346573,1162278003,1232401864,-1948620456,533899535,-1478577959,1853846180,-1553049184]

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

# opening a public key generated with the openssl cli tool showed a structure like this:
algokey = OpenSSL::ASN1::ObjectId 'id-ecPublicKey'
algovalue = OpenSSL::ASN1::ObjectId 'secp384r1'
algo = OpenSSL::ASN1::Sequence.new [algokey,algovalue]
# for some reason OpenSSL seems …
Run Code Online (Sandbox Code Playgroud)

javascript ruby openssl cryptography sjcl

7
推荐指数
1
解决办法
3369
查看次数

标签 统计

cryptography ×1

javascript ×1

openssl ×1

ruby ×1

sjcl ×1