java ec_Java ECPoint.Fp方法代码示例

该代码展示了如何使用BouncyCastle库中的Digest和EntropySource创建SP800-90A Dual EC DRBG。根据所需的securityStrength和指定的digest,初始化P-256、P-384或P-521椭圆曲线上的点P和Q,用于不同安全强度级别的伪随机数生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.bouncycastle.math.ec.ECPoint; //导入方法依赖的package包/类

/**

* Construct a SP800-90A Dual EC DRBG.

*

* Minimum entropy requirement is the security strength requested.

*

* @param digest source digest to use with the DRB stream.

* @param securityStrength security strength required (in bits)

* @param entropySource source of entropy to use for seeding/reseeding.

* @param personalizationString personalization string to distinguish this DRBG (may be null).

* @param nonce nonce to further distinguish this DRBG (may be null).

*/

public DualECSP800DRBG(Digest digest, int securityStrength, EntropySource entropySource, byte[] personalizationString, byte[] nonce)

{

_digest = digest;

_entropySource = entropySource;

_securityStrength = securityStrength;

if (Utils.isTooLarge(personalizationString, MAX_PERSONALIZATION_STRING / 8))

{

throw new IllegalArgumentException("Personalization string too large");

}

if (entropySource.entropySize() < securityStrength || entropySource.entropySize() > MAX_ENTROPY_LENGTH)

{

throw new IllegalArgumentException("EntropySource must provide between " + securityStrength + " and " + MAX_ENTROPY_LENGTH + " bits");

}

byte[] entropy = entropySource.getEntropy();

byte[] seedMaterial = Arrays.concatenate(entropy, nonce, personalizationString);

if (securityStrength <= 128)

{

if (Utils.getMaxSecurityStrength(digest) < 128)

{

throw new IllegalArgumentException("Requested security strength is not supported by digest");

}

_seedlen = 256;

_outlen = 240 / 8;

_curve = (ECCurve.Fp)NISTNamedCurves.getByName("P-256").getCurve();

_P = new ECPoint.Fp(_curve, new ECFieldElement.Fp(_curve.getQ(), p256_Px), new ECFieldElement.Fp(_curve.getQ(), p256_Py));

_Q = new ECPoint.Fp(_curve, new ECFieldElement.Fp(_curve.getQ(), p256_Qx), new ECFieldElement.Fp(_curve.getQ(), p256_Qy));

}

else if (securityStrength <= 192)

{

if (Utils.getMaxSecurityStrength(digest) < 192)

{

throw new IllegalArgumentException("Requested security strength is not supported by digest");

}

_seedlen = 384;

_outlen = 368 / 8;

_curve = (ECCurve.Fp)NISTNamedCurves.getByName("P-384").getCurve();

_P = new ECPoint.Fp(_curve, new ECFieldElement.Fp(_curve.getQ(), p384_Px), new ECFieldElement.Fp(_curve.getQ(), p384_Py));

_Q = new ECPoint.Fp(_curve, new ECFieldElement.Fp(_curve.getQ(), p384_Qx), new ECFieldElement.Fp(_curve.getQ(), p384_Qy));

}

else if (securityStrength <= 256)

{

if (Utils.getMaxSecurityStrength(digest) < 256)

{

throw new IllegalArgumentException("Requested security strength is not supported by digest");

}

_seedlen = 521;

_outlen = 504 / 8;

_curve = (ECCurve.Fp)NISTNamedCurves.getByName("P-521").getCurve();

_P = new ECPoint.Fp(_curve, new ECFieldElement.Fp(_curve.getQ(), p521_Px), new ECFieldElement.Fp(_curve.getQ(), p521_Py));

_Q = new ECPoint.Fp(_curve, new ECFieldElement.Fp(_curve.getQ(), p521_Qx), new ECFieldElement.Fp(_curve.getQ(), p521_Qy));

}

else

{

throw new IllegalArgumentException("security strength cannot be greater than 256 bits");

}

_s = Utils.hash_df(_digest, seedMaterial, _seedlen);

_sLength = _s.length;

_reseedCounter = 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值