List of usage examples for java.security NoSuchAlgorithmException printStackTrace
public void printStackTrace()
From source file:org.opendatakit.briefcase.util.FileSystemUtils.java
private static boolean decryptSubmissionFiles(String base64EncryptedSymmetricKey, FormInstanceMetadata fim, List<String> mediaNames, String encryptedSubmissionFile, String base64EncryptedElementSignature, PrivateKey rsaPrivateKey, File instanceDir, File unencryptedDir) throws FileSystemException, CryptoException, ParsingException { EncryptionInformation ei = new EncryptionInformation(base64EncryptedSymmetricKey, fim.instanceId, rsaPrivateKey);//from w w w. j av a 2s .c o m byte[] elementDigest; try { // construct the base64-encoded RSA-encrypted symmetric key Cipher pkCipher; pkCipher = Cipher.getInstance(ASYMMETRIC_ALGORITHM); // extract digest pkCipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey); byte[] encryptedElementSignature = Base64.decodeBase64(base64EncryptedElementSignature); elementDigest = pkCipher.doFinal(encryptedElementSignature); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new CryptoException("Error decrypting base64EncryptedElementSignature Cause: " + e.toString()); } catch (NoSuchPaddingException e) { e.printStackTrace(); throw new CryptoException("Error decrypting base64EncryptedElementSignature Cause: " + e.toString()); } catch (InvalidKeyException e) { e.printStackTrace(); throw new CryptoException("Error decrypting base64EncryptedElementSignature Cause: " + e.toString()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); throw new CryptoException("Error decrypting base64EncryptedElementSignature Cause: " + e.toString()); } catch (BadPaddingException e) { e.printStackTrace(); throw new CryptoException("Error decrypting base64EncryptedElementSignature Cause: " + e.toString()); } // NOTE: will decrypt only the files in the media list, plus the encryptedSubmissionFile File[] allFiles = instanceDir.listFiles(); List<File> filesToProcess = new ArrayList<File>(); for (File f : allFiles) { if (mediaNames.contains(f.getName())) { filesToProcess.add(f); } else if (encryptedSubmissionFile.equals(f.getName())) { filesToProcess.add(f); } } // should have all media files plus one submission.xml.enc file if (filesToProcess.size() != mediaNames.size() + 1) { // figure out what we're missing... int lostFileCount = 0; List<String> missing = new ArrayList<String>(); for (String name : mediaNames) { if (name == null) { // this was lost due to an pre-ODK Aggregate 1.4.5 mark-as-complete action ++lostFileCount; continue; } File f = new File(instanceDir, name); if (!filesToProcess.contains(f)) { missing.add(name); } } StringBuilder b = new StringBuilder(); for (String name : missing) { b.append(" ").append(name); } if (!filesToProcess.contains(new File(instanceDir, encryptedSubmissionFile))) { b.append(" ").append(encryptedSubmissionFile); throw new FileSystemException( "Error decrypting: " + instanceDir.getName() + " Missing files:" + b.toString()); } else { // ignore the fact that we don't have the lost files if (filesToProcess.size() + lostFileCount != mediaNames.size() + 1) { throw new FileSystemException( "Error decrypting: " + instanceDir.getName() + " Missing files:" + b.toString()); } } } // decrypt the media files IN ORDER. for (String mediaName : mediaNames) { String displayedName = (mediaName == null) ? "<missing .enc file>" : mediaName; File f = (mediaName == null) ? null : new File(instanceDir, mediaName); try { decryptFile(ei, f, unencryptedDir); } catch (InvalidKeyException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + displayedName + " Cause: " + e.toString()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + displayedName + " Cause: " + e.toString()); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + displayedName + " Cause: " + e.toString()); } catch (NoSuchPaddingException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + displayedName + " Cause: " + e.toString()); } catch (IOException e) { e.printStackTrace(); throw new FileSystemException("Error decrypting:" + displayedName + " Cause: " + e.toString()); } } // decrypt the submission file File f = new File(instanceDir, encryptedSubmissionFile); try { decryptFile(ei, f, unencryptedDir); } catch (InvalidKeyException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + f.getName() + " Cause: " + e.toString()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + f.getName() + " Cause: " + e.toString()); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + f.getName() + " Cause: " + e.toString()); } catch (NoSuchPaddingException e) { e.printStackTrace(); throw new CryptoException("Error decrypting:" + f.getName() + " Cause: " + e.toString()); } catch (IOException e) { e.printStackTrace(); throw new FileSystemException("Error decrypting:" + f.getName() + " Cause: " + e.toString()); } // get the FIM for the decrypted submission file File submissionFile = new File(unencryptedDir, encryptedSubmissionFile.substring(0, encryptedSubmissionFile.lastIndexOf(".enc"))); FormInstanceMetadata submissionFim; try { Document subDoc = XmlManipulationUtils.parseXml(submissionFile); submissionFim = XmlManipulationUtils.getFormInstanceMetadata(subDoc.getRootElement()); } catch (ParsingException e) { e.printStackTrace(); throw new FileSystemException( "Error decrypting: " + submissionFile.getName() + " Cause: " + e.toString()); } catch (FileSystemException e) { e.printStackTrace(); throw new FileSystemException( "Error decrypting: " + submissionFile.getName() + " Cause: " + e.getMessage()); } boolean same = submissionFim.xparam.formId.equals(fim.xparam.formId); if (!same) { throw new FileSystemException("Error decrypting:" + unencryptedDir.getName() + " Cause: form instance metadata differs from that in manifest"); } // Construct the element signature string StringBuilder b = new StringBuilder(); appendElementSignatureSource(b, fim.xparam.formId); if (fim.xparam.modelVersion != null) { appendElementSignatureSource(b, Long.toString(fim.xparam.modelVersion)); } appendElementSignatureSource(b, base64EncryptedSymmetricKey); appendElementSignatureSource(b, fim.instanceId); boolean missingFile = false; for (String encFilename : mediaNames) { if (encFilename == null) { missingFile = true; continue; } File decryptedFile = new File(unencryptedDir, encFilename.substring(0, encFilename.lastIndexOf(".enc"))); if (decryptedFile.getName().endsWith(".missing")) { // this is a missing file -- we will not be able to // confirm the signature of the submission. missingFile = true; continue; } String md5 = FileSystemUtils.getMd5Hash(decryptedFile); appendElementSignatureSource(b, decryptedFile.getName() + "::" + md5); } String md5 = FileSystemUtils.getMd5Hash(submissionFile); appendElementSignatureSource(b, submissionFile.getName() + "::" + md5); // compute the digest of the element signature string byte[] messageDigest; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(b.toString().getBytes("UTF-8")); messageDigest = md.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new CryptoException("Error computing xml signature Cause: " + e.toString()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new CryptoException("Error computing xml signature Cause: " + e.toString()); } same = true; for (int i = 0; i < messageDigest.length; ++i) { if (messageDigest[i] != elementDigest[i]) { same = false; break; } } return same; }
From source file:com.codemage.sql.util.PasswordHasher.java
public String getHashcode(String passwordToHash) { String generatedPassword = null; try {/*from ww w . j a v a2 s. co m*/ // Create MessageDigest instance for MD5 MessageDigest md = MessageDigest.getInstance("MD5"); //Add password bytes to digest md.update(passwordToHash.getBytes()); //Get the hash's bytes byte[] bytes = md.digest(); //This bytes[] has bytes in decimal format; //Convert it to hexadecimal format StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } //Get complete hashed password in hex format generatedPassword = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return generatedPassword; }
From source file:org.bimserver.tests.TestEnc.java
private void start() { byte[] key = new byte[16]; new Random().nextBytes(key); SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); String toEncrypt = "1"; try {/* w w w. j av a 2 s .com*/ Cipher encodingCipher = Cipher.getInstance("AES"); encodingCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encodedBytes = encodingCipher.doFinal(toEncrypt.getBytes(Charsets.UTF_8)); System.out.println("Encoded size: " + encodedBytes.length); String encodedHexString = new String(Hex.encodeHex(encodedBytes)); System.out.println("Encoded hex: " + encodedHexString); Cipher decodingCipher = Cipher.getInstance("AES"); decodingCipher.init(Cipher.DECRYPT_MODE, secretKeySpec); ByteBuffer wrap = ByteBuffer .wrap(decodingCipher.doFinal(Hex.decodeHex(encodedHexString.toCharArray()))); String result = new String(wrap.array(), Charsets.UTF_8); System.out.println(result); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (DecoderException e) { e.printStackTrace(); } }
From source file:com.bamobile.fdtks.util.Tools.java
/** * Returns a SHA1 digest of the given array of bytes, in hex values lowercase. *//* ww w.j a va 2 s. c o m*/ public static String sha1(byte[] in) { String res; MessageDigest digest = null; try { digest = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } String tmp; digest.update(in, 0, in.length); byte out[] = digest.digest(); // builds the hex string (lowercase) res = ""; tmp = ""; // tmp = two characters to append to the hex string for (int i = 0; i < 20; i++) { int unsigned = out[i]; if (out[i] < 0) { unsigned += 256; } tmp = Integer.toHexString(unsigned); if (tmp.length() == 1) { tmp = "0" + tmp; } res = res + tmp; } return res; }
From source file:de.phoenix.util.hash.SHA1Hasher.java
public String generate(byte[] bytes) { try {/*from w w w . java 2 s . co m*/ MessageDigest ms = MessageDigest.getInstance(ALGORITHM); ms.update(bytes); return Hex.encodeHexString(ms.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:com.bamobile.fdtks.util.Tools.java
/** * Returns a SHA1 digest of the given string, in hex values lowercase. * * @param _str/*from ww w. j a v a2 s . co m*/ */ public static String sha1(String _str) { String res; MessageDigest digest = null; try { digest = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } String tmp = _str; byte in[] = tmp.getBytes(); digest.update(in, 0, in.length); byte out[] = digest.digest(); // builds the hex string (lowercase) res = ""; tmp = ""; // tmp = two characters to append to the hex string for (int i = 0; i < 20; i++) { int unsigned = out[i]; if (out[i] < 0) { unsigned += 256; } tmp = Integer.toHexString(unsigned); if (tmp.length() == 1) { tmp = "0" + tmp; } res = res + tmp; } return res; }
From source file:me.vertretungsplan.objects.credential.BaseCredential.java
protected String hash(String data) { try {/* w w w. ja va 2 s. co m*/ MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hashBytes = digest.digest(data.getBytes()); return Base64.encodeBase64URLSafeString(hashBytes); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }
From source file:com.imaginamos.taxisya.taxista.activities.RegisterDriverActivity.java
public static final String md5(final String s) { final String MD5 = "MD5"; try {// ww w . j a v a 2s.co m // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance(MD5); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuilder hexString = new StringBuilder(); for (byte aMessageDigest : messageDigest) { String h = Integer.toHexString(0xFF & aMessageDigest); while (h.length() < 2) h = "0" + h; hexString.append(h); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }
From source file:de.phoenix.util.hash.SHA1Hasher.java
public String generate(InputStream binaryStream) { try {/*w w w.j av a 2s . co m*/ MessageDigest ms = MessageDigest.getInstance(ALGORITHM); for (int read = 0; (read = binaryStream.read(buffer)) != -1;) { ms.update(buffer, 0, read); } return Hex.encodeHexString(ms.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:com.wabacus.util.DesEncryptTools.java
public static boolean createEncryptKey(File file) { SecretKey deskey = null;//from w w w .j a v a2 s . co m try { KeyGenerator keygen = KeyGenerator.getInstance("DESede"); deskey = keygen.generateKey(); } catch (NoSuchAlgorithmException e) { throw new WabacusConfigLoadingException("?", e); } if (deskey == null) return false; FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(deskey); } catch (FileNotFoundException e) { throw new WabacusConfigLoadingException( "?" + file.getPath(), e); } catch (IOException e) { throw new WabacusConfigLoadingException( "?" + file.getPath() + "", e); } finally { try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); return false; } try { if (oos != null) oos.close(); } catch (IOException e) { e.printStackTrace(); return false; } } return true; }