本文整理汇总了Java中java.math.BigInteger.mod方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.mod方法的具体用法?Java BigInteger.mod怎么用?Java BigInteger.mod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.mod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: main
import java.math.BigInteger; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
TestJCAProvider d = new TestJCAProvider();
// Add dynamically the desired provider
Security.addProvider(new PaillierProvider());
/////////////////////////////////////////////////////////////////////
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Paillier");
kpg.initialize(32);
KeyPair keyPair = kpg.generateKeyPair();
PublicKey pubKey = keyPair.getPublic();
PrivateKey privKey = keyPair.getPrivate();
final Cipher cipher = Cipher.getInstance("Paillier");
final Cipher cipherHP = Cipher.getInstance("PaillierHP");
System.out.println("The Paillier public key through Generator is \n"+keyPair.toString());
System.out.println("The Paillier public key is \n"+keyPair.getPublic().toString());
System.out.println("The Paillier private key is \n"+keyPair.getPrivate().toString());
String plainText = "101";
String plaintext1 = "101";
// get the n
String delims = "[,]";
String[] keyComponents = pubKey.toString().split(delims);
String keyComponent = "";
for (String keyComponent2 : keyComponents) {
if (keyComponent2.startsWith("n")) {
keyComponent = keyComponent2.substring(2);// ignoring 'n:' or 'r:'
}
}
BigInteger n = new BigInteger(keyComponent);
BigInteger first = new BigInteger(plainText);
BigInteger second = new BigInteger(plaintext1);
BigInteger n2 = n.multiply(n);
// encrypt
BigInteger codedBytes = d.encrypt(first.toByteArray(), pubKey,cipherHP);
BigInteger codedBytes12 = d.encrypt(second.toByteArray(), pubKey,cipherHP);
//product
BigInteger product = codedBytes.multiply(codedBytes12);
// product mod n^2
BigInteger tallyProduct = product.mod(n2);
System.out.println(" Product mod n^2: "+tallyProduct);
d.decrypt(tallyProduct.toByteArray(), privKey,cipherHP);
d.decrypt(codedBytes.toByteArray(),privKey,cipherHP);
d.decrypt(codedBytes12.toByteArray(),privKey,cipherHP);
//////////////////////////////BLOCK EXAMPLE/////////////////////////////////
String plainTextBlock = "This Provider working correctly and its safe 10000000000000000011000000000000000001";
System.out.println("This is the message which will be encrypted: " + plainTextBlock);
// encrypt
byte[] codedBytesBlock = d.encryptBlock(plainTextBlock.getBytes(), pubKey,cipher);
String codedMessageBlock = new String(codedBytesBlock);
String codedMessageBlockInHEX = formatingHexRepresentation(codedBytesBlock);
System.out.println("\n" + "ENCRYPTED : \n" + codedMessageBlock);
System.out.println("\n" + "ENCRYPTED in HEX: \n" + codedMessageBlockInHEX);
// decrypt
byte[] encodedBytesBlock = d.decryptBlock(codedMessageBlock, privKey,cipher);
String encodedMessageBlock = new String(encodedBytesBlock);
System.out.println("\n" + "DECRYPTED: \n" + encodedMessageBlock);
}
示例3: PerformSignature
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Host has collected all the shares for the same j, can use Algorithm 4.3
* on all the σi, j to recover σj , obtaining the aggregate signature (σj ,
* ϵj ). The recipient of (m, j), σ, ϵ can verify the validity of the
* signature by checking if ϵ = Hash(R| |Hash(m)| |j), where R = σ ·G +ϵ ·Y.
*
* @param msgToSign
* @param i
* @param playersList
* @param channel
* @param perfResultsList
* @param perfFile
* @throws NoSuchAlgorithmException
* @throws Exception
*/
static void PerformSignature(BigInteger msgToSign, int counter, ArrayList<MPCPlayer> playersList, ArrayList<Pair<String, Long>> perfResultsList, FileOutputStream perfFile, MPCRunConfig runCfg) throws NoSuchAlgorithmException, Exception {
// Sign EC Point
byte[] plaintext_sig = mpcGlobals.G.multiply(msgToSign).getEncoded(false);
if (!playersList.isEmpty()) {
BigInteger sum_s_BI = new BigInteger("0");
BigInteger card_e_BI = new BigInteger("0");
boolean bFirstPlayer = true;
for (MPCPlayer player : playersList) {
if (bFirstPlayer) {
sum_s_BI = player.Sign(QUORUM_INDEX, counter, mpcGlobals.Rands[counter - 1].getEncoded(false), plaintext_sig);
card_e_BI = player.GetE(QUORUM_INDEX);
bFirstPlayer = false;
} else {
sum_s_BI = sum_s_BI.add(player.Sign(QUORUM_INDEX, counter, mpcGlobals.Rands[counter - 1].getEncoded(false), plaintext_sig));
sum_s_BI = sum_s_BI.mod(mpcGlobals.n);
}
}
System.out.println(String.format("Sign: %s", Util.bytesToHex(sum_s_BI.toByteArray())));
}
}
示例4: mod
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Returns (this mod m), allowing for negative numbers or zeros as results.
* @param m The divisor
* @return The result of the modulo operation
*/
public BigRational mod(BigInteger m) {
if (undefined) {
return this; // We cannot compute the mod of an "uncomputable" number.
} else if (this.isNegative()) {
return this.negate().mod(m).negate();
} else {
BigRational toReturn;
BigInteger a = this.num;
BigInteger b = this.den.multiply(m);
toReturn = new BigRational(a.mod(b), den);
return toReturn;
}
}
示例5: bigIntegerToHex
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Convert BigInteger pbKey/pbExp to hexadecimal pbKey/pbExp
* @param key pbKey/pbExp in integer format
* @return pbKey/pbExp in hexadecimal format
*/
public static String bigIntegerToHex(BigInteger key) {
String digits = "0123456789ABCDEF";
if (key.compareTo(BigInteger.ZERO) == 0) return "0";
String hex = "";
while (key.compareTo(BigInteger.ZERO) > 0) {
BigInteger digit = key.mod(BigInteger.valueOf(16));
hex = digits.charAt(digit.intValueExact()) + hex;
key = key.divide(BigInteger.valueOf(16));
}
return hex;
}
示例6: unblindMessage
import java.math.BigInteger; //导入方法依赖的package包/类
private BigInteger unblindMessage(
BigInteger blindedMsg)
{
BigInteger m = key.getModulus();
BigInteger msg = blindedMsg;
BigInteger blindFactorInverse = blindingFactor.modInverse(m);
msg = msg.multiply(blindFactorInverse);
msg = msg.mod(m);
return msg;
}
示例7: validatePublicValue
import java.math.BigInteger; //导入方法依赖的package包/类
public static BigInteger validatePublicValue(BigInteger N, BigInteger val)
throws CryptoException
{
val = val.mod(N);
// Check that val % N != 0
if (val.equals(ZERO))
{
throw new CryptoException("Invalid public value: 0");
}
return val;
}
示例8: publicPointFromPrivate
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Returns public key point from the given private key. To convert a byte array into a BigInteger, use <tt>
* new BigInteger(1, bytes);</tt>
*/
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
/*
* TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group order,
* but that could change in future versions.
*/
if (privKey.bitLength() > CURVE.getN().bitLength()) {
privKey = privKey.mod(CURVE.getN());
}
return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
示例9: modularExponential
import java.math.BigInteger; //导入方法依赖的package包/类
public static BigInteger modularExponential(long base, long power, long mod) {
BigInteger result = BigInteger.ONE;
base = base % mod;
while(power > 0) {
if(power % 2 == 1) {
result = result.multiply(BigInteger.valueOf(base));
result = result.mod(BigInteger.valueOf(mod));
}
power = power / 2;
base = (base * base) % mod;
}
return result;
}
示例10: ECPrivKey
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Generate a random private key with ECDomainParameters dp
*/
public ECPrivKey(ECDomainParameters dp) {
this.dp = (ECDomainParameters) dp.clone();
SecureRandom rnd = new SecureRandom();
s = new BigInteger(dp.m, rnd);
s = s.mod(dp.r);
}
示例11: publicPointFromPrivate
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Returns public key point from the given private key. To convert a byte
* array into a BigInteger, use <tt>
* new BigInteger(1, bytes);</tt>
*/
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
/*
* TODO: FixedPointCombMultiplier currently doesn't support scalars
* longer than the group order, but that could change in future
* versions.
*/
if (privKey.bitLength() > CURVE.getN().bitLength()) {
privKey = privKey.mod(CURVE.getN());
}
return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
示例12: changePassword
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Update password based on old password, device share, and cloud shares.
*
* @param cloudShares Current cloud shares.
* @return Updated cloud shares.
*/
public SecretShare[] changePassword(SecretShare oldPassword, SecretShare newPassword, SecretShare[] cloudShares) {
BigInteger newPy = newPassword.getShareY();
BigInteger oldPy = oldPassword.getShareY();
BigInteger acc = newPy.subtract(oldPy);
acc = acc.mod(PRIME);
return proactivizeCloudShares(acc, BigInteger.ZERO, cloudShares);
}
示例13: generateShares
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Generates Y values given X values using a polynomial which is based on the
* threshold and a secret value. <code>threshold-1</code> is the degree of
* the polynomial. The secret is the Y intercept (x=0 value). Coefficients
* of the polynomial are randomly generated.
*
* For <code>threshold = 3</code>, the polynomial is given by:<br>
* y = secret + a * x + b * x**2
*
* The number of shares that the secret is split into is given by the number of X values.
*
* @param secret The value to protect. This will be the x=0 value.
* @param xValues The x coordinates of the shares. Note that none of them should be zero.
* All xValues should be unique.
* @param coefficients The coefficients of the polynomial. All the values should be smaller than p.
* There should be threshold-1 coefficients.
* @return The y coordinates of the shares.
*/
private static BigInteger[] generateShares(BigInteger secret, BigInteger[] xValues,
BigInteger[] coefficients) {
// Calculate the Y values given the X values and coefficients.
int numShares = xValues.length;
int numCoefficients = coefficients.length;
BigInteger[] yVals = new BigInteger[numShares];
BigInteger k;
for(int i = 0; i < numShares; i++){
BigInteger accumulator = BigInteger.ZERO;
k = BigInteger.ONE;
for(int j=0; j < numCoefficients; j++){
// Calculate coefficient * x ** k
BigInteger exp = xValues[i].modPow(k, PRIME);
BigInteger mult = exp.multiply(coefficients[j]);
k = k.add(BigInteger.ONE);
// Accumulate the term
BigInteger add1 = accumulator.add(mult);
accumulator = add1.mod(PRIME);
}
// Accumulate the constant term; which is the secret.
BigInteger add2 = accumulator.add(secret);
accumulator = add2.mod(PRIME);
yVals[i] = accumulator;
}
return yVals;
}
示例14: Technology
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Compute the jacobi symbol <code>(a/n)</code>, as described in:
* <a href="http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf">Digital signature standard (DSS). FIPS PUB 186-4, National Institute of Standards and
Technology (NIST), 2013.</a>, pp. 76-77
* @param initial_a the starting value of a
* @param n the value of n
* @return the computed jacobi symbol
*/
public int computeJacobiSymbol(BigInteger initial_a, BigInteger n) {
// Step 1: a = a mod n
BigInteger a = initial_a.mod(n);
// Step 2: if a = 1 or n = 1 return 1
if (a.equals(BigInteger.ONE) || n.equals(BigInteger.ONE)) {
return 1;
}
// Step 3: if a = 0 return 0
if (a.equals(BigInteger.ZERO)) {
return 0;
}
// Step 4: define e and a_1 such that a = 2^e * a_1 where a_1 is odd
int e = 0;
BigInteger a_1 = a;
while (a_1.remainder(BigIntegers.TWO).equals(BigInteger.ZERO)) {
e++;
a_1 = a_1.divide(BigIntegers.TWO);
}
// Step 5: if e is even, then s = 1;
// else if n mod 8 = 1 or n mod 8 = 7, then s = 1
// else if n mod 8 = 3 or n mod 8 = 5, then s = -1
int s;
if (e % 2 == 0) {
s = 1;
} else {
BigInteger n_mod_eight = n.mod(BigIntegers.EIGHT);
if (n_mod_eight.equals(BigInteger.ONE) || n_mod_eight.equals(BigIntegers.SEVEN)) {
s = 1;
} else { // n_mod_eight.equals(THREE) || n_mod_eight.equals(FIVE)
s = -1;
}
}
// Step 6: if n mod 4 = 3 and a_1 mod 4 = 3, then s = -s
if (n.mod(BigIntegers.FOUR).equals(BigIntegers.THREE) && a_1.mod(BigIntegers.FOUR).equals(BigIntegers.THREE)) {
s = -s;
}
// Step 7: n_1 = n mod a_1
BigInteger n_1 = n.mod(a_1);
// Step 8: return s * JacobiSymbol(n_1, a_1)
return s * computeJacobiSymbol(n_1, a_1);
}
示例15: addCryptedBlocks
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Adds the contents of two encrypted blocks mod sigma
*
* @param block1
* the first encrypted block
* @param block2
* the second encrypted block
* @return encrypt((block1 + block2) mod sigma)
* @throws InvalidCipherTextException
*/
public byte[] addCryptedBlocks(byte[] block1, byte[] block2)
throws InvalidCipherTextException
{
// check for correct blocksize
if (forEncryption)
{
if ((block1.length > getOutputBlockSize())
|| (block2.length > getOutputBlockSize()))
{
throw new InvalidCipherTextException(
"BlockLength too large for simple addition.\n");
}
}
else
{
if ((block1.length > getInputBlockSize())
|| (block2.length > getInputBlockSize()))
{
throw new InvalidCipherTextException(
"BlockLength too large for simple addition.\n");
}
}
// calculate resulting block
BigInteger m1Crypt = new BigInteger(1, block1);
BigInteger m2Crypt = new BigInteger(1, block2);
BigInteger m1m2Crypt = m1Crypt.multiply(m2Crypt);
m1m2Crypt = m1m2Crypt.mod(key.getModulus());
if (debug)
{
System.out.println("c(m1) as BigInteger:....... " + m1Crypt);
System.out.println("c(m2) as BigInteger:....... " + m2Crypt);
System.out.println("c(m1)*c(m2)%n = c(m1+m2)%n: " + m1m2Crypt);
}
byte[] output = key.getModulus().toByteArray();
Arrays.fill(output, (byte)0);
System.arraycopy(m1m2Crypt.toByteArray(), 0, output, output.length
- m1m2Crypt.toByteArray().length,
m1m2Crypt.toByteArray().length);
return output;
}