本文整理汇总了Java中java.security.spec.ECPoint.getAffineX方法的典型用法代码示例。如果您正苦于以下问题:Java ECPoint.getAffineX方法的具体用法?Java ECPoint.getAffineX怎么用?Java ECPoint.getAffineX使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.spec.ECPoint
的用法示例。
在下文中一共展示了ECPoint.getAffineX方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkPointOnCurve
import java.security.spec.ECPoint; //导入方法依赖的package包/类
/**
* Checks that a point is on a given elliptic curve. This method implements the partial public key
* validation routine from Section 5.6.2.6 of NIST SP 800-56A
* http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf A partial
* public key validation is sufficient for curves with cofactor 1. See Section B.3 of
* http://www.nsa.gov/ia/_files/SuiteB_Implementer_G-113808.pdf The point validations above are
* taken from recommendations for ECDH, because parameter checks in ECDH are much more important
* than for the case of ECDSA. Performing this test for ECDSA keys is mainly a sanity check.
*
* @param point the point that needs verification
* @param ec the elliptic curve. This must be a curve over a prime order field.
* @throws GeneralSecurityException if the field is binary or if the point is not on the curve.
*/
public static void checkPointOnCurve(ECPoint point, EllipticCurve ec)
throws GeneralSecurityException {
BigInteger p = getModulus(ec);
BigInteger x = point.getAffineX();
BigInteger y = point.getAffineY();
if (x == null || y == null) {
throw new GeneralSecurityException("point is at infinity");
}
// Check 0 <= x < p and 0 <= y < p.
if (x.signum() == -1 || x.compareTo(p) != -1) {
throw new GeneralSecurityException("x is out of range");
}
if (y.signum() == -1 || y.compareTo(p) != -1) {
throw new GeneralSecurityException("y is out of range");
}
// Check y^2 == x^3 + a x + b (mod p)
BigInteger lhs = y.multiply(y).mod(p);
BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
if (!lhs.equals(rhs)) {
throw new GeneralSecurityException("Point is not on curve");
}
}
示例2: getWeakPublicKey
import java.security.spec.ECPoint; //导入方法依赖的package包/类
/**
* Returns a weak public key of order 3 such that the public key point is on the curve specified
* in ecParams. This method is used to check ECC implementations for missing step in the
* verification of the public key. E.g. implementations of ECDH must verify that the public key
* contains a point on the curve as well as public and secret key are using the same curve.
*
* @param ecParams the parameters of the key to attack. This must be a curve in Weierstrass form
* over a prime order field.
* @return a weak EC group with a genrator of order 3.
*/
public static ECPublicKeySpec getWeakPublicKey(ECParameterSpec ecParams)
throws GeneralSecurityException {
EllipticCurve curve = ecParams.getCurve();
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
BigInteger p = getModulus(curve);
BigInteger three = new BigInteger("3");
while (true) {
// Generate a point on the original curve
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
ECPoint w = pub.getW();
BigInteger x = w.getAffineX();
BigInteger y = w.getAffineY();
// Find the curve parameters a,b such that 3*w = infinity.
// This is the case if the following equations are satisfied:
// 3x == l^2 (mod p)
// l == (3x^2 + a) / 2*y (mod p)
// y^2 == x^3 + ax + b (mod p)
BigInteger l;
try {
l = modSqrt(x.multiply(three), p);
} catch (GeneralSecurityException ex) {
continue;
}
BigInteger xSqr = x.multiply(x).mod(p);
BigInteger a = l.multiply(y.add(y)).subtract(xSqr.multiply(three)).mod(p);
BigInteger b = y.multiply(y).subtract(x.multiply(xSqr.add(a))).mod(p);
EllipticCurve newCurve = new EllipticCurve(curve.getField(), a, b);
// Just a sanity check.
checkPointOnCurve(w, newCurve);
// Cofactor and order are of course wrong.
ECParameterSpec spec = new ECParameterSpec(newCurve, w, p, 1);
return new ECPublicKeySpec(w, spec);
}
}
示例3: main
import java.security.spec.ECPoint; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec gps = new ECGenParameterSpec ("secp256r1"); // NIST P-256
kpg.initialize(gps);
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey)apair.getPublic();
ECParameterSpec aspec = apub.getParams();
// could serialize aspec for later use (in compatible JRE)
//
// for test only reuse bogus pubkey, for real substitute values
ECPoint apoint = apub.getW();
BigInteger x = apoint.getAffineX(), y = apoint.getAffineY();
// construct point plus params to pubkey
ECPoint bpoint = new ECPoint (x,y);
ECPublicKeySpec bpubs = new ECPublicKeySpec (bpoint, aspec);
KeyFactory kfa = KeyFactory.getInstance ("EC");
ECPublicKey bpub = (ECPublicKey) kfa.generatePublic(bpubs);
new Ssh2EcdsaSha2NistPublicKey(bpub);
}
示例4: checkPointOnCurve
import java.security.spec.ECPoint; //导入方法依赖的package包/类
/**
* Checks that a point is on a given elliptic curve.
*
* <p><b>Warning:</b> Please use {@link #validatePublicKey} if you want to validate a public key
* to avoid invalid curve attacks or small subgroup attacks in ECDH.
*
* <p>This method implements the partial public key validation routine from Section 5.6.2.6 of <a
* href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf">NIST SP
* 800-56A</a>. A partial public key validation is sufficient for curves with cofactor 1. See
* Section B.3 of http://www.nsa.gov/ia/_files/SuiteB_Implementer_G-113808.pdf.
*
* <p>The point validations above are taken from recommendations for ECDH, because parameter
* checks in ECDH are much more important than for the case of ECDSA. Performing this test for
* ECDSA keys is mainly a sanity check.
*
* @param point the point that needs verification
* @param ec the elliptic curve. This must be a curve over a prime order field.
* @throws GeneralSecurityException if the field is binary or if the point is not on the curve.
*/
static void checkPointOnCurve(ECPoint point, EllipticCurve ec) throws GeneralSecurityException {
BigInteger p = getModulus(ec);
BigInteger x = point.getAffineX();
BigInteger y = point.getAffineY();
if (x == null || y == null) {
throw new GeneralSecurityException("point is at infinity");
}
// Check 0 <= x < p and 0 <= y < p.
if (x.signum() == -1 || x.compareTo(p) != -1) {
throw new GeneralSecurityException("x is out of range");
}
if (y.signum() == -1 || y.compareTo(p) != -1) {
throw new GeneralSecurityException("y is out of range");
}
// Check y^2 == x^3 + a x + b (mod p)
BigInteger lhs = y.multiply(y).mod(p);
BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
if (!lhs.equals(rhs)) {
throw new GeneralSecurityException("Point is not on curve");
}
}
示例5: PointMul
import java.security.spec.ECPoint; //导入方法依赖的package包/类
private static ECPoint PointMul(BigInteger k, ECPoint pt, EllipticCurve curve) {
int len = k.bitLength();
ECPoint Q = new ECPoint(pt.getAffineX(), pt.getAffineY());
for (int i = len - 2; i >= 0; i--) {
Q = PointAdd(Q, Q, curve);
if (k.testBit(i)) {
Q = PointAdd(Q, pt, curve);
}
}
return Q;
}
示例6: testGetAffineX01
import java.security.spec.ECPoint; //导入方法依赖的package包/类
/**
* Test #1 for <code>getAffineX()</code> method<br>
* Assertion: returns affine <code>x</code> coordinate<br>
* Test preconditions: <code>ECPoint</code> instance
* created using valid parameters<br>
* Expected: must return affine <code>x</code> coordinate
* which is equal to the one passed to the constructor;
* (both must refer the same object)
*/
@TestTargetNew(
level = TestLevel.PARTIAL_COMPLETE,
notes = "Verifies positive case.",
method = "getAffineX",
args = {}
)
public final void testGetAffineX01() {
BigInteger x = BigInteger.valueOf(-23456L);
ECPoint p = new ECPoint(x, BigInteger.valueOf(23456L));
BigInteger xRet = p.getAffineX();
assertEquals(x, xRet);
assertSame(x, xRet);
}
示例7: fromJavaKey
import java.security.spec.ECPoint; //导入方法依赖的package包/类
public static SigningPublicKey fromJavaKey(ECPublicKey pk, SigType type)
throws GeneralSecurityException {
ECPoint w = pk.getW();
BigInteger x = w.getAffineX();
BigInteger y = w.getAffineY();
int len = type.getPubkeyLen();
byte[] b = combine(x, y, len);
return new SigningPublicKey(type, b);
}
示例8: Sign
import java.security.spec.ECPoint; //导入方法依赖的package包/类
public byte[] Sign(byte[] id, byte[] msg) {
// 得到M^ = ZA||M
ECPoint pA = PointMul(dA_, spec_.getGenerator(), spec_.getCurve());
byte[] ZA = GetZA(spec_, pA, id);
// System.out.println("ZA=" + bytesToHex(ZA));
byte[] M = new byte[msg.length + ZA.length];
System.arraycopy(ZA, 0, M, 0, ZA.length);
System.arraycopy(msg, 0, M, ZA.length, msg.length);
// 第二步 e=Hv(M^)
byte[] ebytes = SM3Digest.Hash(M);
BigInteger e = new BigInteger(bytesToHex(ebytes), 16);
BigInteger n = spec_.getOrder();
while (true) {
// 第三步 产生随机数k [1,n-1]
Random random = new Random();
BigInteger K = new BigInteger(n.bitLength() - 1, random);
if (K.compareTo(n) == 0 || K.compareTo(BigInteger.ZERO) == 0) {
continue;
}
// 第四步 计算pt1(x1,y1) = [K]G这个点
ECPoint G = spec_.getGenerator();
ECPoint pt1 = PointMul(K, G, spec_.getCurve());
BigInteger x1 = pt1.getAffineX();
// 第五步 计算 r = (e + x1) mod n
BigInteger r = x1.add(e).mod(n);
r = r.add(n).mod(n);
// System.out.println("r=" + r.toString(16));
// 确保r!=0 且 r+k!=n 也就是 (r+k) != 0 mod n
if (r.add(K).mod(n).compareTo(BigInteger.ZERO) == 0) {
continue;
}
// 第六步 计算 s = ((1 + d)^-1 * (k - rd)) mod n
BigInteger tmp1 = dA_.add(BigInteger.ONE).modInverse(n);
BigInteger tmp2 = K.subtract(r.multiply(dA_).mod(n)).mod(n);
BigInteger s = tmp1.multiply(tmp2).mod(n);
s = s.add(n).mod(n);
if (s.compareTo(BigInteger.ZERO) == 0) {
continue;
}
ECFieldFp fp = (ECFieldFp) spec_.getCurve().getField();
byte[] pb = fp.getP().toByteArray();
int len = pb[0] == 0 ? pb.length - 1 : pb.length;
byte[] rb = r.toByteArray();
byte[] sb = s.toByteArray();
byte[] sig = new byte[2 * len];
if (rb[0] == 0) {
System.arraycopy(rb, 1, sig, len - rb.length + 1, rb.length - 1);
} else {
System.arraycopy(rb, 0, sig, len - rb.length, rb.length);
}
if (sb[0] == 0) {
System.arraycopy(sb, 1, sig, 2 * len - sb.length + 1, sb.length - 1);
} else {
System.arraycopy(sb, 0, sig, 2 * len - sb.length, sb.length);
}
return sig;
}
}
示例9: Sign
import java.security.spec.ECPoint; //导入方法依赖的package包/类
public byte[] Sign(byte[] id, byte[] msg) {
// 得到M^ = ZA||M
ECPoint pA = PointMul(dA_, spec_.getGenerator(), spec_.getCurve());
byte[] ZA = GetZA(spec_, pA, id);
// System.out.println("ZA=" + bytesToHex(ZA));
byte[] M = new byte[msg.length + ZA.length];
System.arraycopy(ZA, 0, M, 0, ZA.length);
System.arraycopy(msg, 0, M, ZA.length, msg.length);
// 第二步 e=Hv(M^)
byte[] ebytes = SM3Digest.Hash(M);
BigInteger e = new BigInteger(bytesToHex(ebytes), 16);
BigInteger n = spec_.getOrder();
while (true) {
// 第三步 产生随机数k [1,n-1]
Random random = new java.util.Random();
BigInteger K = new BigInteger(n.bitLength() - 1, random);
if (K.compareTo(n) == 0 || K.compareTo(BigInteger.ZERO) == 0) {
continue;
}
// 第四步 计算pt1(x1,y1) = [K]G这个点
ECPoint G = spec_.getGenerator();
ECPoint pt1 = PointMul(K, G, spec_.getCurve());
BigInteger x1 = pt1.getAffineX();
// 第五步 计算 r = (e + x1) mod n
BigInteger r = x1.add(e).mod(n);
r = r.add(n).mod(n);
// System.out.println("r=" + r.toString(16));
// 确保r!=0 且 r+k!=n 也就是 (r+k) != 0 mod n
if (r.add(K).mod(n).compareTo(BigInteger.ZERO) == 0) {
continue;
}
// 第六步 计算 s = ((1 + d)^-1 * (k - rd)) mod n
BigInteger tmp1 = dA_.add(BigInteger.ONE).modInverse(n);
BigInteger tmp2 = K.subtract(r.multiply(dA_).mod(n)).mod(n);
BigInteger s = tmp1.multiply(tmp2).mod(n);
s = s.add(n).mod(n);
if (s.compareTo(BigInteger.ZERO) == 0) {
continue;
}
ECFieldFp fp = (ECFieldFp) spec_.getCurve().getField();
byte[] pb = fp.getP().toByteArray();
int len = pb[0] == 0 ? pb.length - 1 : pb.length;
byte[] rb = r.toByteArray();
byte[] sb = s.toByteArray();
byte[] sig = new byte[2 * len];
if (rb[0] == 0) {
System.arraycopy(rb, 1, sig, len - rb.length + 1, rb.length - 1);
} else {
System.arraycopy(rb, 0, sig, len - rb.length, rb.length);
}
if (sb[0] == 0) {
System.arraycopy(sb, 1, sig, 2 * len - sb.length + 1, sb.length - 1);
} else {
System.arraycopy(sb, 0, sig, 2 * len - sb.length, sb.length);
}
return sig;
}
}
示例10: testGetAffineX01
import java.security.spec.ECPoint; //导入方法依赖的package包/类
/**
* Test #1 for <code>getAffineX()</code> method<br>
* Assertion: returns affine <code>x</code> coordinate<br>
* Test preconditions: <code>ECPoint</code> instance
* created using valid parameters<br>
* Expected: must return affine <code>x</code> coordinate
* which is equal to the one passed to the constructor;
* (both must refer the same object)
*/
public final void testGetAffineX01() {
BigInteger x = BigInteger.valueOf(-23456L);
ECPoint p = new ECPoint(x, BigInteger.valueOf(23456L));
BigInteger xRet = p.getAffineX();
assertEquals(x, xRet);
assertSame(x, xRet);
}