【问题标题】:when I am executing the code only public key is generated but not the private key ,so what should I do?当我执行代码时只生成公钥而不生成私钥,那我该怎么办?
【发布时间】:2015-05-18 12:13:15
【问题描述】:
package com.project;
import java.security.*;
import java.security.spec.*;

public class ECCKeyGeneration {
  public static void main(String[] args) throws Exception {
    KeyPairGenerator kpg;
    kpg = KeyPairGenerator.getInstance("EC","SunEC");
    ECGenParameterSpec ecsp;
    ecsp = new ECGenParameterSpec("secp192r1");
    kpg.initialize(ecsp);

    KeyPair kp = kpg.genKeyPair();
    PrivateKey privKey = kp.getPrivate();
    PublicKey pubKey = kp.getPublic();

    System.out.println(privKey.toString());
    System.out.println(pubKey.toString());
  }
}

[2] 这是用于生成公钥和私钥的椭圆曲线密码学的代码,但是当我执行此代码时,只显示公钥而不显示私钥,所以请帮帮我,让我知道该怎么做生成私钥!!

【问题讨论】:

  • 编写生成私钥的代码?由于我们不知道该代码的作用,我们不知道类,我们可以假设 getPrivate() 的实现类似于:return "";
  • @Stultuske 请冷静...KeyPair是一个标准的java对象...检查一下here
  • 另找here方法getPrivate()
  • 执行时显示 --> Main.java:4: error: class ECCKeyGeneration is public, 应在名为 ECCKeyGeneration.java 的文件中声明 public class ECCKeyGeneration { ^ 1 error //so what to怎么办??
  • 将您的文件命名为 ECCKeyGeneration.java,并带有 try catch 块。然后编译并运行文件。我试过你的代码,它可以工作并提供私钥和公钥。

标签: java elliptic-curve


【解决方案1】:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;

public class ECCKeyGeneration {

    public static void main(String[] args)  {
        try {
            KeyPairGenerator kpg;
            kpg = KeyPairGenerator.getInstance("EC","SunEC");
            ECGenParameterSpec ecsp;
            ecsp = new ECGenParameterSpec("secp192r1");
            kpg.initialize(ecsp);

            KeyPair kp = kpg.genKeyPair();
            PrivateKey privKey = kp.getPrivate();
            PublicKey pubKey = kp.getPublic();

            System.out.println(privKey.toString());
            System.out.println(pubKey.toString());
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

我刚刚测试了上面的代码。

输出

Sun EC 私钥,192 位 私有值: 3248833611418544793834748156439256292267803494576663573112
参数:secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1) Sun EC 公钥,192 位 公共 x 坐标: 5122655651118956061783347731888893733494103991283417332818 公众号 协调:223043343028867724454216740788693823451155477884918709166
参数:secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1)

你得到相同的输出吗?

【讨论】:

  • sun.security.ec.ECPrivateKeyImpl@ffffdf17 Sun EC public key, 192 bits public x coord: 2986437850845590154783978576780601064801052697129613842435 public y coord: 4073070570923625270823330364164716679062682143264492286968 parameters: secp192r1 [NIST P-192, X9.62 prime192v1] (1.2 .840.10045.3.1.1)。 @xavy:我没有得到你上面提到的私人价值。
  • 在私钥之后, 来了,但不是你上面提到的值
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 2017-08-14
相关资源
最近更新 更多