Java Code Examples for org.bouncycastle.math.ec.ECPoint#multiply()

The following examples show how to use org.bouncycastle.math.ec.ECPoint#multiply() . 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: SM2KeyGenerator.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public KeyPair generateKeyPair(String privKey) {
    SM2PrivateKey privateKey = new SM2PrivateKey(new BigInteger(privKey, 16));
    byte[] privateKeyBytes = privateKey.getEncoded();
    privateKey.setD(new BigInteger(Hex.toHexString(privateKeyBytes), 16));
    try {
        ECPoint g = ecdp.getG();
        ECPoint p = g.multiply(privateKey.getD());
        // System.out.println("===d:" + privateKey.getD().toString(16));
        SM2PublicKey publicKey = new SM2PublicKey(p);
        return new KeyPair(publicKey, privateKey);
    } catch (Exception e) {
        System.out.println(
                "====generate keypair from priv key failed, error msg:" + e.getMessage());
        return null;
    }
}
 
Example 2
Source File: ECDSA.java    From bushido-java-core with GNU General Public License v3.0 6 votes vote down vote up
private ECDSASignature findSignature(BigInteger d, BigInteger e) throws Exception {
    BigInteger N = key.params.getN();
    ECPoint G = key.params.getG();
    int badrs = 0;
    BigInteger k;
    ECPoint Q;
    BigInteger r;
    BigInteger s;
    do {
        if (this.k.equals(BigInteger.ZERO) == true || badrs > 0) {
            this.deterministicK(badrs);
        }
        badrs++;
        k = this.k;
        Q = G.multiply(k);
        Q = Q.normalize();
        r = Q.getAffineXCoord().toBigInteger().mod(N);
        s = k.modInverse(N).multiply(e.add(d.multiply(r))).mod(N);
    } while (r.compareTo(BigInteger.ZERO) <= 0 || s.compareTo(BigInteger.ZERO) <= 0);

    s = toLowS(s);
    return new ECDSASignature(r, s, key.isCompressed());
}
 
Example 3
Source File: TrustAddressGenerator.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
public static String preimageToAddress(byte[] preimage) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    Security.addProvider(new BouncyCastleProvider());

    // get the hash of the preimage text
    Keccak.Digest256 digest = new Keccak.Digest256();
    digest.update(preimage);
    byte[] hash = digest.digest();

    // use the hash to derive a new address
    BigInteger keyDerivationFactor = new BigInteger(Numeric.toHexStringNoPrefix(hash), 16);
    ECPoint donatePKPoint = extractPublicKey(decodeKey(masterPubKey));
    ECPoint digestPKPoint = donatePKPoint.multiply(keyDerivationFactor);
    return getAddress(digestPKPoint);
}
 
Example 4
Source File: PushService.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
private boolean verifyKeyPair() {
    ECNamedCurveParameterSpec curveParameters = ECNamedCurveTable.getParameterSpec(Utils.CURVE);
    ECPoint g = curveParameters.getG();
    ECPoint sG = g.multiply(((ECPrivateKey) privateKey).getS());

    return sG.equals(((ECPublicKey) publicKey).getQ());
}
 
Example 5
Source File: SM2KeyGenerator.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public KeyPair generateKeyPair() {
    SM2PrivateKey privateKey = generatePrivateKey();
    ECPoint g = ecdp.getG();
    ECPoint p = g.multiply(privateKey.getD());
    SM2PublicKey publicKey = new SM2PublicKey(p);
    return new KeyPair(publicKey, privateKey);
}
 
Example 6
Source File: Cipher.java    From nuls with MIT License 5 votes vote down vote up
public ECPoint initEnc(SM2 sm2, ECPoint userKey) {
    AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.generateKeyPair();
    ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) key.getPrivate();
    ECPublicKeyParameters ecpub = (ECPublicKeyParameters) key.getPublic();
    BigInteger k = ecpriv.getD();
    ECPoint c1 = ecpub.getQ();
    this.p2 = userKey.multiply(k);
    reset();
    return c1;
}
 
Example 7
Source File: Utils.java    From webpush-java with MIT License 5 votes vote down vote up
/**
 * Verify that the private key belongs to the public key.
 *
 * @param privateKey
 * @param publicKey
 * @return
 */
public static boolean verifyKeyPair(PrivateKey privateKey, PublicKey publicKey) {
    ECNamedCurveParameterSpec curveParameters = ECNamedCurveTable.getParameterSpec(CURVE);
    ECPoint g = curveParameters.getG();
    ECPoint sG = g.multiply(((java.security.interfaces.ECPrivateKey) privateKey).getS());

    return sG.equals(((ECPublicKey) publicKey).getQ());
}
 
Example 8
Source File: SM2Tool.java    From ID-SDK with Apache License 2.0 4 votes vote down vote up
/**
 * 公钥加密
 * 
 * @param input
 *            加密原文
 * @param publicKey
 *            公钥
 * @return
 */
public byte[] encrypt(String input, ECPoint publicKey) {
	byte[] inputBuffer = input.getBytes();
	if (debug)
		printHexString(inputBuffer);

	byte[] C1Buffer;
	ECPoint kpb;
	byte[] t;
	do {
		/* 1 产生随机数k,k属于[1, n-1] */
		BigInteger k = random(n);
		if (debug) {
			System.out.print("k: ");
			printHexString(k.toByteArray());
		}

		/* 2 计算椭圆曲线点C1 = [k]G = (x1, y1) */
		ECPoint C1 = G.multiply(k);
		C1Buffer = C1.getEncoded(false);
		if (debug) {
			System.out.print("C1: ");
			printHexString(C1Buffer);
		}

		/*
		 * 3 计算椭圆曲线点 S = [h]Pb
		 */
		BigInteger h = ecc_bc_spec.getH();
		if (h != null) {
			ECPoint S = publicKey.multiply(h);
			if (S.isInfinity())
				throw new IllegalStateException();
		}

		/* 4 计算 [k]PB = (x2, y2) */
		kpb = publicKey.multiply(k).normalize();

		/* 5 计算 t = KDF(x2||y2, klen) */
		byte[] kpbBytes = kpb.getEncoded(false);
		t = KDF(kpbBytes, inputBuffer.length);
	} while (allZero(t));

	/* 6 计算C2=M^t */
	byte[] C2 = new byte[inputBuffer.length];
	for (int i = 0; i < inputBuffer.length; i++) {
		C2[i] = (byte) (inputBuffer[i] ^ t[i]);
	}

	/* 7 计算C3 = Hash(x2 || M || y2) */
	byte[] C3 = sm3hash(kpb.getXCoord().toBigInteger().toByteArray(), inputBuffer,
			kpb.getYCoord().toBigInteger().toByteArray());

	/* 8 输出密文 C=C1 || C2 || C3 */

	byte[] encryptResult = new byte[C1Buffer.length + C2.length + C3.length];

	System.arraycopy(C1Buffer, 0, encryptResult, 0, C1Buffer.length);
	System.arraycopy(C2, 0, encryptResult, C1Buffer.length, C2.length);
	System.arraycopy(C3, 0, encryptResult, C1Buffer.length + C2.length, C3.length);

	if (debug) {
		System.out.print("密文: ");
		printHexString(encryptResult);
	}

	return encryptResult;
}
 
Example 9
Source File: SM2Tool.java    From ID-SDK with Apache License 2.0 4 votes vote down vote up
/**
 * 私钥解密
 * 
 * @param encryptData
 *            密文数据字节数组
 * @param privateKey
 *            解密私钥
 * @return
 */
public String decrypt(byte[] encryptData, BigInteger privateKey) {

	if (debug)
		System.out.println("encryptData length: " + encryptData.length);

	byte[] C1Byte = new byte[65];
	System.arraycopy(encryptData, 0, C1Byte, 0, C1Byte.length);

	ECPoint C1 = curve.decodePoint(C1Byte).normalize();

	/*
	 * 计算椭圆曲线点 S = [h]C1 是否为无穷点
	 */
	BigInteger h = ecc_bc_spec.getH();
	if (h != null) {
		ECPoint S = C1.multiply(h);
		if (S.isInfinity())
			throw new IllegalStateException();
	}
	/* 计算[dB]C1 = (x2, y2) */
	ECPoint dBC1 = C1.multiply(privateKey).normalize();

	/* 计算t = KDF(x2 || y2, klen) */
	byte[] dBC1Bytes = dBC1.getEncoded(false);
	int klen = encryptData.length - 65 - DIGEST_LENGTH;
	byte[] t = KDF(dBC1Bytes, klen);
	if (allZero(t)) {
		System.err.println("all zero");
		throw new IllegalStateException();
	}

	/* 5 计算M'=C2^t */
	byte[] M = new byte[klen];
	for (int i = 0; i < M.length; i++) {
		M[i] = (byte) (encryptData[C1Byte.length + i] ^ t[i]);
	}
	if (debug)
		printHexString(M);

	/* 6 计算 u = Hash(x2 || M' || y2) 判断 u == C3是否成立 */
	byte[] C3 = new byte[DIGEST_LENGTH];

	if (debug)
		try {
			System.out.println("M = " + new String(M, "UTF8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	System.arraycopy(encryptData, encryptData.length - DIGEST_LENGTH, C3, 0, DIGEST_LENGTH);
	byte[] u = sm3hash(dBC1.getXCoord().toBigInteger().toByteArray(), M,
			dBC1.getYCoord().toBigInteger().toByteArray());
	if (Arrays.equals(u, C3)) {
		if (debug)
			System.out.println("解密成功");
		try {
			return new String(M, "UTF8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	} else {
		if (debug) {
			System.out.print("u = ");
			printHexString(u);
			System.out.print("C3 = ");
			printHexString(C3);
			System.err.println("解密验证失败");
		}
		return null;
	}

}
 
Example 10
Source File: SM2Util.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * 公钥加密
 * 
 * @param input
 *            加密原文
 * @param publicKey
 *            公钥
 * @return
 */
public static byte[] encrypt(String input, ECPoint publicKey) {

	byte[] inputBuffer = input.getBytes();
	byte[] C1Buffer;
	ECPoint kpb;
	byte[] t;
	do {
		/* 1 产生随机数k,k属于[1, n-1] */
		BigInteger k = random(n);

		/* 2 计算椭圆曲线点C1 = [k]G = (x1, y1) */
		ECPoint C1 = G.multiply(k);
		C1Buffer = C1.getEncoded(false);

		/*
		 * 3 计算椭圆曲线点 S = [h]Pb
		 */
		BigInteger h = ecc_bc_spec.getH();
		if (h != null) {
			ECPoint S = publicKey.multiply(h);
			if (S.isInfinity())
				throw new IllegalStateException();
		}

		/* 4 计算 [k]PB = (x2, y2) */
		kpb = publicKey.multiply(k).normalize();

		/* 5 计算 t = KDF(x2||y2, klen) */
		byte[] kpbBytes = kpb.getEncoded(false);
		t = KDF(kpbBytes, inputBuffer.length);
	} while (allZero(t));

	/* 6 计算C2=M^t */
	byte[] C2 = new byte[inputBuffer.length];
	for (int i = 0; i < inputBuffer.length; i++) {
		C2[i] = (byte) (inputBuffer[i] ^ t[i]);
	}

	/* 7 计算C3 = Hash(x2 || M || y2) */
	byte[] C3 = sm3hash(kpb.getXCoord().toBigInteger().toByteArray(), inputBuffer,
			kpb.getYCoord().toBigInteger().toByteArray());

	/* 8 输出密文 C=C1 || C2 || C3 */

	byte[] encryptResult = new byte[C1Buffer.length + C2.length + C3.length];

	System.arraycopy(C1Buffer, 0, encryptResult, 0, C1Buffer.length);
	System.arraycopy(C2, 0, encryptResult, C1Buffer.length, C2.length);
	System.arraycopy(C3, 0, encryptResult, C1Buffer.length + C2.length, C3.length);

	return encryptResult;
}
 
Example 11
Source File: SM2Util.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * 私钥解密
 * 
 * @param encryptData
 *            密文数据字节数组
 * @param privateKey
 *            解密私钥
 * @return
 */
public static String decrypt(byte[] encryptData, BigInteger privateKey) {
	byte[] C1Byte = new byte[65];
	System.arraycopy(encryptData, 0, C1Byte, 0, C1Byte.length);

	ECPoint C1 = curve.decodePoint(C1Byte).normalize();

	/*
	 * 计算椭圆曲线点 S = [h]C1 是否为无穷点
	 */
	BigInteger h = ecc_bc_spec.getH();
	if (h != null) {
		ECPoint S = C1.multiply(h);
		if (S.isInfinity())
			throw new IllegalStateException();
	}
	/* 计算[dB]C1 = (x2, y2) */
	ECPoint dBC1 = C1.multiply(privateKey).normalize();

	/* 计算t = KDF(x2 || y2, klen) */
	byte[] dBC1Bytes = dBC1.getEncoded(false);
	int klen = encryptData.length - 65 - DIGEST_LENGTH;
	byte[] t = KDF(dBC1Bytes, klen);

	if (allZero(t)) {
		System.err.println("all zero");
		throw new IllegalStateException();
	}

	/* 5 计算M'=C2^t */
	byte[] M = new byte[klen];
	for (int i = 0; i < M.length; i++) {
		M[i] = (byte) (encryptData[C1Byte.length + i] ^ t[i]);
	}

	/* 6 计算 u = Hash(x2 || M' || y2) 判断 u == C3是否成立 */
	byte[] C3 = new byte[DIGEST_LENGTH];

	System.arraycopy(encryptData, encryptData.length - DIGEST_LENGTH, C3, 0, DIGEST_LENGTH);
	byte[] u = sm3hash(dBC1.getXCoord().toBigInteger().toByteArray(), M,
			dBC1.getYCoord().toBigInteger().toByteArray());
	if (Arrays.equals(u, C3)) {
		try {
			return new String(M, "UTF8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	} else {
		return null;
	}
}
 
Example 12
Source File: Cipher.java    From nuls with MIT License 4 votes vote down vote up
public void initDec(BigInteger userD, ECPoint c1) {
    this.p2 = c1.multiply(userD);
    reset();
}
 
Example 13
Source File: SM2.java    From protools with Apache License 2.0 4 votes vote down vote up
/**
 * 公钥加密
 *
 * @param input     加密原文
 * @param publicKey 公钥
 * @return
 */
public byte[] encrypt(String input, ECPoint publicKey) {

    byte[] inputBuffer = input.getBytes();

    byte[] C1Buffer;
    ECPoint kpb;
    byte[] t;
    do {
        /* 1 产生随机数k,k属于[1, n-1] */
        BigInteger k = random(n);

        /* 2 计算椭圆曲线点C1 = [k]G = (x1, y1) */
        ECPoint C1 = G.multiply(k);
        C1Buffer = C1.getEncoded(false);

        /*
         * 3 计算椭圆曲线点 S = [h]Pb
         */
        BigInteger h = ecc_bc_spec.getH();
        if (h != null) {
            ECPoint S = publicKey.multiply(h);
            if (S.isInfinity()) {
                throw new IllegalStateException();
            }
        }

        /* 4 计算 [k]PB = (x2, y2) */
        kpb = publicKey.multiply(k).normalize();

        /* 5 计算 t = KDF(x2||y2, klen) */
        byte[] kpbBytes = kpb.getEncoded(false);
        t = KDF(kpbBytes, inputBuffer.length);
        // DerivationFunction kdf = new KDF1BytesGenerator(new
        // ShortenedDigest(new SHA256Digest(), DIGEST_LENGTH));
        //
        // t = new byte[inputBuffer.length];
        // kdf.init(new ISO18033KDFParameters(kpbBytes));
        // kdf.generateBytes(t, 0, t.length);
    } while (allZero(t));

    /* 6 计算C2=M^t */
    byte[] C2 = new byte[inputBuffer.length];
    for (int i = 0; i < inputBuffer.length; i++) {
        C2[i] = (byte) (inputBuffer[i] ^ t[i]);
    }

    /* 7 计算C3 = Hash(x2 || M || y2) */
    byte[] C3 = sm3hash(kpb.getXCoord().toBigInteger().toByteArray(), inputBuffer,
            kpb.getYCoord().toBigInteger().toByteArray());

    /* 8 输出密文 C=C1 || C2 || C3 */

    byte[] encryptResult = new byte[C1Buffer.length + C2.length + C3.length];

    System.arraycopy(C1Buffer, 0, encryptResult, 0, C1Buffer.length);
    System.arraycopy(C2, 0, encryptResult, C1Buffer.length, C2.length);
    System.arraycopy(C3, 0, encryptResult, C1Buffer.length + C2.length, C3.length);

    return encryptResult;
}
 
Example 14
Source File: SM2.java    From protools with Apache License 2.0 4 votes vote down vote up
/**
 * 私钥解密
 *
 * @param encryptData 密文数据字节数组
 * @param privateKey  解密私钥
 * @return
 */
public String decrypt(byte[] encryptData, BigInteger privateKey) {

    byte[] C1Byte = new byte[65];
    System.arraycopy(encryptData, 0, C1Byte, 0, C1Byte.length);

    ECPoint C1 = curve.decodePoint(C1Byte).normalize();

    /*
     * 计算椭圆曲线点 S = [h]C1 是否为无穷点
     */
    BigInteger h = ecc_bc_spec.getH();
    if (h != null) {
        ECPoint S = C1.multiply(h);
        if (S.isInfinity()) {
            throw new IllegalStateException();
        }
    }
    /* 计算[dB]C1 = (x2, y2) */
    ECPoint dBC1 = C1.multiply(privateKey).normalize();

    /* 计算t = KDF(x2 || y2, klen) */
    byte[] dBC1Bytes = dBC1.getEncoded(false);
    int klen = encryptData.length - 65 - DIGEST_LENGTH;
    byte[] t = KDF(dBC1Bytes, klen);
    // DerivationFunction kdf = new KDF1BytesGenerator(new
    // ShortenedDigest(new SHA256Digest(), DIGEST_LENGTH));
    // if (debug)
    // System.out.println("klen = " + klen);
    // kdf.init(new ISO18033KDFParameters(dBC1Bytes));
    // kdf.generateBytes(t, 0, t.length);

    if (allZero(t)) {
        System.err.println("all zero");
        throw new IllegalStateException();
    }

    /* 5 计算M'=C2^t */
    byte[] M = new byte[klen];
    for (int i = 0; i < M.length; i++) {
        M[i] = (byte) (encryptData[C1Byte.length + i] ^ t[i]);
    }
    /* 6 计算 u = Hash(x2 || M' || y2) 判断 u == C3是否成立 */
    byte[] C3 = new byte[DIGEST_LENGTH];

    System.arraycopy(encryptData, encryptData.length - DIGEST_LENGTH, C3, 0, DIGEST_LENGTH);
    byte[] u = sm3hash(dBC1.getXCoord().toBigInteger().toByteArray(), M,
            dBC1.getYCoord().toBigInteger().toByteArray());
    if (Arrays.equals(u, C3)) {
        try {
            return new String(M, "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    } else {
        return null;
    }

}