List of usage examples for java.security NoSuchAlgorithmException printStackTrace
public void printStackTrace()
From source file:Main.java
public static String getSha(String string, String encoding) { try {/* w ww . ja v a 2 s .c o m*/ byte[] sha = MessageDigest.getInstance("SHA").digest(string.getBytes(encoding)); return new String(encodeHex(sha)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return string; }
From source file:Main.java
public static String SHA1(String str) { try {//from w w w . j a va2 s . c o m MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); digest.update(str.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (byte i : messageDigest) { String shaHex = Integer.toHexString(i & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }
From source file:com.google.api.ads.adwords.awreporting.model.util.UrlHashUtil.java
/** * Creates a SHA-1 Hash of the url// w ww . jav a 2s . c o m * * @param url the url that needs to be hashed * @return a Stri g with a SHA-1 hash of the URL */ public static String createUrlHash(String url) { String hash = null; MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.reset(); messageDigest.update(url.getBytes("UTF-8")); final byte[] resultByte = messageDigest.digest(); hash = new String(Hex.encodeHex(resultByte)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return hash; }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.M) private static void generateNewKey() { try {/*ww w . j a v a 2 s .c om*/ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, KEY_PROVIDER); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT) .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).build()); keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } }
From source file:Main.java
public static String md5_32(String plainText) { String re_md5 = new String(); try {//from w w w. ja va 2 s . c o m MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } re_md5 = buf.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return re_md5; }
From source file:Main.java
private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) { try {// w ww . j av a2 s. c om if (bksFile != null && password != null) { KeyStore e = KeyStore.getInstance("BKS"); e.load(bksFile, password.toCharArray()); KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(e, password.toCharArray()); return keyManagerFactory.getKeyManagers(); } return null; } catch (KeyStoreException var4) { var4.printStackTrace(); } catch (NoSuchAlgorithmException var5) { var5.printStackTrace(); } catch (UnrecoverableKeyException var6) { var6.printStackTrace(); } catch (CertificateException var7) { var7.printStackTrace(); } catch (IOException var8) { var8.printStackTrace(); } catch (Exception var9) { var9.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] getRSAEncryptedData(byte[] dataWithHash) { BigInteger modulus = new BigInteger( "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F", 16);/*from w w w. j a v a2 s. c o m*/ 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:aes_encryption.AES_Encryption.java
public static void setKey(String myKey) { MessageDigest sha = null;/*from w w w. j a v a 2s . com*/ try { key = myKey.getBytes("UTF-8"); System.out.println(key.length); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); //change the bits later 128 then 256 System.out.println(key.length); System.out.println(new String(key, "UTF-8")); secretKey = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:Main.java
public static String GetHash(byte[] data) { MessageDigest md;//from w w w.j a v a 2 s .c om String hash = null; try { md = MessageDigest.getInstance("MD5"); hash = new BigInteger(1, md.digest(data)).toString(16); } catch (NoSuchAlgorithmException e) { Log.i("BMSUtil", "Hashing Error!"); e.printStackTrace(); } return hash; }
From source file:Main.java
@SuppressLint("TrulyRandom") public static SecretKey generateHmacKey(String algorithm) { try {//w ww .j a v a 2 s .c om KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm); SecretKey key = keyGenerator.generateKey(); return key; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }