List of usage examples for java.math BigInteger BigInteger
private BigInteger(byte[] magnitude, int signum)
From source file:Main.java
public static void main(String[] args) throws Exception { Random randomGenerator = SecureRandom.getInstance("SHA1PRNG"); BigInteger randomInteger = new BigInteger(1024, randomGenerator); System.out.println(randomInteger); }
From source file:Main.java
public static void main(String[] args) throws Exception { int bitLength = 512; // 512 bits Random rnd = new Random(); System.out.println("BitLength : " + bitLength); BigInteger mod = new BigInteger(bitLength, rnd); }
From source file:ClientServicesQA.java
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String id = "LNWS-AM38-12M9"; id = id.replace("-", ""); System.out.println(id);/* ww w. j a v a 2 s. co m*/ byte[] bytes = {}; BigInteger big = new BigInteger(id, 36); System.out.println(big); /* ClientServicesQA cs = new ClientServicesQA(); WebDriver driver; Calendar cal = Calendar.getInstance(); int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); driver = new FirefoxDriver(); String baseURL = "https://login-aws-qa.tegrity.com/Service/login.aspx"; // cs.loginToClientServices(baseURL, driver); // cs.createNewTegrityCustomer(driver); cs.createNewInstitution(driver, dayOfMonth, month, year); // System.out.println("The current day of the month is: " + dayOfMonth); // System.out.println("The current month is: " + month); */ }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", "BC"); EllipticCurve curve = new EllipticCurve( new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffeffffffffffffffff", 16)), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)); ECParameterSpec ecSpec = new ECParameterSpec(curve, new ECPoint(new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), 1); keyGen.initialize(ecSpec, new SecureRandom()); KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC"); KeyPair aPair = keyGen.generateKeyPair(); KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "BC"); KeyPair bPair = keyGen.generateKeyPair(); aKeyAgree.init(aPair.getPrivate());/*from ww w .j a v a2 s .c o m*/ bKeyAgree.init(bPair.getPrivate()); aKeyAgree.doPhase(bPair.getPublic(), true); bKeyAgree.doPhase(aPair.getPublic(), true); MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = new byte[] { (byte) 0xbe, (byte) 0xef }; Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC"); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("12345678", 16), new BigInteger("11", 16)); RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger("12345678", 16), new BigInteger("12345678", 16)); RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] cipherText = cipher.doFinal(input); System.out.println("cipher: " + new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println("plain : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", "BC"); EllipticCurve curve = new EllipticCurve( new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffeffffffffffffffff", 16)), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)); ECParameterSpec ecSpec = new ECParameterSpec(curve, new ECPoint(new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)), new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), 1); keyGen.initialize(ecSpec, new SecureRandom()); KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC"); KeyPair aPair = keyGen.generateKeyPair(); KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "BC"); KeyPair bPair = keyGen.generateKeyPair(); aKeyAgree.init(aPair.getPrivate());// www . j av a2 s.com bKeyAgree.init(bPair.getPrivate()); aKeyAgree.doPhase(bPair.getPublic(), true); bKeyAgree.doPhase(aPair.getPublic(), true); MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "2F78C7"; BigInteger base = BigInteger.valueOf(2); BigInteger modulus = new BigInteger(s, 16); DHParameterSpec skipParameterSpec = new DHParameterSpec(modulus, base); KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH"); kpg1.initialize(skipParameterSpec);/*from ww w. j a v a 2s . co m*/ KeyPair kp1 = kpg1.generateKeyPair(); KeyAgreement ka1 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate(); DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic(); ka1.init(privateKey1); System.out.println("1 is using " + publicKey1.getY() + " for its public key"); KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH"); kpg2.initialize(skipParameterSpec); KeyPair kp2 = kpg2.generateKeyPair(); KeyAgreement ka2 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate(); DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic(); ka2.init(privateKey2); System.out.println("2 is using " + publicKey2.getY() + " for its public key"); ka1.doPhase(publicKey2, true); byte[] sharedKey1 = ka1.generateSecret(); System.out.println("1 is using " + new BigInteger(1, sharedKey1) + " for its shared key"); ka2.doPhase(publicKey1, true); byte[] sharedKey2 = ka2.generateSecret(); System.out.println("2 is using " + new BigInteger(1, sharedKey2) + " for its shared key"); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "2F78C7"; BigInteger base = BigInteger.valueOf(2); BigInteger modulous = new BigInteger(s, 16); DHParameterSpec skipParameterSpec = new DHParameterSpec(modulous, base); KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH"); kpg1.initialize(skipParameterSpec);//from w ww. j a va2 s .c om KeyPair kp1 = kpg1.generateKeyPair(); KeyAgreement ka1 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate(); DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic(); ka1.init(privateKey1); System.out.println("1 is using " + publicKey1.getY() + " for its public key"); KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH"); kpg2.initialize(skipParameterSpec); KeyPair kp2 = kpg2.generateKeyPair(); KeyAgreement ka2 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate(); DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic(); ka2.init(privateKey2); System.out.println("2 is using " + publicKey2.getY() + "for its public key"); // Use the KeyAgreement object of 1 to generate its shared key ka1.doPhase(publicKey2, true); SecretKey sharedKey1 = ka1.generateSecret("DES"); System.out.println("1 is using " + new String(sharedKey1.getEncoded()) + " as its DES session key"); // Use the KeyAgreement object of 2 to generate its shared key ka2.doPhase(publicKey1, true); SecretKey sharedKey2 = ka2.generateSecret("DES"); System.out.println("2 is using " + new String(sharedKey2.getEncoded()) + "as its DES session key"); }
From source file:HashCollider.java
/**i * @param args the command line arguments *//*from w ww.j a v a2 s .co m*/ public static void main(String[] args) { Random random = new Random(); byte[] nbytes1 = new byte[64]; byte[] nbytes2 = new byte[64]; float average = 0; // set number of bits for checking collision int number_of_bits = 4; random.nextBytes(nbytes1); random.nextBytes(nbytes2); String sha1 = DigestUtils.sha256Hex(nbytes1); String sha2 = DigestUtils.sha256Hex(nbytes2); String nsh1 = new BigInteger(sha1, 16).toString(2); String nsh2 = new BigInteger(sha2, 16).toString(2); int i = 0; for (int j = 0; j < 5; j++) { while (true) { if (nsh1.substring(0, number_of_bits - 1).equals(nsh2.substring(0, number_of_bits - 1))) { System.out.println(i); average += i; break; } i++; // System.out.println("......"+i); random.nextBytes(nbytes1); random.nextBytes(nbytes2); sha1 = DigestUtils.sha256Hex(nbytes1); sha2 = DigestUtils.sha256Hex(nbytes2); nsh1 = new BigInteger(sha1, 16).toString(2); nsh2 = new BigInteger(sha2, 16).toString(2); } i = 0; // System.out.println(nsh1+"\n"+nsh2); random.nextBytes(nbytes1); random.nextBytes(nbytes2); sha1 = DigestUtils.sha256Hex(nbytes1); sha2 = DigestUtils.sha256Hex(nbytes2); nsh1 = new BigInteger(sha1, 16).toString(2); nsh2 = new BigInteger(sha2, 16).toString(2); } // System.out.println("Average for " + number_of_bits + " bits is " + average / 5); }
From source file:dhtaccess.tools.Get.java
public static void main(String[] args) { boolean details = false; // parse properties Properties prop = System.getProperties(); String gateway = prop.getProperty("dhtaccess.gateway"); if (gateway == null || gateway.length() <= 0) { gateway = DEFAULT_GATEWAY;/* w w w . j a v a2s . c o m*/ } // parse options Options options = new Options(); options.addOption("h", "help", false, "print help"); options.addOption("g", "gateway", true, "gateway URI, list at http://opendht.org/servers.txt"); options.addOption("d", "details", false, "print secret hash and TTL"); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println("There is an invalid option."); e.printStackTrace(); System.exit(1); } String optVal; if (cmd.hasOption('h')) { usage(COMMAND); System.exit(1); } optVal = cmd.getOptionValue('g'); if (optVal != null) { gateway = optVal; } if (cmd.hasOption('d')) { details = true; } args = cmd.getArgs(); // parse arguments if (args.length < 1) { usage(COMMAND); System.exit(1); } // prepare for RPC DHTAccessor accessor = null; try { accessor = new DHTAccessor(gateway); } catch (MalformedURLException e) { e.printStackTrace(); System.exit(1); } for (int index = 0; index < args.length; index++) { byte[] key = null; try { key = args[index].getBytes(ENCODE); } catch (UnsupportedEncodingException e1) { // NOTREACHED } // RPC if (args.length > 1) { System.out.println(args[index] + ":"); } if (details) { Set<DetailedGetResult> results = accessor.getDetails(key); for (DetailedGetResult r : results) { String valString = null; try { valString = new String((byte[]) r.getValue(), ENCODE); } catch (UnsupportedEncodingException e) { // NOTREACHED } BigInteger hashedSecure = new BigInteger(1, (byte[]) r.getHashedSecret()); System.out.println(valString + " " + r.getTTL() + " " + r.getHashType() + " 0x" + ("0000000" + hashedSecure.toString(16)).substring(0, 8)); } } else { Set<byte[]> results = accessor.get(key); for (byte[] valBytes : results) { try { System.out.println(new String((byte[]) valBytes, ENCODE)); } catch (UnsupportedEncodingException e) { // NOTREACHED } } } } // for (int index = 0... }