diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-06-09 10:17:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 10:17:12 +0200 |
commit | addf44cccdc9fd6d9358e617a501ea26ca6af722 (patch) | |
tree | 81a7b7dbf07b4245cac6c190d4faa9b0cab07d1a /src/test/java/org | |
parent | Merge pull request #48 from UnionInternationalCheminsdeFer/1.3.3 (diff) | |
parent | Delete DecodeDB9EuroTicketTest.java (diff) | |
download | UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.tar UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.tar.gz UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.tar.bz2 UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.tar.lz UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.tar.xz UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.tar.zst UIC-barcode-addf44cccdc9fd6d9358e617a501ea26ca6af722.zip |
Diffstat (limited to 'src/test/java/org')
3 files changed, 627 insertions, 0 deletions
diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java new file mode 100644 index 0000000..3a3f7d2 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java @@ -0,0 +1,168 @@ +package org.uic.barcode.test;
+
+import java.io.IOException;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.SignatureException;
+import java.security.spec.ECGenParameterSpec;
+import java.util.Arrays;
+import java.util.zip.DataFormatException;
+
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.Decoder;
+import org.uic.barcode.Encoder;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.api.IData;
+import org.uic.barcode.logger.LoggerFactory;
+import org.uic.barcode.test.utils.Level2TestDataFactory;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.utils.ECKeyEncoder;
+import org.uic.barcode.utils.SecurityUtils;
+
+public class DynamicFrameDoubleSignatureCompressedCurveTest {
+
+ public String signatureAlgorithmOID = null;
+ public String elipticCurve = null;
+ public String keyPairAlgorithmOID = null;
+
+ public KeyPair keyPairLevel1 = null;
+ public KeyPair keyPairLevel2 = null;
+
+ public IUicRailTicket testFCBticket = null;
+
+ public Provider provider = null;
+
+
+ @Before public void initialize() {
+
+ LoggerFactory.setActivateConsoleLog(true);
+
+ signatureAlgorithmOID = Constants.ECDSA_SHA256;
+ keyPairAlgorithmOID = Constants.KG_EC_256;
+ elipticCurve = "secp256r1";
+
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+
+ provider = new BouncyCastleProvider();
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve);
+ keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve);
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(keyPairLevel1 != null);
+
+ assert(keyPairLevel2 != null);
+
+ }
+
+
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID);
+ enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic(), ECKeyEncoder.ENCODING_X962_COMPRESSED);
+ enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+
+ IData level2Data = Level2TestDataFactory.getLevel2SimpleTestData();
+ try {
+ enc.setLevel2Data(level2Data);
+ enc.signLevel2(keyPairLevel2.getPrivate());
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ Decoder dec = null;
+ try {
+ dec = new Decoder(encoded);
+ } catch (IOException e) {
+ assert(false);
+ } catch (EncodingFormatException e) {
+ assert(false);
+ } catch (DataFormatException e) {
+ assert(false);
+ }
+ assert(dec != null);
+
+ int signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(), null);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel2();
+ } catch (Exception e) {
+ assert(false);
+ }
+ assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK);
+
+ IData level2DataDec = dec.getLevel2Data();
+
+ assert(level2Data.getFormat().equals(level2DataDec.getFormat()));
+ assert(Arrays.equals(level2Data.getData(),level2DataDec.getData()));
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+ }
+
+
+ public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{
+
+ //ECNamedCurveGenParameterSpec namedParamSpec = new ECNamedCurveGenParameterSpec(elipticCurve);
+
+ ECGenParameterSpec namedParamSpec = new ECGenParameterSpec(elipticCurve);
+ KeyPairGenerator ecKPGen = KeyPairGenerator.getInstance("EC", "BC");
+ ecKPGen.initialize(namedParamSpec, new SecureRandom());
+ KeyPair keyPair = ecKPGen.generateKeyPair();
+ KeyPair kp = new KeyPair(SecurityUtils.convert(keyPair.getPublic(), provider),SecurityUtils.convert(keyPair.getPrivate(), provider));
+ return kp;
+ }
+
+
+}
diff --git a/src/test/java/org/uic/barcode/test/ECKeyEncoderTest.java b/src/test/java/org/uic/barcode/test/ECKeyEncoderTest.java new file mode 100644 index 0000000..df8807e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/ECKeyEncoderTest.java @@ -0,0 +1,227 @@ +package org.uic.barcode.test;
+
+import java.math.BigInteger;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.interfaces.ECPublicKey;
+import java.security.spec.ECGenParameterSpec;
+import java.util.Arrays;
+
+import org.bouncycastle.asn1.sec.SECNamedCurves;
+import org.bouncycastle.asn1.x9.ECNamedCurveTable;
+import org.bouncycastle.asn1.x9.X9ECParameters;
+import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
+import org.bouncycastle.crypto.params.ECDomainParameters;
+import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.math.ec.ECCurve;
+import org.bouncycastle.math.ec.ECPoint;
+import org.bouncycastle.util.encoders.Hex;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+import org.uic.barcode.utils.ECKeyEncoder;
+
+public class ECKeyEncoderTest {
+
+ public static Provider provider = null;
+
+ @Before public void initialize() {
+
+ provider = new BouncyCastleProvider();
+ Security.addProvider(provider);
+ }
+
+ @Test public void testEncodeCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp256k1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.10", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = getCompressed(key,name);
+ String compressedHexBC = Hex.toHexString(compressedBC);
+
+ byte[] publicKeyBytes = ECKeyEncoder.getEncoded(key, ECKeyEncoder.ENCODING_X962_COMPRESSED);
+ String compressedHex = Hex.toHexString(publicKeyBytes);
+
+ if (!compressedHexBC.equals(compressedHex)) {
+ assert(compressedHexBC.equals(compressedHex));
+ }
+
+ assert(Arrays.equals(compressedBC, publicKeyBytes));
+
+ assert(compressedHexBC.equals(compressedHex));
+ }
+ }
+
+ @Test public void testDecodeCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp256k1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.10", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = getCompressed(key,name);
+ //String compressedHex = Hex.toHexString(compressedBC);
+
+ PublicKey publicKey = ECKeyEncoder.fromEncoded(compressedBC, "1.3.132.0.10", provider);
+
+ compareKeys((ECPublicKey) publicKey, key);
+
+ }
+ }
+
+ @Test public void testEncodeUnCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp256k1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.10", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ byte[] uncompressedBC = getUnCompressed(key,name);
+ //String uncompressedHex = Hex.toHexString(uncompressedBC);
+
+ byte[] publicKeyUnComp = ECKeyEncoder.getEncoded(key, ECKeyEncoder.ENCODING_X962_UNCOMPESSED);
+
+ assert(Arrays.equals(uncompressedBC, publicKeyUnComp));
+
+ }
+ }
+
+ @Test public void testDecodeUnCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp256k1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.10", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = getUnCompressed(key,name);
+ //String compressedHex = Hex.toHexString(compressedBC);
+
+ PublicKey publicKey = ECKeyEncoder.fromEncoded(compressedBC, "1.3.132.0.10", provider);
+
+ compareKeys((ECPublicKey) publicKey, key);
+
+ }
+ }
+
+ @Test public void testEncodeX509() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp256k1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.10", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ byte[] publicKeyBcX509 = key.getEncoded();
+
+ byte[] publicKeyBytes = ECKeyEncoder.getEncoded(key, ECKeyEncoder.ENCODING_X509);
+ assert(Arrays.equals(publicKeyBcX509, publicKeyBytes));
+
+ }
+ }
+
+ @Test public void testDecodeX509() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp256k1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.10", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = key.getEncoded();
+ //String compressedHex = Hex.toHexString(compressedBC);
+
+ PublicKey publicKey = ECKeyEncoder.fromEncoded(compressedBC, "1.3.132.0.10", provider);
+
+ compareKeys((ECPublicKey) publicKey, key);
+
+ }
+ }
+
+
+
+
+ private ECPublicKey createECPublicKey(String name) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
+ ECKeyPairGenerator gen = new ECKeyPairGenerator();
+ SecureRandom secureRandom = new SecureRandom();
+ X9ECParameters secnamecurves = SECNamedCurves.getByName(name);
+ ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(),
+ secnamecurves.getN(), secnamecurves.getH());
+ ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom);
+ gen.init(keyGenParam);
+
+
+ KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "BC");
+ ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1");
+ kpg.initialize(ecGenParameterSpec);
+ ECPublicKey key = (ECPublicKey) kpg.generateKeyPair().getPublic();
+ return key;
+ }
+
+ private byte[] getCompressed(ECPublicKey key, String name) {
+
+ byte[] x = key.getW().getAffineX().toByteArray();
+ byte[] y = key.getW().getAffineY().toByteArray();
+
+ // assumes that x and y are (unsigned) big endian encoded
+ BigInteger xbi = new BigInteger(1, x);
+ BigInteger ybi = new BigInteger(1, y);
+ X9ECParameters x9 = ECNamedCurveTable.getByName(name);
+ ECCurve curve = x9.getCurve();
+ ECPoint point = curve.createPoint(xbi, ybi);
+
+ return point.getEncoded(true);
+ }
+
+ private byte[] getUnCompressed(ECPublicKey key, String name) {
+
+ byte[] x = key.getW().getAffineX().toByteArray();
+ byte[] y = key.getW().getAffineY().toByteArray();
+
+ // assumes that x and y are (unsigned) big endian encoded
+ BigInteger xbi = new BigInteger(1, x);
+ BigInteger ybi = new BigInteger(1, y);
+ X9ECParameters x9 = ECNamedCurveTable.getByName(name);
+ ECCurve curve = x9.getCurve();
+ ECPoint point = curve.createPoint(xbi, ybi);
+
+ return point.getEncoded(false);
+
+ }
+
+ private void compareKeys(ECPublicKey key1, ECPublicKey key2) {
+
+ assert(key1.getW().getAffineX().equals(key2.getW().getAffineX()) );
+ assert(key1.getW().getAffineY().equals(key2.getW().getAffineY()) );
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/test/ECKeyEncoderTest521.java b/src/test/java/org/uic/barcode/test/ECKeyEncoderTest521.java new file mode 100644 index 0000000..d70e38e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/ECKeyEncoderTest521.java @@ -0,0 +1,232 @@ +package org.uic.barcode.test;
+
+import java.math.BigInteger;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.interfaces.ECPublicKey;
+import java.security.spec.ECGenParameterSpec;
+import java.util.Arrays;
+
+import org.bouncycastle.asn1.sec.SECNamedCurves;
+import org.bouncycastle.asn1.x9.ECNamedCurveTable;
+import org.bouncycastle.asn1.x9.X9ECParameters;
+import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
+import org.bouncycastle.crypto.params.ECDomainParameters;
+import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.math.ec.ECCurve;
+import org.bouncycastle.math.ec.ECPoint;
+import org.bouncycastle.util.encoders.Hex;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+import org.uic.barcode.utils.ECKeyEncoder;
+
+public class ECKeyEncoderTest521 {
+
+ public static Provider provider = null;
+
+ @Before public void initialize() {
+
+ provider = new BouncyCastleProvider();
+ Security.addProvider(provider);
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+ }
+
+ @Test public void testEncodeCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp521r1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = getCompressed(key,name);
+ String compressedHexBC = Hex.toHexString(compressedBC);
+
+ byte[] publicKeyBytes = ECKeyEncoder.getEncoded(key, ECKeyEncoder.ENCODING_X962_COMPRESSED);
+ String compressedHex = Hex.toHexString(publicKeyBytes);
+
+ if (!compressedHexBC.equals(compressedHex)) {
+ assert(compressedHexBC.equals(compressedHex));
+ }
+
+ assert(Arrays.equals(compressedBC, publicKeyBytes));
+
+ assert(compressedHexBC.equals(compressedHex));
+ }
+ }
+
+ @Test public void testDecodeCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp521r1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = getCompressed(key,name);
+
+ PublicKey publicKey = ECKeyEncoder.fromEncoded(compressedBC, "1.3.132.0.35", provider);
+
+ compareKeys((ECPublicKey) publicKey, key);
+
+ }
+ }
+
+ @Test public void testEncodeUnCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp521r1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ byte[] uncompressedBC = getUnCompressed(key,name);
+ //String uncompressedHex = Hex.toHexString(uncompressedBC);
+
+ byte[] publicKeyUnComp = ECKeyEncoder.getEncoded(key, ECKeyEncoder.ENCODING_X962_UNCOMPESSED);
+
+ if (!Arrays.equals(uncompressedBC, publicKeyUnComp)) {
+ assert(Arrays.equals(uncompressedBC, publicKeyUnComp));
+ }
+
+ assert(Arrays.equals(uncompressedBC, publicKeyUnComp));
+
+ }
+ }
+
+ @Test public void testDecodeUnCompressed() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp521r1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = getUnCompressed(key,name);
+ //String compressedHex = Hex.toHexString(compressedBC);
+
+ PublicKey publicKey = ECKeyEncoder.fromEncoded(compressedBC, "1.3.132.0.35", provider);
+
+ compareKeys((ECPublicKey) publicKey, key);
+
+ }
+ }
+
+ @Test public void testEncodeX509() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp521r1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ byte[] publicKeyBcX509 = key.getEncoded();
+
+ byte[] publicKeyBytes = ECKeyEncoder.getEncoded(key, ECKeyEncoder.ENCODING_X509);
+ assert(Arrays.equals(publicKeyBcX509, publicKeyBytes));
+
+ }
+ }
+
+ @Test public void testDecodeX509() throws Exception {
+
+ for (int i = 0; i < 10; i++) {
+
+ String name = "secp521r1";
+
+ AlgorithmNameResolver.addMap(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, "1.3.132.0.35", "EC");
+
+ ECPublicKey key = createECPublicKey(name);
+
+ // some additional encoding tricks
+ byte[] compressedBC = key.getEncoded();
+ //String compressedHex = Hex.toHexString(compressedBC);
+
+ PublicKey publicKey = ECKeyEncoder.fromEncoded(compressedBC, "1.3.132.0.35", provider);
+
+ compareKeys((ECPublicKey) publicKey, key);
+
+ }
+ }
+
+
+
+
+ private ECPublicKey createECPublicKey(String name) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
+ ECKeyPairGenerator gen = new ECKeyPairGenerator();
+ SecureRandom secureRandom = new SecureRandom();
+ X9ECParameters secnamecurves = SECNamedCurves.getByName(name);
+ ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(),
+ secnamecurves.getN(), secnamecurves.getH());
+ ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom);
+ gen.init(keyGenParam);
+
+
+ KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "BC");
+ ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp521r1");
+ kpg.initialize(ecGenParameterSpec);
+ ECPublicKey key = (ECPublicKey) kpg.generateKeyPair().getPublic();
+ return key;
+ }
+
+ private byte[] getCompressed(ECPublicKey key, String name) {
+
+ byte[] x = key.getW().getAffineX().toByteArray();
+ byte[] y = key.getW().getAffineY().toByteArray();
+
+ // assumes that x and y are (unsigned) big endian encoded
+ BigInteger xbi = new BigInteger(1, x);
+ BigInteger ybi = new BigInteger(1, y);
+ X9ECParameters x9 = ECNamedCurveTable.getByName(name);
+ ECCurve curve = x9.getCurve();
+ ECPoint point = curve.createPoint(xbi, ybi);
+
+ return point.getEncoded(true);
+ }
+
+ private byte[] getUnCompressed(ECPublicKey key, String name) {
+
+ byte[] x = key.getW().getAffineX().toByteArray();
+ byte[] y = key.getW().getAffineY().toByteArray();
+
+ // assumes that x and y are (unsigned) big endian encoded
+ BigInteger xbi = new BigInteger(1, x);
+ BigInteger ybi = new BigInteger(1, y);
+ X9ECParameters x9 = ECNamedCurveTable.getByName(name);
+ ECCurve curve = x9.getCurve();
+ ECPoint point = curve.createPoint(xbi, ybi);
+
+ return point.getEncoded(false);
+
+ }
+
+ private void compareKeys(ECPublicKey key1, ECPublicKey key2) {
+
+ assert(key1.getW().getAffineX().equals(key2.getW().getAffineX()) );
+ assert(key1.getW().getAffineY().equals(key2.getW().getAffineY()) );
+
+ }
+
+}
|