コード例 #1
0
ファイル: RSA.java プロジェクト: templetonm/NetSecurity
  public RSA(Random random) {
    savedRandom = random;

    p = getPrime(random); // p and q are arbitrary large primes
    q = getPrime(random);
    n = p.multiply(q);
    phiN = (p.subtract(ONE)).multiply(q.subtract(ONE));

    S = getPrime(random); // s is an arbitrary secret; we'll use a prime
    V = S.modPow(new BigInteger("2"), n);
  }
コード例 #2
0
ファイル: BinShift.java プロジェクト: fpapai/basex
  @Override
  public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
    final B64 b64 = toB64(exprs[0], qc, true);
    long by = toLong(exprs[1], qc);
    if (b64 == null) return null;
    if (by == 0) return b64;

    byte[] bytes = b64.binary(info);
    final int bl = bytes.length;

    byte[] tmp = new byte[bl];
    int r = 0;
    if (by > 7) {
      tmp = new BigInteger(bytes).shiftLeft((int) by).toByteArray();
      if (tmp.length != bl) {
        bytes = tmp;
        tmp = new byte[bl];
        System.arraycopy(bytes, bytes.length - bl, tmp, 0, bl);
      }
    } else if (by > 0) {
      for (int i = bl - 1; i >= 0; i--) {
        final byte b = bytes[i];
        tmp[i] = (byte) (b << by | r);
        r = b >>> 32 - by;
      }
    } else if (by > -8) {
      by = -by;
      for (int i = 0; i < bl; i++) {
        final int b = bytes[i] & 0xFF;
        tmp[i] = (byte) (b >>> by | r);
        r = b << 32 - by;
      }
    } else {
      by = -by;
      BigInteger bi = new BigInteger(bytes);
      if (bi.signum() >= 0) {
        bi = bi.shiftRight((int) by);
      } else {
        final BigInteger o = BigInteger.ONE.shiftLeft(bl * 8 + 1);
        final BigInteger m = o.subtract(BigInteger.ONE).shiftRight((int) by + 1);
        bi = bi.subtract(o).shiftRight((int) by).and(m);
      }
      tmp = bi.toByteArray();
      final int tl = tmp.length;
      if (tl != bl) {
        bytes = tmp;
        tmp = new byte[bl];
        System.arraycopy(bytes, 0, tmp, bl - tl, tl);
      }
    }
    return new B64(tmp);
  }
コード例 #3
0
ファイル: Shuoj1141.java プロジェクト: yahaa/Java
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println(3 ^ 5 ^ 4 ^ 5 ^ 5 ^ 6 ^ 6 ^ 7);
    System.out.println(3 ^ 7);

    int[] a = new int[10];
    for (int i = 0; i < 10; i++) a[i] = input.nextInt();
    for (int aa : a) {
      System.out.print(aa);
    }
    sss(1, 3, 4, 5, 6, 7, 8, 9);

    BigInteger aa =
        new BigInteger(
            "12809348038290834905895869048359034859083495894038590438590348590834905890345");
    BigInteger bb = new BigInteger("111111111111111111111");
    System.out.println(aa.subtract(bb));
    BigDecimal rrrr = new BigDecimal(input.next());
    int aaa;
    double b;
    float c;
    StringBuilder sssss = new StringBuilder("1234234");
    System.out.println();

    Integer ttt = 4;
    Double t1 = 14.2;
  }
 public int Decrypto(BigInteger c) {
   BigInteger nSquare = n.multiply(n);
   BigInteger Ans = c.modPow(lambda, nSquare);
   Ans = Ans.subtract(BigInteger.ONE).divide(n);
   Ans = Ans.remainder(n).multiply(nu).remainder(n);
   return Ans.intValue();
 }
コード例 #5
0
ファイル: CipherChatClient.java プロジェクト: oghenez/mycila
 private void makeKeys() {
   PrimeGenerator pg = new PrimeGenerator(513, 10, sr);
   do {
     p = pg.getStrongPrime();
   } while (p.subtract(BigIntegerMath.ONE).mod(BigIntegerMath.THREE).equals(BigIntegerMath.ZERO));
   do {
     q = pg.getStrongPrime();
   } while (q.subtract(BigIntegerMath.ONE).mod(BigIntegerMath.THREE).equals(BigIntegerMath.ZERO));
   modulus = p.multiply(q);
   // Use 3 as enciphering exponent - OK since we are using salt
   decipherExp =
       BigIntegerMath.THREE.modInverse(
           p.subtract(BigIntegerMath.ONE).multiply(q.subtract(BigIntegerMath.ONE)));
   ciphertextBlockSize = (modulus.bitLength() - 1) / 8 + 1;
   // Maximum size of plaintext is 6 bytes less than ciphertext
   // 1 to get under modulus
   // 4 for the salt
   // 1 for a pad byte
   plaintextBlockSize = ciphertextBlockSize - 6;
 }
コード例 #6
0
ファイル: p1083.java プロジェクト: hehaodele/acm-icpc
  void start() throws IOException {
    BigInteger n = new BigInteger(reader.next());
    String s = reader.next();

    BigInteger answer = BigInteger.ONE;
    while (n.compareTo(BigInteger.ONE) > 0) {
      answer = answer.multiply(n);
      n = n.subtract(BigInteger.valueOf(s.length()));
    }
    writer.println(answer);
  }
コード例 #7
0
 private void updateFromScrollBar() {
   beginUpdate();
   BigInteger oldStartVal = startVal;
   startVal = scrollBar.getValueHP();
   constrain();
   model.fireTableDataChanged();
   if (oldStartVal != null) {
     modifySelection(oldStartVal.subtract(startVal).intValue() / addressSize);
   }
   endUpdate();
 }
コード例 #8
0
ファイル: FairWarning.java プロジェクト: vrnss/competitions
 void solve(int caseNum) {
   int n = in.nextInt();
   BigInteger[] past = new BigInteger[n], d = new BigInteger[n];
   for (int i = 0; i < n; i++) past[i] = new BigInteger(in.next());
   Arrays.sort(past);
   for (int i = 0; i < n; i++) d[i] = past[i].subtract(past[0]);
   for (int i = 2; i < n; i++) d[i] = d[i].gcd(d[i - 1]);
   BigInteger gcd = d[n - 1], mod = past[0].mod(gcd);
   System.out.printf("Case #%d: ", caseNum);
   if (mod.equals(BigInteger.ZERO)) System.out.println(0);
   else System.out.println(gcd.subtract(mod));
 }
コード例 #9
0
ファイル: Paillier.java プロジェクト: citp/ThresholdECDSA
  /**
   * Decrypts the given ciphertext.
   *
   * @param c Ciphertext as BigInteger c
   * @return Decrypted value D(c) as BigInteger
   */
  public BigInteger decrypt(BigInteger c) {
    // Check whether everything is set for doing decryption
    if (decryptMode == false) throw new IllegalStateException(this.notReadyForDecryption);
    if (!(key.inModNSPlusOne(c))) throw new IllegalArgumentException("c must be less than n^2");

    BigInteger c1 = null;

    // first we calculate c^d mod n^2
    c1 = c.modPow(deckey.getD(), deckey.getNSPlusOne());

    // after we calculate c1=c^d mod n^2 = (1+n)^(m*d mod n)
    // we now find (c1-1)/n=m*d mod n
    // therefore m= d^-1*(c1-1)/n mod n
    // TODO: Is this true?
    return (deckey.getDInvs().multiply((c1.subtract(BigInteger.ONE)).divide(deckey.getN())))
        .mod(deckey.getN());
  }