简体   繁体   中英

Dynamic bouncycastle provider registration failure

For my tasks I need to have very small test project, that would just do some ECC operations.

For this I've decided to use bouncycastle. Since there is quite a lot of java snippets I was surprised, that dynamic provider adding not working as expected.

Here is full code, and this is how it looks in IJ:

在此处输入图片说明

So, the question is: how do I resolve all needed functions, especially addProvider ?

I'm using maven for this project and here is my pom.xml

Update:

Error:(11, 29) java: <identifier> expected
Error:(11, 30) java: illegal start of type
Error:(11, 33) java: ')' expected
Error:(11, 54) java: ';' expected
Error:(11, 55) java: illegal start of type
Error:(11, 56) java: <identifier> expected

As @Vagelis comments you're missing main method, and some imports.

Also java.security.Security.addProvider() is in the jdk itself so there is no extra dependency needed in the pom.xml .

Your code corrected could be:

import java.math.BigInteger;
import java.security.Security;
import java.security.spec.ECFieldFp;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.ECPublicKeySpec;
import java.security.spec.EllipticCurve;

import org.bouncycastle.jce.ECPointUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public class Main {

        public static void main(String args[]){

            Security.addProvider(new BouncyCastleProvider());
            EllipticCurve curve = new EllipticCurve(
            new ECFieldFp(new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839")), // q
            new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
            new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b
            ECParameterSpec spec = new ECParameterSpec(
            curve,
            ECPointUtil.decodePoint(curve, Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
            new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"), // n
            1); // h

            ECPrivateKeySpec priKey = new ECPrivateKeySpec(
            new BigInteger("876300101507107567501066130761671078357010671067781776716671676178726717"), // d
            spec);
            ECPublicKeySpec pubKey = new ECPublicKeySpec(
            ECPointUtil.decodePoint(curve, Hex.decode("025b6dc53bc61a2548ffb0f671472de6c9521a9d2d2534e65abfcbd5fe0c70")), // Q
            spec);
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM