org.bouncycastle.pqc.math.linearalgebra.ByteUtils Java Examples

The following examples show how to use org.bouncycastle.pqc.math.linearalgebra.ByteUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SRPAssistant.java    From InflatableDonkey with MIT License 6 votes vote down vote up
public static byte[] generateM1(
        Digest digest,
        BigInteger N,
        BigInteger g,
        byte[] ephemeralKeyA,
        byte[] ephemeralKeyB,
        byte[] key,
        byte[] salt, // s
        byte[] identity) {

    // M1 = H(H(N) XOR H(g) | H(I) | s | A | B | K) 
    int length = length(N);

    // hI = H(I)
    byte[] hI = hash(digest, identity);

    // tmp = H(N) XOR H(g)
    byte[] hNxhG = ByteUtils.xor(hash(digest, padded(N, length)), hash(digest, padded(g, length)));

    return hash(digest, hNxhG, hI, salt, ephemeralKeyA, ephemeralKeyB, key);
}
 
Example #2
Source File: SM2PrivateKeyTest.java    From gmhelper with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncoded() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
    KeyPair keyPair = SM2Util.generateKeyPair();
    BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();
    SM2PublicKey sm2PublicKey = new SM2PublicKey(publicKey.getAlgorithm(), publicKey);
    SM2PrivateKey sm2PrivateKey1 = new SM2PrivateKey(privateKey, publicKey);
    SM2PrivateKey sm2PrivateKey2 = new SM2PrivateKey(privateKey, sm2PublicKey);
    String nativePriDER = ByteUtils.toHexString(privateKey.getEncoded());
    String sm2PriDER1 = ByteUtils.toHexString(sm2PrivateKey1.getEncoded());
    String sm2PriDER2 = ByteUtils.toHexString(sm2PrivateKey2.getEncoded());
    if (nativePriDER.equalsIgnoreCase(sm2PriDER1)) {
        Assert.fail();
    }
    if (!sm2PriDER1.equalsIgnoreCase(sm2PriDER2)) {
        Assert.fail();
    }
    System.out.println("Native EC Private Key DER:\n" + nativePriDER.toUpperCase());
    System.out.println("SM2 EC Private Key DER:\n" + sm2PriDER1.toUpperCase());
}
 
Example #3
Source File: EncryptionUtil.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
public static String getSha3Hash(String data) {
    String trimmedData = trimNewLines(data);
    byte[] dataBytes = trimmedData.getBytes();
    SHA3Digest md = new SHA3Digest(256);
    md.reset();
    md.update(dataBytes, 0, dataBytes.length);
    byte[] hashedBytes = new byte[256 / 8];
    md.doFinal(hashedBytes, 0);
    String sha3Hash = ByteUtils.toHexString(hashedBytes);
    return sha3Hash;
}
 
Example #4
Source File: EncryptionUtil.java    From Groza with Apache License 2.0 6 votes vote down vote up
public static String getSha3Hash(String data) {
    String trimmedData = trimNewLines(data);
    byte[] dataBytes = trimmedData.getBytes();
    SHA3Digest md = new SHA3Digest(256);
    md.reset();
    md.update(dataBytes, 0, dataBytes.length);
    byte[] hashedBytes = new byte[256 / 8];
    md.doFinal(hashedBytes, 0);
    String sha3Hash = ByteUtils.toHexString(hashedBytes);
    return sha3Hash;
}
 
Example #5
Source File: BCECUtilTest.java    From gmhelper with Apache License 2.0 6 votes vote down vote up
@Test
public void testECPrivateKeyPKCS8() {
    try {
        AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter();
        ECPrivateKeyParameters priKeyParams = (ECPrivateKeyParameters) keyPair.getPrivate();
        ECPublicKeyParameters pubKeyParams = (ECPublicKeyParameters) keyPair.getPublic();
        byte[] pkcs8Bytes = BCECUtil.convertECPrivateKeyToPKCS8(priKeyParams, pubKeyParams);
        BCECPrivateKey priKey = BCECUtil.convertPKCS8ToECPrivateKey(pkcs8Bytes);

        byte[] sign = SM2Util.sign(priKey, GMBaseTest.WITH_ID, GMBaseTest.SRC_DATA);
        System.out.println("SM2 sign with withId result:\n" + ByteUtils.toHexString(sign));
        boolean flag = SM2Util.verify(pubKeyParams, GMBaseTest.WITH_ID, GMBaseTest.SRC_DATA, sign);
        if (!flag) {
            Assert.fail("[withId] verify failed");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
 
Example #6
Source File: GMTest.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testSM2EncryptAndDecrypt() {
    try {
        SM2KeyPair keyPair = SM2Util.generateKeyPair();

        byte[] encryptedData = SM2Util.encrypt(SRC_DATA_24B, keyPair.getPublicKey());
        System.out.println("SM2 encrypt result:\n" + ByteUtils.toHexString(encryptedData));
        String decryptedData = SM2Util.decrypt(encryptedData, keyPair.getPrivateKey());
        System.out.println("SM2 decrypt result:\n" + decryptedData);
        if (!decryptedData.equals(SRC_DATA_24B)) {
            Assert.fail();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #7
Source File: GMTest.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testSM2SignAndVerify() {
    try {
        SM2KeyPair keyPair = SM2Util.generateKeyPair();

        byte[] sign = SM2Util.sign(SRC_DATA, WITH_ID, keyPair);
        System.out.println("SM2 sign with withId result:\n" + ByteUtils.toHexString(sign));
        boolean flag = SM2Util.verify(SRC_DATA, sign, WITH_ID, keyPair.getPublicKey());
        if (!flag) {
            Assert.fail("verify failed");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #8
Source File: TransactionUtil.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 
 * @description 校验地址是否符合规则
 * @param address
 *            地址
 * @return 校验结果
 */
public static boolean validAddress(String address) {
	try {
		byte[] decodeBytes = Base58Util.decode(address);
		byte[] checkByteByte = ByteUtils.subArray(decodeBytes, decodeBytes.length - 4);
		byte[] noCheckByte = ByteUtils.subArray(decodeBytes, 0, decodeBytes.length - 4);
		byte[] sha256 = Sha256(noCheckByte);
		byte[] twice = Sha256(sha256);
		for (int i = 0; i < 4; i++) {
			if (twice[i] != checkByteByte[i]) {
				return false;
			}
		}
		return true;
	} catch (Exception e) {
		return false;
	}
}
 
Example #9
Source File: SM2UtilTest.java    From gmhelper with Apache License 2.0 6 votes vote down vote up
@Test
public void testSM2KeyRecovery() {
    try {
        String priHex = "5DD701828C424B84C5D56770ECF7C4FE882E654CAC53C7CC89A66B1709068B9D";
        String xHex = "FF6712D3A7FC0D1B9E01FF471A87EA87525E47C7775039D19304E554DEFE0913";
        String yHex = "F632025F692776D4C13470ECA36AC85D560E794E1BCCF53D82C015988E0EB956";
        String encodedPubHex = "04FF6712D3A7FC0D1B9E01FF471A87EA87525E47C7775039D19304E554DEFE0913F632025F692776D4C13470ECA36AC85D560E794E1BCCF53D82C015988E0EB956";
        String signHex = "30450220213C6CD6EBD6A4D5C2D0AB38E29D441836D1457A8118D34864C247D727831962022100D9248480342AC8513CCDF0F89A2250DC8F6EB4F2471E144E9A812E0AF497F801";
        byte[] signBytes = ByteUtils.fromHexString(signHex);
        byte[] src = ByteUtils.fromHexString("0102030405060708010203040506070801020304050607080102030405060708");
        byte[] withId = ByteUtils.fromHexString("31323334353637383132333435363738");

        ECPrivateKeyParameters priKey = new ECPrivateKeyParameters(
            new BigInteger(ByteUtils.fromHexString(priHex)), SM2Util.DOMAIN_PARAMS);
        ECPublicKeyParameters pubKey = BCECUtil.createECPublicKeyParameters(xHex, yHex, SM2Util.CURVE, SM2Util.DOMAIN_PARAMS);

        if (!SM2Util.verify(pubKey, src, signBytes)) {
            Assert.fail("verify failed");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #10
Source File: SM2UtilTest.java    From gmhelper with Apache License 2.0 6 votes vote down vote up
@Test
public void testSM2KeyGen2() {
    try {
        AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter();
        ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
        ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();

        System.out.println("Pri Hex:"
            + ByteUtils.toHexString(priKey.getD().toByteArray()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineXCoord().getEncoded()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineYCoord().getEncoded()).toUpperCase());
        System.out.println("Pub Point Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getEncoded(false)).toUpperCase());
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #11
Source File: ValidationChecksum.java    From pgpverify-maven-plugin with Apache License 2.0 5 votes vote down vote up
private byte[] calculateChecksum() {
    final SHA256Digest digest = new SHA256Digest();
    final byte[] result = new byte[digest.getDigestSize()];
    for (final Artifact artifact : this.artifacts) {
        final byte[] id = artifact.getId().getBytes(UTF_8);
        digest.update(id, 0, id.length);
        digest.update((byte) '\0');
    }
    digest.doFinal(result, 0);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Checksum of resolved artifacts: {}", ByteUtils.toHexString(result, "0x", ""));
    }
    return result;
}
 
Example #12
Source File: CertUtil.java    From javasdk with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * read pem and convert to address.
 * @param s pem file context
 * @return address
 * @throws Exception -
 */
public static String pemToAddr(String s) throws Exception {
    PemReader pemReader = new PemReader(new StringReader(s));
    PemObject pemObject = pemReader.readPemObject();
    X509CertificateHolder cert = new X509CertificateHolder(pemObject.getContent());
    SubjectPublicKeyInfo pkInfo = cert.getSubjectPublicKeyInfo();
    DERBitString pk = pkInfo.getPublicKeyData();
    byte[] pk64 = ByteUtils.subArray(pk.getBytes(),1);
    return ByteUtils.toHexString(HashUtil.sha3omit12(pk64));
}
 
Example #13
Source File: GMTest.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testSM3() {
    try {
        byte[] hash = SM3Util.hash(SRC_DATA);
        System.out.println("SM3 hash result:\n" + ByteUtils.toHexString(hash));
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }
}
 
Example #14
Source File: SM3UtilTest.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Test
public void testHashAndVerify() {
    try {
        byte[] hash = SM3Util.hash(SRC_DATA);
        System.out.println("SM3 hash result:\n" + ByteUtils.toHexString(hash));
        boolean flag = SM3Util.verify(SRC_DATA, hash);
        if (!flag) {
            Assert.fail();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #15
Source File: SM2UtilTest.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptAndDecrypt_C1C2C3() {
    try {
        AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter();
        ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
        ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();

        System.out.println("Pri Hex:"
            + ByteUtils.toHexString(priKey.getD().toByteArray()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineXCoord().getEncoded()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineYCoord().getEncoded()).toUpperCase());
        System.out.println("Pub Point Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getEncoded(false)).toUpperCase());

        byte[] encryptedData = SM2Util.encrypt(Mode.C1C3C2, pubKey, SRC_DATA_48B);
        System.out.println("SM2 encrypt result:\n" + ByteUtils.toHexString(encryptedData));
        byte[] decryptedData = SM2Util.decrypt(Mode.C1C3C2, priKey, encryptedData);
        System.out.println("SM2 decrypt result:\n" + ByteUtils.toHexString(decryptedData));
        if (!Arrays.equals(decryptedData, SRC_DATA_48B)) {
            Assert.fail();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #16
Source File: SM2UtilTest.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptAndDecrypt() {
    try {
        AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter();
        ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
        ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();

        System.out.println("Pri Hex:"
            + ByteUtils.toHexString(priKey.getD().toByteArray()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineXCoord().getEncoded()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineYCoord().getEncoded()).toUpperCase());
        System.out.println("Pub Point Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getEncoded(false)).toUpperCase());

        byte[] encryptedData = SM2Util.encrypt(pubKey, SRC_DATA_24B);
        System.out.println("SM2 encrypt result:\n" + ByteUtils.toHexString(encryptedData));
        byte[] decryptedData = SM2Util.decrypt(priKey, encryptedData);
        System.out.println("SM2 decrypt result:\n" + ByteUtils.toHexString(decryptedData));
        if (!Arrays.equals(decryptedData, SRC_DATA_24B)) {
            Assert.fail();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #17
Source File: SM2UtilTest.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignAndVerify() {
    try {
        AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter();
        ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
        ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();

        System.out.println("Pri Hex:"
            + ByteUtils.toHexString(priKey.getD().toByteArray()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineXCoord().getEncoded()).toUpperCase());
        System.out.println("Pub X Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getAffineYCoord().getEncoded()).toUpperCase());
        System.out.println("Pub Point Hex:"
            + ByteUtils.toHexString(pubKey.getQ().getEncoded(false)).toUpperCase());

        byte[] sign = SM2Util.sign(priKey, WITH_ID, SRC_DATA);
        System.out.println("SM2 sign with withId result:\n" + ByteUtils.toHexString(sign));
        byte[] rawSign = SM2Util.decodeDERSM2Sign(sign);
        sign = SM2Util.encodeSM2SignToDER(rawSign);
        System.out.println("SM2 sign with withId result:\n" + ByteUtils.toHexString(sign));
        boolean flag = SM2Util.verify(pubKey, WITH_ID, SRC_DATA, sign);
        if (!flag) {
            Assert.fail("verify failed");
        }

        sign = SM2Util.sign(priKey, SRC_DATA);
        System.out.println("SM2 sign without withId result:\n" + ByteUtils.toHexString(sign));
        flag = SM2Util.verify(pubKey, SRC_DATA, sign);
        if (!flag) {
            Assert.fail("verify failed");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #18
Source File: SM4UtilTest.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Test
public void testMac() throws Exception {
    byte[] key = SM4Util.generateKey();
    byte[] iv = SM4Util.generateKey();

    byte[] mac = SM4Util.doCMac(key, SRC_DATA_24B);
    System.out.println("CMAC:\n" + ByteUtils.toHexString(mac).toUpperCase());

    mac = SM4Util.doGMac(key, iv, 16, SRC_DATA_24B);
    System.out.println("GMAC:\n" + ByteUtils.toHexString(mac).toUpperCase());

    byte[] cipher = SM4Util.encrypt_CBC_NoPadding(key, iv, SRC_DATA_32B);
    byte[] cipherLast16 = Arrays.copyOfRange(cipher, cipher.length - 16, cipher.length);
    mac = SM4Util.doCBCMac(key, iv, null, SRC_DATA_32B);
    if (!Arrays.equals(cipherLast16, mac)) {
        Assert.fail();
    }
    System.out.println("CBCMAC:\n" + ByteUtils.toHexString(mac).toUpperCase());

    cipher = SM4Util.encrypt_CBC_Padding(key, iv, SRC_DATA_32B);
    cipherLast16 = Arrays.copyOfRange(cipher, cipher.length - 16, cipher.length);
    mac = SM4Util.doCBCMac(key, iv, SRC_DATA_32B);
    if (!Arrays.equals(cipherLast16, mac)) {
        Assert.fail();
    }
    System.out.println("CBCMAC:\n" + ByteUtils.toHexString(mac).toUpperCase());
}
 
Example #19
Source File: SM2CertUtilTest.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBCECPublicKey() {
    try {
        //当前测试例依赖以下测试例生成的文件,所以先调用一下
        new SM2X509CertMakerTest().testMakeCertificate();

        X509Certificate cert = SM2CertUtil.getX509Certificate("target/test.sm2.cer");
        BCECPublicKey pubKey = SM2CertUtil.getBCECPublicKey(cert);
        byte[] priKeyData = FileUtil.readFile("target/test.sm2.pri");
        ECPrivateKeyParameters priKeyParameters = BCECUtil.convertSEC1ToECPrivateKey(priKeyData);

        byte[] sign = SM2Util.sign(priKeyParameters, GMBaseTest.WITH_ID, GMBaseTest.SRC_DATA);
        System.out.println("SM2 sign with withId result:\n" + ByteUtils.toHexString(sign));
        boolean flag = SM2Util.verify(pubKey, GMBaseTest.WITH_ID, GMBaseTest.SRC_DATA, sign);
        if (!flag) {
            Assert.fail("[withId] verify failed");
        }

        sign = SM2Util.sign(priKeyParameters, GMBaseTest.SRC_DATA);
        System.out.println("SM2 sign without withId result:\n" + ByteUtils.toHexString(sign));
        flag = SM2Util.verify(pubKey, GMBaseTest.SRC_DATA, sign);
        if (!flag) {
            Assert.fail("verify failed");
        }

        byte[] cipherText = SM2Util.encrypt(pubKey, GMBaseTest.SRC_DATA);
        System.out.println("SM2 encrypt result:\n" + ByteUtils.toHexString(cipherText));
        byte[] plain = SM2Util.decrypt(priKeyParameters, cipherText);
        System.out.println("SM2 decrypt result:\n" + ByteUtils.toHexString(plain));
        if (!Arrays.equals(plain, GMBaseTest.SRC_DATA)) {
            Assert.fail("plain not equals the src");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #20
Source File: BCECUtil.java    From littleca with Apache License 2.0 4 votes vote down vote up
public static ECPublicKeyParameters createEcPublicKey(String xHex, String yHex,
                                                      ECCurve curve, ECDomainParameters domainParameters) {
    byte[] xBytes = ByteUtils.fromHexString(xHex);
    byte[] yBytes = ByteUtils.fromHexString(yHex);
    return createEcPublicKey(xBytes, yBytes, curve, domainParameters);
}
 
Example #21
Source File: SM2UtilTest.java    From gmhelper with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyPairEncoding() {
    try {
        AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPairParameter();
        ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
        ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();

        byte[] priKeyPkcs8Der = BCECUtil.convertECPrivateKeyToPKCS8(priKey, pubKey);
        System.out.println("private key pkcs8 der length:" + priKeyPkcs8Der.length);
        System.out.println("private key pkcs8 der:" + ByteUtils.toHexString(priKeyPkcs8Der));
        FileUtil.writeFile("target/ec.pkcs8.pri.der", priKeyPkcs8Der);

        String priKeyPkcs8Pem = BCECUtil.convertECPrivateKeyPKCS8ToPEM(priKeyPkcs8Der);
        FileUtil.writeFile("target/ec.pkcs8.pri.pem", priKeyPkcs8Pem.getBytes("UTF-8"));
        byte[] priKeyFromPem = BCECUtil.convertECPrivateKeyPEMToPKCS8(priKeyPkcs8Pem);
        if (!Arrays.equals(priKeyFromPem, priKeyPkcs8Der)) {
            throw new Exception("priKeyFromPem != priKeyPkcs8Der");
        }

        BCECPrivateKey newPriKey = BCECUtil.convertPKCS8ToECPrivateKey(priKeyPkcs8Der);

        byte[] priKeyPkcs1Der = BCECUtil.convertECPrivateKeyToSEC1(priKey, pubKey);
        System.out.println("private key pkcs1 der length:" + priKeyPkcs1Der.length);
        System.out.println("private key pkcs1 der:" + ByteUtils.toHexString(priKeyPkcs1Der));
        FileUtil.writeFile("target/ec.pkcs1.pri", priKeyPkcs1Der);

        byte[] pubKeyX509Der = BCECUtil.convertECPublicKeyToX509(pubKey);
        System.out.println("public key der length:" + pubKeyX509Der.length);
        System.out.println("public key der:" + ByteUtils.toHexString(pubKeyX509Der));
        FileUtil.writeFile("target/ec.x509.pub.der", pubKeyX509Der);

        String pubKeyX509Pem = BCECUtil.convertECPublicKeyX509ToPEM(pubKeyX509Der);
        FileUtil.writeFile("target/ec.x509.pub.pem", pubKeyX509Pem.getBytes("UTF-8"));
        byte[] pubKeyFromPem = BCECUtil.convertECPublicKeyPEMToX509(pubKeyX509Pem);
        if (!Arrays.equals(pubKeyFromPem, pubKeyX509Der)) {
            throw new Exception("pubKeyFromPem != pubKeyX509Der");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }
}
 
Example #22
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public static ECPublicKeyParameters createECPublicKeyParameters(String xHex, String yHex,
    ECCurve curve, ECDomainParameters domainParameters) {
    return createECPublicKeyParameters(ByteUtils.fromHexString(xHex), ByteUtils.fromHexString(yHex),
        curve, domainParameters);
}
 
Example #23
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 2 votes vote down vote up
/**
 * @param xHex             十六进制形式的公钥x分量,如果是SM2算法,Hex字符串长度应该是64(即32字节)
 * @param yHex             十六进制形式的公钥y分量,如果是SM2算法,Hex字符串长度应该是64(即32字节)
 * @param curve            EC曲线参数,一般是固定的,如果是SM2算法的可参考{@link SM2Util#CURVE}
 * @param domainParameters EC Domain参数,一般是固定的,如果是SM2算法的可参考{@link SM2Util#DOMAIN_PARAMS}
 * @return
 */
public static ECPublicKeyParameters createECPublicKeyParameters(
        String xHex, String yHex, ECCurve curve, ECDomainParameters domainParameters) {
    return createECPublicKeyParameters(ByteUtils.fromHexString(xHex), ByteUtils.fromHexString(yHex),
            curve, domainParameters);
}
 
Example #24
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 2 votes vote down vote up
/**
 * @param dHex             十六进制字符串形式的私钥d值,如果是SM2算法,Hex字符串长度应该是64(即32字节)
 * @param domainParameters EC Domain参数,一般是固定的,如果是SM2算法的可参考{@link SM2Util#DOMAIN_PARAMS}
 * @return
 */
public static ECPrivateKeyParameters createECPrivateKeyParameters(
        String dHex, ECDomainParameters domainParameters) {
    return createECPrivateKeyParameters(ByteUtils.fromHexString(dHex), domainParameters);
}