如何从object-java中检索值

时间:2012-03-12 04:59:29

标签: java object elliptic-curve

我需要知道如何访问对象的值...例如在我的代码中 `

public static void main(String[] args) throws  Exception {
        Security.addProvider(new BouncyCastleProvider());
      BigInteger ZERO=new BigInteger("0");
       int c;
     ECCurve curve = new ECCurve.Fp(
            newBigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b

ECParameterSpec ecSpec = new ECParameterSpec(
           curve,
            curve.decodePoint( Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
            new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDSA", "BC");
kpg.initialize(ecSpec, new SecureRandom());
KeyPair keyPair = kpg.generateKeyPair();
PublicKey pubKey = keyPair.getPublic();
System.out.println(pubKey);
PrivateKey privKey = keyPair.getPrivate();
System.out.println(privKey);`

int y = numNoRange + p; //其中p是privatekey的值..还有我需要添加privatekey值的数字,但private是object,所以我需要知道如何从对象中检索值。谢谢你..

2 个答案:

答案 0 :(得分:0)

如果你知道对象p是什么类型的话;然后只是施展它。然后得到价值 这是一个从double转换为int

的简单示例
double d = 3.5;
int x = (int) d;

答案 1 :(得分:0)

PublicKey是用于表示非对称加密算法的公钥的基类。就其本质而言,它是一种结构,而不是单一的价值。

例如,如果您使用RSA算法,则可以将公钥转换为 RSAPublicKey然后访问modulusexponent

if (pubKey instanceof RSAPublicKey) {
    RSAPublicKey rsaPubKey = (RSAPublicKey)pubKey;
    BigInteger modulus = rsaPubKey.getModulus();
    BigInteger exponent = rsaPubKey.getPublicExponent();
    System.out.println("Modulus " + modulus.toString());
    System.out.println("Exponent " + exponent.toString());
}

对于椭圆曲线加密,该键由两个值组成 - 椭圆曲线的参数affineXaffineY

if (pubKey instanceof ECPublicKey) {
    ECPublicKey ecPubKey = (ECPublicKey)pubKey;
    ECPoint point = ecPubKey.getW();
    BigInteger affineX = point.getAffineX();
    BigInteger affineY = point.getAffineY();
    System.out.println("Affine X " + affineX.toString());
    System.out.println("Affine Y " + affineY.toString());
}

同样可以访问PrivateKey的内部结构。