本文整理汇总了Java中java.math.BigInteger.modInverse方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.modInverse方法的具体用法?Java BigInteger.modInverse怎么用?Java BigInteger.modInverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.modInverse方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getGenerator
import java.math.BigInteger; //导入方法依赖的package包/类
private BigInteger getGenerator(BigInteger q, BigInteger p, BigInteger pMinusOne) {
log.info("creating a generator");
BigInteger h = BigInteger.ONE;
boolean safe = false;
while (!safe) {
h = randomGenerator.randomInZq(p);
safe = modExp(h, TWO, p).compareTo(ONE) != 0
&& modExp(h, q, p).compareTo(ONE) != 0
&& pMinusOne.mod(h).compareTo(BigInteger.ZERO) != 0;
BigInteger gInv = safe ? h.modInverse(p) : ONE;
safe = safe && pMinusOne.mod(gInv).compareTo(BigInteger.ZERO) != 0;
}
log.info("generator created");
return modExp(h, TWO, p);
}
示例2: chineseRemainder
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Computes the integer x that is expressed through the given primes and the
* congruences with the chinese remainder theorem (CRT).
*
* @param congruences
* the congruences c_i
* @param primes
* the primes p_i
* @return an integer x for that x % p_i == c_i
*/
private static BigInteger chineseRemainder(Vector congruences, Vector primes)
{
BigInteger retval = ZERO;
BigInteger all = ONE;
for (int i = 0; i < primes.size(); i++)
{
all = all.multiply((BigInteger)primes.elementAt(i));
}
for (int i = 0; i < primes.size(); i++)
{
BigInteger a = (BigInteger)primes.elementAt(i);
BigInteger b = all.divide(a);
BigInteger b_ = b.modInverse(a);
BigInteger tmp = b.multiply(b_);
tmp = tmp.multiply((BigInteger)congruences.elementAt(i));
retval = retval.add(tmp);
}
return retval.mod(all);
}
示例3: generateS
import java.math.BigInteger; //导入方法依赖的package包/类
private BigInteger generateS(BigInteger x, BigInteger q,
BigInteger r, BigInteger k) throws SignatureException {
byte[] s2;
try {
s2 = md.digest();
} catch (RuntimeException re) {
// Only for RawDSA due to its 20-byte length restriction
throw new SignatureException(re.getMessage());
}
// get the leftmost min(N, outLen) bits of the digest value
int nBytes = q.bitLength()/8;
if (nBytes < s2.length) {
s2 = Arrays.copyOfRange(s2, 0, nBytes);
}
BigInteger z = new BigInteger(1, s2);
BigInteger k1 = k.modInverse(q);
return x.multiply(r).add(z).multiply(k1).mod(q);
}
示例4: verifySignature
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* return true if the value r and s represent a DSA signature for
* the passed in message for standard DSA the message should be a
* SHA-1 hash of the real message to be verified.
*/
public boolean verifySignature(
byte[] message,
BigInteger r,
BigInteger s)
{
DSAParameters params = key.getParameters();
BigInteger m = calculateE(params.getQ(), message);
BigInteger zero = BigInteger.valueOf(0);
if (zero.compareTo(r) >= 0 || params.getQ().compareTo(r) <= 0)
{
return false;
}
if (zero.compareTo(s) >= 0 || params.getQ().compareTo(s) <= 0)
{
return false;
}
BigInteger w = s.modInverse(params.getQ());
BigInteger u1 = m.multiply(w).mod(params.getQ());
BigInteger u2 = r.multiply(w).mod(params.getQ());
u1 = params.getG().modPow(u1, params.getP());
u2 = ((DSAPublicKeyParameters)key).getY().modPow(u2, params.getP());
BigInteger v = u1.multiply(u2).mod(params.getP()).mod(params.getQ());
return v.equals(r);
}
示例5: engineCreateWitness
import java.math.BigInteger; //导入方法依赖的package包/类
@Override
protected byte[] engineCreateWitness(byte[] element) throws AccumulatorException {
BigInteger hash;
try {
hash = fullDomainHash(publicKey.getKey(), element);
} catch (NoSuchAlgorithmException e) {
throw new AccumulatorException(e);
}
BigInteger exponent = hash.modInverse(privateKey.getKey());
return accumulatorValue.modPow(exponent, publicKey.getKey()).toByteArray();
}
示例6: verifySignature
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* return true if the value r and s represent a DSA signature for
* the passed in message for standard DSA the message should be a
* SHA-1 hash of the real message to be verified.
*/
@Override
public boolean verifySignature(
byte[] message,
BigInteger r,
BigInteger s)
{
DSAParameters params = key.getParameters();
BigInteger m = calculateE(params.getQ(), message);
BigInteger zero = BigInteger.valueOf(0);
if (zero.compareTo(r) >= 0 || params.getQ().compareTo(r) <= 0)
{
return false;
}
if (zero.compareTo(s) >= 0 || params.getQ().compareTo(s) <= 0)
{
return false;
}
BigInteger w = s.modInverse(params.getQ());
BigInteger u1 = m.multiply(w).mod(params.getQ());
BigInteger u2 = r.multiply(w).mod(params.getQ());
u1 = params.getG().modPow(u1, params.getP());
u2 = ((DSAPublicKeyParameters)key).getY().modPow(u2, params.getP());
BigInteger v = u1.multiply(u2).mod(params.getP()).mod(params.getQ());
return v.equals(r);
}
示例7: modInv
import java.math.BigInteger; //导入方法依赖的package包/类
public static void modInv(int order) {
int failCount = 0, successCount = 0, nonInvCount = 0;
for (int i=0; i<SIZE; i++) {
BigInteger x = fetchNumber(order);
while(x.equals(BigInteger.ZERO))
x = fetchNumber(order);
BigInteger m = fetchNumber(order).abs();
while(m.compareTo(BigInteger.ONE) != 1)
m = fetchNumber(order).abs();
try {
BigInteger inv = x.modInverse(m);
BigInteger prod = inv.multiply(x).remainder(m);
if (prod.signum() == -1)
prod = prod.add(m);
if (prod.equals(BigInteger.ONE))
successCount++;
else
failCount++;
} catch(ArithmeticException e) {
nonInvCount++;
}
}
report("Modular Inverse for " + order + " bits", failCount);
}
示例8: processBlock
import java.math.BigInteger; //导入方法依赖的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);
}
示例9: verifySignature
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* return true if the value r and s represent a DSA signature for
* the passed in message (for standard DSA the message should be
* a SHA-1 hash of the real message to be verified).
*/
public boolean verifySignature(
byte[] message,
BigInteger r,
BigInteger s)
{
BigInteger n = key.getParameters().getN();
BigInteger e = calculateE(n, message);
// r in the range [1,n-1]
if (r.compareTo(ONE) < 0 || r.compareTo(n) >= 0)
{
return false;
}
// s in the range [1,n-1]
if (s.compareTo(ONE) < 0 || s.compareTo(n) >= 0)
{
return false;
}
BigInteger c = s.modInverse(n);
BigInteger u1 = e.multiply(c).mod(n);
BigInteger u2 = r.multiply(c).mod(n);
ECPoint G = key.getParameters().getG();
ECPoint Q = ((ECPublicKeyParameters)key).getQ();
ECPoint point = ECAlgorithms.sumOfTwoMultiplies(G, u1, Q, u2);
// components must be bogus.
if (point.isInfinity())
{
return false;
}
BigInteger v = point.getX().toBigInteger().mod(n);
return v.equals(r);
}
示例10: verifySignature
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* return true if the value r and s represent a GOST3410 signature for
* the passed in message (for standard GOST3410 the message should be
* a GOST3411 hash of the real message to be verified).
*/
public boolean verifySignature(
byte[] message,
BigInteger r,
BigInteger s)
{
byte[] mRev = new byte[message.length]; // conversion is little-endian
for (int i = 0; i != mRev.length; i++)
{
mRev[i] = message[mRev.length - 1 - i];
}
BigInteger e = new BigInteger(1, mRev);
BigInteger n = key.getParameters().getN();
// r in the range [1,n-1]
if (r.compareTo(ECConstants.ONE) < 0 || r.compareTo(n) >= 0)
{
return false;
}
// s in the range [1,n-1]
if (s.compareTo(ECConstants.ONE) < 0 || s.compareTo(n) >= 0)
{
return false;
}
BigInteger v = e.modInverse(n);
BigInteger z1 = s.multiply(v).mod(n);
BigInteger z2 = (n.subtract(r)).multiply(v).mod(n);
ECPoint G = key.getParameters().getG(); // P
ECPoint Q = ((ECPublicKeyParameters)key).getQ();
ECPoint point = ECAlgorithms.sumOfTwoMultiplies(G, z1, Q, z2);
// components must be bogus.
if (point.isInfinity())
{
return false;
}
BigInteger R = point.getX().toBigInteger().mod(n);
return R.equals(r);
}
示例11: generateKeyPair
import java.math.BigInteger; //导入方法依赖的package包/类
public KeyPair generateKeyPair() {
// accommodate odd key sizes in case anybody wants to use them
int lp = (keySize + 1) >> 1;
int lq = keySize - lp;
if (random == null) {
random = JCAUtil.getSecureRandom();
}
BigInteger e = publicExponent;
while (true) {
// generate two random primes of size lp/lq
BigInteger p = BigInteger.probablePrime(lp, random);
BigInteger q, n;
do {
q = BigInteger.probablePrime(lq, random);
// convention is for p > q
if (p.compareTo(q) < 0) {
BigInteger tmp = p;
p = q;
q = tmp;
}
// modulus n = p * q
n = p.multiply(q);
// even with correctly sized p and q, there is a chance that
// n will be one bit short. re-generate the smaller prime if so
} while (n.bitLength() < keySize);
// phi = (p - 1) * (q - 1) must be relative prime to e
// otherwise RSA just won't work ;-)
BigInteger p1 = p.subtract(BigInteger.ONE);
BigInteger q1 = q.subtract(BigInteger.ONE);
BigInteger phi = p1.multiply(q1);
// generate new p and q until they work. typically
// the first try will succeed when using F4
if (e.gcd(phi).equals(BigInteger.ONE) == false) {
continue;
}
// private exponent d is the inverse of e mod phi
BigInteger d = e.modInverse(phi);
// 1st prime exponent pe = d mod (p - 1)
BigInteger pe = d.mod(p1);
// 2nd prime exponent qe = d mod (q - 1)
BigInteger qe = d.mod(q1);
// crt coefficient coeff is the inverse of q mod p
BigInteger coeff = q.modInverse(p);
try {
PublicKey publicKey = new RSAPublicKeyImpl(n, e);
PrivateKey privateKey =
new RSAPrivateCrtKeyImpl(n, e, d, p, q, pe, qe, coeff);
return new KeyPair(publicKey, privateKey);
} catch (InvalidKeyException exc) {
// invalid key exception only thrown for keys < 512 bit,
// will not happen here
throw new RuntimeException(exc);
}
}
}
示例12: generateW
import java.math.BigInteger; //导入方法依赖的package包/类
private BigInteger generateW(BigInteger p, BigInteger q,
BigInteger g, BigInteger s) {
return s.modInverse(q);
}
示例13: verifySignature
import java.math.BigInteger; //导入方法依赖的package包/类
public static boolean verifySignature(byte[] message, DSASignature ds, DSAPublicKey dpk) throws IOException {
/* Inspired by Bouncycastle's DSASigner class */
SHA1 md = new SHA1();
md.update(message);
byte[] sha_message = new byte[md.getDigestLength()];
md.digest(sha_message);
BigInteger m = new BigInteger(1, sha_message);
BigInteger r = ds.getR();
BigInteger s = ds.getS();
BigInteger g = dpk.getG();
BigInteger p = dpk.getP();
BigInteger q = dpk.getQ();
BigInteger y = dpk.getY();
BigInteger zero = BigInteger.ZERO;
if (log.isEnabled()) {
log.log(60, "ssh-dss signature: m: " + m.toString(16));
log.log(60, "ssh-dss signature: r: " + r.toString(16));
log.log(60, "ssh-dss signature: s: " + s.toString(16));
log.log(60, "ssh-dss signature: g: " + g.toString(16));
log.log(60, "ssh-dss signature: p: " + p.toString(16));
log.log(60, "ssh-dss signature: q: " + q.toString(16));
log.log(60, "ssh-dss signature: y: " + y.toString(16));
}
if (zero.compareTo(r) >= 0 || q.compareTo(r) <= 0) {
log.log(20, "ssh-dss signature: zero.compareTo(r) >= 0 || q.compareTo(r) <= 0");
return false;
}
if (zero.compareTo(s) >= 0 || q.compareTo(s) <= 0) {
log.log(20, "ssh-dss signature: zero.compareTo(s) >= 0 || q.compareTo(s) <= 0");
return false;
}
BigInteger w = s.modInverse(q);
BigInteger u1 = m.multiply(w).mod(q);
BigInteger u2 = r.multiply(w).mod(q);
u1 = g.modPow(u1, p);
u2 = y.modPow(u2, p);
BigInteger v = u1.multiply(u2).mod(p).mod(q);
return v.equals(r);
}
示例14: calcPrivateExp
import java.math.BigInteger; //导入方法依赖的package包/类
private static void calcPrivateExp(BigInteger p, BigInteger q, BigInteger exp) {
BigInteger mod = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger d = exp.modInverse(mod);
System.out.println(p + " " + q + " " + d);
}