Example usage for java.math BigInteger modInverse

List of usage examples for java.math BigInteger modInverse

Introduction

In this page you can find the example usage for java.math BigInteger modInverse.

Prototype

public BigInteger modInverse(BigInteger m) 

Source Link

Document

Returns a BigInteger whose value is (this -1 mod m) .

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("7");
    BigInteger bi2 = new BigInteger("20");

    // perform modInverse operation on bi1 using bi2
    BigInteger bi3 = bi1.modInverse(bi2);

    System.out.println(bi3);//www.  ja  va  2  s  . co m
}

From source file:Main.java

public static BigInteger computeUnblindSignature(BigInteger r, BigInteger bs)
        throws UnsupportedEncodingException {
    RSAPublicKey pubKey = (RSAPublicKey) getPublicKey(public_key);
    BigInteger n = pubKey.getModulus();

    BigInteger s = r.modInverse(n).multiply(bs).mod(n);

    return s;//from  w  w  w  . j  a va  2s  .c  o m
}

From source file:com.mastercard.mcbp.utils.crypto.CryptoServiceImpl.java

/**
 * {@inheritDoc}//  w w  w  .j a v  a  2  s . co m
 */
@Override
public final int initRsaPrivateKey(final ByteArray primeP, final ByteArray primeQ,
        final ByteArray primeExponentP, final ByteArray primeExponentQ, final ByteArray crtCoefficient)
        throws McbpCryptoException {
    try {
        final BigInteger p = new BigInteger(primeP.toHexString(), 16);
        final BigInteger q = new BigInteger(primeQ.toHexString(), 16);
        final BigInteger dp = new BigInteger(primeExponentP.toHexString(), 16);
        final BigInteger dq = new BigInteger(primeExponentQ.toHexString(), 16);
        final BigInteger a = new BigInteger(crtCoefficient.toHexString(), 16);

        final BigInteger n = p.multiply(q);
        final BigInteger e = dp.modInverse(p.subtract(BigInteger.ONE));

        final BigInteger d = e.modInverse(p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE))
                .divide((p.subtract(BigInteger.ONE)).gcd(q.subtract(BigInteger.ONE))));

        final RSAPrivateKey rsaKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
                .generatePrivate(new RSAPrivateCrtKeySpec(n, e, d, p, q, dp, dq, a));

        initRsaPrivate(rsaKey);

        return n.bitLength() / 8;

    } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new McbpCryptoException(e.toString());
    }
}