本文整理汇总了Java中org.bouncycastle.util.BigIntegers.createRandomInRange方法的典型用法代码示例。如果您正苦于以下问题:Java BigIntegers.createRandomInRange方法的具体用法?Java BigIntegers.createRandomInRange怎么用?Java BigIntegers.createRandomInRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.util.BigIntegers
的用法示例。
在下文中一共展示了BigIntegers.createRandomInRange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateZeroKnowledgeProof
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Calculate a zero knowledge proof of x using Schnorr's signature.
* The returned array has two elements {g^v, r = v-x*h} for x.
*/
public static BigInteger[] calculateZeroKnowledgeProof(
BigInteger p,
BigInteger q,
BigInteger g,
BigInteger gx,
BigInteger x,
String participantId,
Digest digest,
SecureRandom random)
{
BigInteger[] zeroKnowledgeProof = new BigInteger[2];
/* Generate a random v, and compute g^v */
BigInteger vMin = ZERO;
BigInteger vMax = q.subtract(ONE);
BigInteger v = BigIntegers.createRandomInRange(vMin, vMax, random);
BigInteger gv = g.modPow(v, p);
BigInteger h = calculateHashForZeroKnowledgeProof(g, gv, gx, participantId, digest); // h
zeroKnowledgeProof[0] = gv;
zeroKnowledgeProof[1] = v.subtract(x.multiply(h)).mod(q); // r = v-x*h
return zeroKnowledgeProof;
}
示例2: calculatePrivate
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
BigInteger calculatePrivate(DHParameters dhParams, SecureRandom random)
{
BigInteger p = dhParams.getP();
int limit = dhParams.getL();
if (limit != 0)
{
return new BigInteger(limit, random).setBit(limit - 1);
}
BigInteger min = TWO;
int m = dhParams.getM();
if (m != 0)
{
min = ONE.shiftLeft(m - 1);
}
BigInteger max = p.subtract(TWO);
BigInteger q = dhParams.getQ();
if (q != null)
{
max = q.subtract(TWO);
}
return BigIntegers.createRandomInRange(min, max, random);
}
示例3: encrypt
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Generate and encapsulate a random session key.
*
* @param out the output buffer for the encapsulated key.
* @param outOff the offset for the output buffer.
* @param keyLen the length of the random session key.
* @return the random session key.
*/
public CipherParameters encrypt(byte[] out, int outOff, int keyLen)
throws IllegalArgumentException
{
if (key.isPrivate())
{
throw new IllegalArgumentException("Public key required for encryption");
}
BigInteger n = key.getModulus();
BigInteger e = key.getExponent();
// Generate the ephemeral random and encode it
BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd);
// Encrypt the random and encode it
BigInteger c = r.modPow(e, n);
byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c);
System.arraycopy(C, 0, out, outOff, C.length);
return generateKey(n, r, keyLen);
}
示例4: selectGenerator
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
static BigInteger selectGenerator(BigInteger p, SecureRandom random)
{
BigInteger pMinusTwo = p.subtract(TWO);
BigInteger g;
/*
* RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81)
*/
do
{
BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random);
g = h.modPow(TWO, p);
}
while (g.equals(ONE));
return g;
}
示例5: generatePrivateKey
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
private static BigInteger generatePrivateKey(BigInteger q, SecureRandom random)
{
// B.1.2 Key Pair Generation by Testing Candidates
int minWeight = q.bitLength() >>> 2;
for (;;)
{
// TODO Prefer this method? (change test cases that used fixed random)
// B.1.1 Key Pair Generation Using Extra Random Bits
// BigInteger x = new BigInteger(q.bitLength() + 64, random).mod(q.subtract(ONE)).add(ONE);
BigInteger x = BigIntegers.createRandomInRange(ONE, q.subtract(ONE), random);
if (WNafUtil.getNafWeight(x) >= minWeight)
{
return x;
}
}
}
示例6: performTest
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
public void performTest()
throws Exception
{
BigInteger pSubOne = DHStandardGroups.rfc3526_2048.getP().subtract(ONE);
for (int i = 0; i < 10; ++i)
{
BigInteger message = BigIntegers.createRandomInRange(ONE, pSubOne, RND);
BigInteger m1 = encDecTest(message);
BigInteger m2 = labelledEncDecTest(message, "myRandomLabel");
BigInteger m3 = encDecEncodingTest(message);
BigInteger m4 = labelledEncDecEncodingTest(message, "myOtherCoolLabel");
if (!message.equals(m1) || !message.equals(m2) || !message.equals(m3) || !message.equals(m4))
{
fail("decrypted message != original message");
}
}
}
示例7: generateX1
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Return a value that can be used as x1 or x3 during round 1.
* <p/>
* <p/>
* The returned value is a random value in the range <tt>[0, q-1]</tt>.
*/
public static BigInteger generateX1(
BigInteger q,
SecureRandom random)
{
BigInteger min = ZERO;
BigInteger max = q.subtract(ONE);
return BigIntegers.createRandomInRange(min, max, random);
}
示例8: generateX2
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Return a value that can be used as x2 or x4 during round 1.
* <p/>
* <p/>
* The returned value is a random value in the range <tt>[1, q-1]</tt>.
*/
public static BigInteger generateX2(
BigInteger q,
SecureRandom random)
{
BigInteger min = ONE;
BigInteger max = q.subtract(ONE);
return BigIntegers.createRandomInRange(min, max, random);
}
示例9: encrypt
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Generate and encapsulate a random session key.
*
* @param out the output buffer for the encapsulated key.
* @param outOff the offset for the output buffer.
* @param keyLen the length of the random session key.
* @return the random session key.
*/
public CipherParameters encrypt(byte[] out, int outOff, int keyLen)
throws IllegalArgumentException
{
if (key.isPrivate())
{
throw new IllegalArgumentException("Public key required for encryption");
}
BigInteger n = key.getModulus();
BigInteger e = key.getExponent();
// Generate the ephemeral random and encode it
BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd);
byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
// Encrypt the random and encode it
BigInteger c = r.modPow(e, n);
byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c);
System.arraycopy(C, 0, out, outOff, C.length);
// Initialise the KDF
kdf.init(new KDFParameters(R, null));
// Generate the secret key
byte[] K = new byte[keyLen];
kdf.generateBytes(K, 0, K.length);
return new KeyParameter(K);
}
示例10: calculateGenerator_FIPS186_2
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
private static BigInteger calculateGenerator_FIPS186_2(BigInteger p, BigInteger q, SecureRandom r)
{
BigInteger e = p.subtract(ONE).divide(q);
BigInteger pSub2 = p.subtract(TWO);
for (;;)
{
BigInteger h = BigIntegers.createRandomInRange(TWO, pSub2, r);
BigInteger g = h.modPow(e, p);
if (g.bitLength() > 1)
{
return g;
}
}
}
示例11: generatePrivateKey
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
private static BigInteger generatePrivateKey(BigInteger q, SecureRandom random)
{
// TODO Prefer this method? (change test cases that used fixed random)
// B.1.1 Key Pair Generation Using Extra Random Bits
// BigInteger c = new BigInteger(q.bitLength() + 64, random);
// return c.mod(q.subtract(ONE)).add(ONE);
// B.1.2 Key Pair Generation by Testing Candidates
return BigIntegers.createRandomInRange(ONE, q.subtract(ONE), random);
}
示例12: selectGenerator
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
static BigInteger selectGenerator(BigInteger p, BigInteger q, SecureRandom random)
{
BigInteger pMinusTwo = p.subtract(TWO);
BigInteger g;
/*
* (see: Handbook of Applied Cryptography 4.80)
*/
// do
// {
// g = BigIntegers.createRandomInRange(TWO, pMinusTwo, random);
// }
// while (g.modPow(TWO, p).equals(ONE) || g.modPow(q, p).equals(ONE));
/*
* RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81)
*/
do
{
BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random);
g = h.modPow(TWO, p);
}
while (g.equals(ONE));
return g;
}
示例13: generateX1
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Return a value that can be used as x1 or x3 during round 1.
* <p>
* The returned value is a random value in the range <tt>[0, q-1]</tt>.
*/
public static BigInteger generateX1(
BigInteger q,
SecureRandom random)
{
BigInteger min = ZERO;
BigInteger max = q.subtract(ONE);
return BigIntegers.createRandomInRange(min, max, random);
}
示例14: generateX2
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Return a value that can be used as x2 or x4 during round 1.
* <p>
* The returned value is a random value in the range <tt>[1, q-1]</tt>.
*/
public static BigInteger generateX2(
BigInteger q,
SecureRandom random)
{
BigInteger min = ONE;
BigInteger max = q.subtract(ONE);
return BigIntegers.createRandomInRange(min, max, random);
}
示例15: processBlock
import org.bouncycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Process a single block using the basic RSA algorithm.
*
* @param in the input array.
* @param inOff the offset into the input buffer where the data starts.
* @param inLen the length of the data to be processed.
* @return the result of the RSA process.
* @exception DataLengthException the input block is too large.
*/
public byte[] processBlock(
byte[] in,
int inOff,
int inLen)
{
if (key == null)
{
throw new IllegalStateException("RSA engine not initialised");
}
BigInteger input = core.convertInput(in, inOff, inLen);
BigInteger result;
if (key instanceof RSAPrivateCrtKeyParameters)
{
RSAPrivateCrtKeyParameters k = (RSAPrivateCrtKeyParameters)key;
BigInteger e = k.getPublicExponent();
if (e != null) // can't do blinding without a public exponent
{
BigInteger m = k.getModulus();
BigInteger r = BigIntegers.createRandomInRange(ONE, m.subtract(ONE), random);
BigInteger blindedInput = r.modPow(e, m).multiply(input).mod(m);
BigInteger blindedResult = core.processBlock(blindedInput);
BigInteger rInv = r.modInverse(m);
result = blindedResult.multiply(rInv).mod(m);
}
else
{
result = core.processBlock(input);
}
}
else
{
result = core.processBlock(input);
}
return core.convertOutput(result);
}