Code example for NoSuchAlgorithmException

Methods: printStackTrace, toString

0
			sr.nextBytes(salt);
			if (debug) Log.d(TAG,"generateSalt: salt="+salt.toString());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			throw e;
		} 
		return toHexString(salt);
	} 
	/** 
	 * @author Isaac Potoczny-Jones 
	 *  
	 * @return null if failure, otherwise hex string version of key 
	 */ 
	public static String generateMasterKey () throws NoSuchAlgorithmException {
		try { 
			KeyGenerator keygen;
			keygen = KeyGenerator.getInstance("AES");
			keygen.init(256);
			SecretKey genDesKey = keygen.generateKey();
			return toHexString(genDesKey.getEncoded());
		} catch (NoSuchAlgorithmException e) {
			Log.e(TAG,"generateMasterKey(): "+e.toString());
			throw e;
		} 
	} 
 
	/** 
	 *  
	 * @param message 
	 * @return MD5 digest of message in a byte array 
	 * @throws NoSuchAlgorithmException 
	 * @throws IOException 
	 */ 
	public static byte[] md5String(String message) {
	 
		byte[] input = message.getBytes();
	 
		MessageDigest hash;
		ByteArrayInputStream	bIn = null;
		DigestInputStream	dIn = null;
	 
		try { 
			hash = MessageDigest.getInstance("MD5");
	 
			bIn = new ByteArrayInputStream(input);
			dIn = new DigestInputStream(bIn, hash);
	 
			for(int i=0;i<input.length;i++) {
				dIn.read();
			} 
	 
		} catch (NoSuchAlgorithmException e) {
			Log.e(TAG,"md5String(): "+e.toString());
		} catch (IOException e) {
			Log.e(TAG,"md5String(): "+e.toString());
		} 
	 
		return dIn.getMessageDigest().digest();
	} 
 
	/** 
	 *  
	 * @param bytes 
	 * @return String version in Hex format of input byte array 
	 */ 
	public static String toHexString(byte bytes[]) {
	 
		StringBuffer retString = new StringBuffer();
		for (int i = 0; i < bytes.length; ++i) {
			retString.append(Integer
					.toHexString(0x0100 + (bytes[i] & 0x00FF))
					.substring(1));
		} 
		return retString.toString();
	} 
 
	public static byte[] hexStringToBytes(String hex) {
 
		byte [] bytes = new byte [hex.length() / 2];
		int j = 0;
		for (int i = 0; i < hex.length(); i += 2)
		{ 
			try { 
				String hexByte=hex.substring(i, i+2);
 
				Integer I = Integer.valueOf(0);
				I = Integer.decode("0x"+hexByte);
				int k = I.intValue ();
				bytes[j++] = Integer.valueOf(k).byteValue();
			} catch (NumberFormatException e)
			{ 
				Log.i(TAG,e.getLocalizedMessage());
				return bytes;
			} catch (StringIndexOutOfBoundsException e)
			{ 
				Log.i(TAG,"StringIndexOutOfBoundsException");
				return bytes;
			} 
		} 
		return bytes;
	} 
 
	/** 
	 * Set the password to be used as an encryption key 
	 *  
	 * @param pass - might be a user-entered key, or one generated by generateMasterKey. 
	 * @throws Exception 
	 */ 
	public void setPassword(String pass) {
		if (debug) Log.d(TAG,"setPassword("+pass+")");
		password = pass;
		pbeKeySpec = new PBEKeySpec(password.toCharArray());
		try { 
			pbeKey = keyFac.generateSecret(pbeKeySpec);
			pbeCipher = Cipher
					.getInstance(algorithm,"BC");
		} catch (InvalidKeySpecException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchAlgorithmException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchProviderException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} catch (NoSuchPaddingException e) {
			Log.e(TAG,"setPassword(): "+e.toString());
		} 
		 
		// Every time we set a new password, also the session key changes: