List of usage examples for java.math BigInteger BigInteger
private BigInteger(byte[] magnitude, int signum)
From source file:Main.java
public static PublicKey getPublicKey(String n, String publicExponent) { KeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(n, 16), new BigInteger(publicExponent, 16)); KeyFactory factory = null;//from w ww. j a v a 2 s . c om PublicKey publicKey = null; try { factory = KeyFactory.getInstance(KEY_ALGORITHM); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } try { publicKey = factory.generatePublic(publicKeySpec); } catch (InvalidKeySpecException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } return publicKey; }
From source file:Main.java
public static boolean isGoodPrime(byte[] prime, int g) { if (!(g >= 2 && g <= 7)) { return false; }/*from w ww. j ava2 s. c o m*/ if (prime.length != 256 || prime[0] >= 0) { return false; } BigInteger dhBI = new BigInteger(1, prime); if (g == 2) { // p mod 8 = 7 for g = 2; BigInteger res = dhBI.mod(BigInteger.valueOf(8)); if (res.intValue() != 7) { return false; } } else if (g == 3) { // p mod 3 = 2 for g = 3; BigInteger res = dhBI.mod(BigInteger.valueOf(3)); if (res.intValue() != 2) { return false; } } else if (g == 5) { // p mod 5 = 1 or 4 for g = 5; BigInteger res = dhBI.mod(BigInteger.valueOf(5)); int val = res.intValue(); if (val != 1 && val != 4) { return false; } } else if (g == 6) { // p mod 24 = 19 or 23 for g = 6; BigInteger res = dhBI.mod(BigInteger.valueOf(24)); int val = res.intValue(); if (val != 19 && val != 23) { return false; } } else if (g == 7) { // p mod 7 = 3, 5 or 6 for g = 7. BigInteger res = dhBI.mod(BigInteger.valueOf(7)); int val = res.intValue(); if (val != 3 && val != 5 && val != 6) { return false; } } String hex = bytesToHex(prime); if (hex.equals( "C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C3720FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F642477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B")) { return true; } BigInteger dhBI2 = dhBI.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(2)); return !(!dhBI.isProbablePrime(30) || !dhBI2.isProbablePrime(30)); }
From source file:Main.java
public static BigInteger getZ(ArrayList<byte[]> c1, ArrayList<byte[]> c2, BigInteger p) { BigInteger z = BigInteger.ZERO; //TODO: make sure c1 and c2 are of the same size int size = c1.size(); if (size > c2.size()) { size = c2.size();/* w ww . j a v a2 s . c o m*/ } for (int i = 0; i < size; i++) { BigInteger c1BI = new BigInteger(1, c1.get(i)); BigInteger c2BI = new BigInteger(1, c2.get(i)); BigInteger exp = new BigInteger(1, ByteBuffer.allocate(8).putLong((long) Math.pow(2, i)).array()); z = z.add((c1BI.multiply(c2BI)).modPow(exp, p)); Log.d("CeCk", "z calculation " + i + "/" + size + " round"); } return z.mod(p); }
From source file:Main.java
public static byte[] getRSAEncryptedData(byte[] dataWithHash) { BigInteger modulus = new BigInteger( "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F", 16);/*from ww w .j a v a 2 s . com*/ BigInteger pubExp = new BigInteger("010001", 16); KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp); RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherData = cipher.doFinal(dataWithHash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalBlockSizeException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvalidKeyException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (BadPaddingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvalidKeySpecException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (NoSuchPaddingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return null; }
From source file:Main.java
public static String sha1(String s) { if (mSha1Digest == null) { try {/*w ww . j av a2 s . com*/ mSha1Digest = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } byte[] data = mSha1Digest.digest(s.getBytes()); return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data)); }
From source file:Main.java
public static String getPwdHash(String password, String salt) { try {//from ww w. j a v a2 s. c o m String temp = password + salt; MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(temp.getBytes("UTF-8")); // Change this to "UTF-16" if needed byte[] digest = md.digest(); return String.format("%0" + (digest.length * 2) + 'x', new BigInteger(1, digest)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Calculates an MD5 hash for a text.//from w ww.j a va 2s .c o m * * @param dataToEncode * The data to encode. * @return A text representation of a hexadecimal value of length 32. */ public static String messageDigestFive(final String dataToEncode) { MessageDigest m; try { m = MessageDigest.getInstance("MD5"); byte[] data = dataToEncode.getBytes(); m.update(data, 0, data.length); BigInteger i = new BigInteger(1, m.digest()); return String.format("%1$032X", i); } catch (NoSuchAlgorithmException e) { // MD5 Should be supported by the runtime! throw new IllegalStateException(e); } }
From source file:Main.java
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec secret = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA256"); mac.init(secret);//from w w w . j a v a 2 s . c om byte[] byteData = mac.doFinal(baseString.getBytes("UTF-8")); BigInteger hash = new BigInteger(1, byteData); String hmac = hash.toString(16); if (hmac.length() % 2 != 0) { hmac = "0" + hmac; } return hmac; }
From source file:business.security.SecureTokenGenerator.java
public static String generateToken() { return new BigInteger(130, rng).toString(32); }
From source file:Main.java
public static String md5Calc(File f) { int i;//w w w . j a v a2s . c o m byte[] buffer = new byte[1024]; int read = 0; String md5hash; try { MessageDigest digest = MessageDigest.getInstance("MD5"); InputStream is = new FileInputStream(f); while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); md5hash = bigInt.toString(16); is.close(); } catch (Exception e) { e.printStackTrace(); return null; } if (md5hash.length() != 33) { String tmp = ""; for (i = 1; i < (33 - md5hash.length()); i++) { tmp = tmp.concat("0"); } md5hash = tmp.concat(md5hash); } return md5hash; }