-->

我如何压缩或编码椭圆曲线公钥,并把它在网络上?(How Do i compress or encod

2019-07-29 19:14发布

我开发签署一份文件,并通过网络向应用Server.I正在使用套接字编程在java做它发送的分布式数字签名。 我认为,公共密钥应进行编码或压缩即x和y的值都以某种方式表示为一个单一的二进制数据,并保存在公共登记或network.But我不知道如何做到这一点在Java中。

        // I have class like this 
           public class CryptoSystem{                  

               EllipticCurve ec = new EllipticCurve(new P192());

               //-------------------
               //--------------------

               public ECKeyPair generatekeyPair()
               {
                  return ECKeyPair(ec);

               }


            }    
        // i don't think i have problem in the above


    CryptoSystem crypto = new CryptoSystem();
    ECKeyPair keyPair = crypto.generateKeyPair();
    BigInteger prvKey = keyPair.getPrivateKey();
    ECPoint pubKey = keyPair.getPublicKey();
     // recommend me here to  compress and send it  the public key to a shared network.

我想知道如何编码的公钥和域参数,使签名的验证会对其进行解码,当你通过网络将它们发送到验证ü要去不得不编码为一个字节数组使用it.because 。我是没有用充气城堡供应商。 整个执行ECDSA算法的是我的项目

Answer 1:

椭圆曲线点使用在X9.62中指定的编码几乎总是编码。

它是可选的使用点压缩。 这是微不足道的使用点压缩编码,解码,但压缩点需要更多的工作,所以除非你真的需要保存额外的字节,我不会理会。 让我知道,如果你需要它,我将添加细节。 您可以通过第一个字节,这将是0×02或0×03认识X9.62编码点与点的压缩。

没有点压缩编码是非常简单的:先从0×04(表示没有压缩)。 然后用第一x坐标,则y坐标跟随,在两个零填充的留给了该领域的字节大小:

int qLength = (q.bitLength()+7)/8;
byte[] xArr = toUnsignedByteArray(x);
byte[] yArr = toUnsignedByteArray(y);
byte[] res = new byte[1+2*qLength];
res[0] = 0x04;
System.arraycopy(xArr, 0, res, qLength - xArr.length, xArr.length);
System.arraycopy(yArr, 0, res, 2* qLength - yArr.length, nLength);

解码这当然是微不足道的。



Answer 2:

我敢肯定的是,BC实现使用X9.63编码,所以这将是相当标准化的编码。 您需要将充气城堡供应商添加到您的JRE( Security.addProvider(new BouncyCastleProvider())看到有弹性的文档。

public static void showECKeyEncodings() {

    try {
        KeyPairGenerator kp = KeyPairGenerator.getInstance("ECDSA");
        ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable
                .getParameterSpec("prime192v1");
        kp.initialize(ecSpec);
        KeyPair keyPair = kp.generateKeyPair();

        PrivateKey privKey = keyPair.getPrivate();
        byte[] encodedPrivKey = privKey.getEncoded();
        System.out.println(toHex(encodedPrivKey));

        PublicKey pubKey = keyPair.getPublic();
        byte[] encodedPubKey = pubKey.getEncoded();
        System.out.println(toHex(encodedPubKey));

        KeyFactory kf = KeyFactory.getInstance("ECDSA");
        PublicKey pubKey2 = kf.generatePublic(new X509EncodedKeySpec(encodedPubKey));
        if (Arrays.equals(pubKey2.getEncoded(), encodedPubKey)) {
            System.out.println("That worked for the public key");
        }

        PrivateKey privKey2 = kf.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivKey));
        if (Arrays.equals(privKey2.getEncoded(), encodedPrivKey)) {
            System.out.println("That worked for the private key");
        }

    } catch (GeneralSecurityException e) {
        throw new IllegalStateException(e);
    }

}


文章来源: How Do i compress or encode the Elliptic curve public key and put it over the network?