List of usage examples for java.security NoSuchAlgorithmException getMessage
public String getMessage()
From source file:org.dmb.trueprice.utils.external.PasswordFactory_old.java
public Password createPassword(String humanValue) { // try // { // // Print out 10 hashes // for(int i = 0; i < 10; i++) //// System.out.println(PasswordHash.createHash("p\r\nassw0Rd!")); // System.out.println(createHash(humanValue)); // // // Test password validation // boolean failure = false; // System.out.println("Running tests..."); // for(int i = 0; i < 100; i++) // { // String password = ""+i; // String hash = createHash(password); // String secondHash = createHash(password); // if(hash.equals(secondHash)) { // System.out.println("FAILURE: TWO HASHES ARE EQUAL!"); // failure = true; // } // String wrongPassword = ""+(i+1); // if(validatePassword(wrongPassword, hash)) { // System.out.println("FAILURE: WRONG PASSWORD ACCEPTED!"); // failure = true; // } // if(!validatePassword(password, hash)) { // System.out.println("FAILURE: GOOD PASSWORD NOT ACCEPTED!"); // failure = true; // } // } // if(failure) // System.out.println("TESTS FAILED!"); // else // System.out.println("TESTS PASSED!"); // } // catch(Exception ex) // { // System.out.println("ERROR: " + ex); // } try {/*from ww w . ja v a2 s. com*/ log.info("First Hash >" + createHash(humanValue)); String hash = createHash(humanValue); log.info("second hash >" + hash); hash = createHash(humanValue); log.info("3rd hash >" + hash); hash = createHash(humanValue); log.info("4th hash >" + hash); return new Password(hash); } catch (NoSuchAlgorithmException ex) { log.warn(" No message affected to this exception. Cause : " + ex.getMessage()); } catch (InvalidKeySpecException ex) { log.warn(" No message affected to this exception. Cause : " + ex.getMessage()); } return null; }
From source file:piramide.multimodal.applicationserver.rest.DirectoriesManager.java
private String encode(String arg) { try {//w ww. j av a 2s . c om final MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(arg.getBytes()); byte[] md5sum = digest.digest(); final BigInteger bigInt = new BigInteger(1, md5sum); final String output = bigInt.toString(16); return output; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("MD5 required: " + e.getMessage(), e); } }
From source file:org.dataone.proto.trove.mn.service.v1.impl.MNReadImpl.java
@Override public Checksum getChecksum(Identifier pid, String checksumAlgorithm) throws InvalidRequest, InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, NotFound { // on a membernode it would be more appropriate to calculate the checksum for the object itself // based on the checksum algorithm. if it is provided... otherwise pull from systemmetadata SystemMetadata systemMetadata = this.getSystemMetadata(pid); if ((checksumAlgorithm == null) || systemMetadata.getChecksum().getAlgorithm().equalsIgnoreCase(checksumAlgorithm)) { return systemMetadata.getChecksum(); } else {/* w w w. j av a 2 s.c o m*/ InputStream inputStream = null; try { inputStream = this.get(pid); //new FileInputStream(new File(filePath)); // // possible XXX here... // thread pool it or something with ServiceFailure if too many concurrent threads // return ChecksumUtil.checksum(inputStream, checksumAlgorithm); } catch (NoSuchAlgorithmException ex) { logger.warn(ex.getMessage()); throw new InvalidRequest("1402", ex.getMessage()); } catch (FileNotFoundException ex) { logger.warn(ex.getMessage()); throw new NotFound("1420", ex.getMessage()); } catch (IOException ex) { logger.warn(ex.getMessage()); throw new ServiceFailure("1410", ex.getMessage()); } catch (InsufficientResources ex) { logger.warn(ex.getMessage()); throw new ServiceFailure("1410", ex.getMessage()); } finally { try { inputStream.close(); } catch (IOException ex) { logger.warn(ex.getMessage()); throw new ServiceFailure("1410", ex.getMessage()); } } } }
From source file:AuthSSLProtocolSocketFactory.java
private SSLContext createSSLContext() { try {//from ww w. j a v a 2 s . co m KeyManager[] keymanagers = null; TrustManager[] trustmanagers = null; if (this.keystoreUrl != null) { KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword); Enumeration aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); Certificate[] certs = keystore.getCertificateChain(alias); if (certs != null) { System.out.println("Certificate chain '" + alias + "':"); for (int c = 0; c < certs.length; c++) { if (certs[c] instanceof X509Certificate) { X509Certificate cert = (X509Certificate) certs[c]; System.out.println(" Certificate " + (c + 1) + ":"); System.out.println(" Subject DN: " + cert.getSubjectDN()); System.out.println(" Signature Algorithm: " + cert.getSigAlgName()); System.out.println(" Valid from: " + cert.getNotBefore()); System.out.println(" Valid until: " + cert.getNotAfter()); System.out.println(" Issuer: " + cert.getIssuerDN()); } } } } keymanagers = createKeyManagers(keystore, this.keystorePassword); } if (this.truststoreUrl != null) { KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword); Enumeration aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); System.out.println("Trusted certificate '" + alias + "':"); Certificate trustedcert = keystore.getCertificate(alias); if (trustedcert != null && trustedcert instanceof X509Certificate) { X509Certificate cert = (X509Certificate) trustedcert; System.out.println(" Subject DN: " + cert.getSubjectDN()); System.out.println(" Signature Algorithm: " + cert.getSigAlgName()); System.out.println(" Valid from: " + cert.getNotBefore()); System.out.println(" Valid until: " + cert.getNotAfter()); System.out.println(" Issuer: " + cert.getIssuerDN()); } } trustmanagers = createTrustManagers(keystore); } SSLContext sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(keymanagers, trustmanagers, null); return sslcontext; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage()); } catch (KeyStoreException e) { e.printStackTrace(); throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage()); } catch (GeneralSecurityException e) { e.printStackTrace(); throw new AuthSSLInitializationError("Key management exception: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage()); } }
From source file:org.eurekaclinical.user.service.resource.UserResource.java
/** * Changes a user's password.//from w w w.j a v a 2 s. c om * * @param request the incoming servlet request * @param passwordChangeRequest the request to use to make the password * change * @return the response. * * @throws HttpStatusException Thrown when a password cannot be properly * hashed, or the passwords are mismatched. */ @RolesAllowed({ "researcher", "admin" }) @Path("/passwordchange") @POST public Response changePassword(@Context HttpServletRequest request, PasswordChangeRequest passwordChangeRequest) { String username = request.getUserPrincipal().getName(); LocalUserEntity user = this.localUserDao.getByName(username); Response response = null; if (user == null) { throw new HttpStatusException(Response.Status.NOT_FOUND); } String newPassword = passwordChangeRequest.getNewPassword(); String oldPasswordHash; String newPasswordHash; try { oldPasswordHash = StringUtil.md5(passwordChangeRequest.getOldPassword()); newPasswordHash = StringUtil.md5(newPassword); } catch (NoSuchAlgorithmException e) { LOGGER.error(e.getMessage(), e); throw new HttpStatusException(Response.Status.INTERNAL_SERVER_ERROR, e); } if (user.getPassword().equals(oldPasswordHash)) { user.setPassword(newPasswordHash); user.setPasswordExpiration(this.getExpirationDate()); this.localUserDao.update(user); try { this.emailSender.sendPasswordChangeMessage(user); response = Response.status(Status.NO_CONTENT).build(); } catch (EmailException ee) { LOGGER.error(ee.getMessage(), ee); } } else { throw new HttpStatusException(Response.Status.BAD_REQUEST, "Error while changing password. Old password is incorrect."); } return response; }
From source file:org.apache.commons.httpclient.contrib.ssl.AuthSSLProtocolSocketFactory.java
private SSLContext createSSLContext() { try {//from www . ja va2 s . c om KeyManager[] keymanagers = null; TrustManager[] trustmanagers = null; if (this.keystoreUrl != null) { KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword); if (LOG.isDebugEnabled()) { Enumeration aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); Certificate[] certs = keystore.getCertificateChain(alias); if (certs != null) { LOG.debug("Certificate chain '" + alias + "':"); for (int c = 0; c < certs.length; c++) { if (certs[c] instanceof X509Certificate) { X509Certificate cert = (X509Certificate) certs[c]; LOG.debug(" Certificate " + (c + 1) + ":"); LOG.debug(" Subject DN: " + cert.getSubjectDN()); LOG.debug(" Signature Algorithm: " + cert.getSigAlgName()); LOG.debug(" Valid from: " + cert.getNotBefore()); LOG.debug(" Valid until: " + cert.getNotAfter()); LOG.debug(" Issuer: " + cert.getIssuerDN()); } } } } } keymanagers = createKeyManagers(keystore, this.keystorePassword); } if (this.truststoreUrl != null) { KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword); if (LOG.isDebugEnabled()) { Enumeration aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); LOG.debug("Trusted certificate '" + alias + "':"); Certificate trustedcert = keystore.getCertificate(alias); if (trustedcert != null && trustedcert instanceof X509Certificate) { X509Certificate cert = (X509Certificate) trustedcert; LOG.debug(" Subject DN: " + cert.getSubjectDN()); LOG.debug(" Signature Algorithm: " + cert.getSigAlgName()); LOG.debug(" Valid from: " + cert.getNotBefore()); LOG.debug(" Valid until: " + cert.getNotAfter()); LOG.debug(" Issuer: " + cert.getIssuerDN()); } } } trustmanagers = createTrustManagers(keystore); } SSLContext sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(keymanagers, trustmanagers, null); return sslcontext; } catch (NoSuchAlgorithmException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage()); } catch (KeyStoreException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage()); } catch (GeneralSecurityException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("Key management exception: " + e.getMessage()); } catch (IOException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage()); } }
From source file:se.inera.axel.shs.client.AuthSSLProtocolSocketFactory.java
private SSLContext createSSLContext() { try {//from w ww .j av a2 s.com KeyManager[] keymanagers = null; TrustManager[] trustmanagers = null; if (this.keystoreUrl != null) { KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword); if (LOG.isDebugEnabled()) { Enumeration aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); Certificate[] certs = keystore.getCertificateChain(alias); if (certs != null) { LOG.debug("Certificate chain '" + alias + "':"); for (int c = 0; c < certs.length; c++) { if (certs[c] instanceof X509Certificate) { X509Certificate cert = (X509Certificate) certs[c]; LOG.debug(" Certificate " + (c + 1) + ":"); LOG.debug(" Subject DN: " + cert.getSubjectDN()); LOG.debug(" Signature Algorithm: " + cert.getSigAlgName()); LOG.debug(" Valid from: " + cert.getNotBefore()); LOG.debug(" Valid until: " + cert.getNotAfter()); LOG.debug(" Issuer: " + cert.getIssuerDN()); } } } } } keymanagers = createKeyManagers(keystore, this.keystorePassword); } if (this.truststoreUrl != null) { KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword); if (LOG.isDebugEnabled()) { Enumeration aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); LOG.debug("Trusted certificate '" + alias + "':"); Certificate trustedcert = keystore.getCertificate(alias); if (trustedcert != null && trustedcert instanceof X509Certificate) { X509Certificate cert = (X509Certificate) trustedcert; LOG.debug(" Subject DN: " + cert.getSubjectDN()); LOG.debug(" Signature Algorithm: " + cert.getSigAlgName()); LOG.debug(" Valid from: " + cert.getNotBefore()); LOG.debug(" Valid until: " + cert.getNotAfter()); LOG.debug(" Issuer: " + cert.getIssuerDN()); } } } trustmanagers = createTrustManagers(keystore); } SSLContext sslcontext = SSLContext.getInstance("TLSv1"); sslcontext.init(keymanagers, trustmanagers, null); return sslcontext; } catch (NoSuchAlgorithmException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage()); } catch (KeyStoreException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage()); } catch (GeneralSecurityException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("Key management exception: " + e.getMessage()); } catch (IOException e) { LOG.error(e.getMessage(), e); throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage()); } }
From source file:org.atricore.idbus.capabilities.sso.support.core.encryption.XmlSecurityEncrypterImpl.java
private SecretKey generateDataEncryptionKey() { try {//w ww.j a va2 s. co m logger.debug("using uri algorithm [" + getSymmetricKeyAlgorithmURI() + "]"); String jceAlgorithmName = JCEMapper.getJCEKeyAlgorithmFromURI(getSymmetricKeyAlgorithmURI()); int keyLength = JCEMapper.getKeyLengthFromURI(getSymmetricKeyAlgorithmURI()); logger.debug("generating key with algorithm [" + jceAlgorithmName + ":" + keyLength + "]"); KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName); keyGenerator.init(keyLength); return keyGenerator.generateKey(); } catch (NoSuchAlgorithmException e) { logger.error(e.getMessage(), e); } return null; }
From source file:ch.bfh.evoting.alljoyn.MessageEncrypter.java
/** * /*ww w. ja v a 2s . c o m*/ * Method that decrypts data * @param ciphertext byte array to decrypt * @return the decrypted string if decryption was successful, * null otherwise * */ public String decrypt(byte[] ciphertext) { //Inspired from http://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption //iv is same as block size: for AES => 128 bits = 16 bytes byte[] iv = Arrays.copyOfRange(ciphertext, 0, 16); byte[] cipherText = Arrays.copyOfRange(ciphertext, 16, ciphertext.length); Cipher cipher; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); String s = new String(cipher.doFinal(cipherText)); //Since this decryption was successful, it means we have the correct key, //so we can disable the count of failed decryptions countDecryptionFailed = false; return s; } catch (NoSuchAlgorithmException e) { Log.d(TAG, e.getMessage() + " "); e.printStackTrace(); return null; } catch (NoSuchPaddingException e) { Log.d(TAG, e.getMessage() + " "); e.printStackTrace(); return null; } catch (InvalidKeyException e) { Log.d(TAG, e.getMessage() + " "); e.printStackTrace(); return null; } catch (InvalidAlgorithmParameterException e) { Log.d(TAG, e.getMessage() + " "); e.printStackTrace(); return null; } catch (IllegalBlockSizeException e) { Log.d(TAG, e.getMessage() + " "); countFailedDecryption(); e.printStackTrace(); return null; } catch (BadPaddingException e) { Log.d(TAG, e.getMessage() + " "); countFailedDecryption(); e.printStackTrace(); return null; } }
From source file:be.fedict.eid.applet.service.impl.handler.ContinueInsecureMessageHandler.java
public Object handleMessage(ContinueInsecureMessage message, Map<String, String> httpHeaders, HttpServletRequest request, HttpSession session) throws ServletException { if (this.changePin || this.unblockPin) { AdministrationMessage administrationMessage = new AdministrationMessage(this.changePin, this.unblockPin, this.logoff, this.removeCard, this.requireSecureReader); return administrationMessage; }/* www .j av a 2s . co m*/ SignatureService signatureService = this.signatureServiceLocator.locateService(); if (null != signatureService) { // TODO DRY refactor: is a copy-paste from HelloMessageHandler String filesDigestAlgo = signatureService.getFilesDigestAlgorithm(); if (null != filesDigestAlgo) { LOG.debug("files digest algo: " + filesDigestAlgo); FilesDigestRequestMessage filesDigestRequestMessage = new FilesDigestRequestMessage(); filesDigestRequestMessage.digestAlgo = filesDigestAlgo; return filesDigestRequestMessage; } DigestInfo digestInfo; try { digestInfo = signatureService.preSign(null, null); } catch (NoSuchAlgorithmException e) { throw new ServletException("no such algo: " + e.getMessage(), e); } // also save it in the session for later verification SignatureDataMessageHandler.setDigestValue(digestInfo.digestValue, digestInfo.digestAlgo, session); IdentityService identityService = this.identityServiceLocator.locateService(); boolean removeCard; if (null != identityService) { IdentityRequest identityRequest = identityService.getIdentityRequest(); removeCard = identityRequest.removeCard(); } else { removeCard = this.removeCard; } SignRequestMessage signRequestMessage = new SignRequestMessage(digestInfo.digestValue, digestInfo.digestAlgo, digestInfo.description, this.logoff, removeCard, this.requireSecureReader); return signRequestMessage; } AuthenticationService authenticationService = this.authenticationServiceLocator.locateService(); if (null != authenticationService) { byte[] challenge = AuthenticationChallenge.generateChallenge(session); IdentityIntegrityService identityIntegrityService = this.identityIntegrityServiceLocator .locateService(); boolean includeIntegrityData = null != identityIntegrityService; boolean includeIdentity; boolean includeAddress; boolean includePhoto; boolean includeCertificates; boolean removeCard; IdentityService identityService = this.identityServiceLocator.locateService(); if (null != identityService) { IdentityRequest identityRequest = identityService.getIdentityRequest(); includeIdentity = identityRequest.includeIdentity(); includeAddress = identityRequest.includeAddress(); includePhoto = identityRequest.includePhoto(); includeCertificates = identityRequest.includeCertificates(); removeCard = identityRequest.removeCard(); } else { includeIdentity = this.includeIdentity; includeAddress = this.includeAddress; includePhoto = this.includePhoto; includeCertificates = this.includeCertificates; removeCard = this.removeCard; } RequestContext requestContext = new RequestContext(session); requestContext.setIncludeIdentity(includeIdentity); requestContext.setIncludeAddress(includeAddress); requestContext.setIncludePhoto(includePhoto); requestContext.setIncludeCertificates(includeCertificates); String transactionMessage = null; SecureCardReaderService secureCardReaderService = this.secureCardReaderServiceLocator.locateService(); if (null != secureCardReaderService) { transactionMessage = secureCardReaderService.getTransactionMessage(); if (null != transactionMessage && transactionMessage.length() > SecureCardReaderService.TRANSACTION_MESSAGE_MAX_SIZE) { transactionMessage = transactionMessage.substring(0, SecureCardReaderService.TRANSACTION_MESSAGE_MAX_SIZE); } LOG.debug("transaction message: " + transactionMessage); } requestContext.setTransactionMessage(transactionMessage); AuthenticationRequestMessage authenticationRequestMessage = new AuthenticationRequestMessage(challenge, this.includeHostname, this.includeInetAddress, this.logoff, this.preLogoff, removeCard, this.sessionIdChannelBinding, this.serverCertificateChannelBinding, includeIdentity, includeCertificates, includeAddress, includePhoto, includeIntegrityData, this.requireSecureReader, transactionMessage); return authenticationRequestMessage; } else { IdentityIntegrityService identityIntegrityService = this.identityIntegrityServiceLocator .locateService(); boolean includeIntegrityData = null != identityIntegrityService; PrivacyService privacyService = this.privacyServiceLocator.locateService(); String identityDataUsage; if (null != privacyService) { String clientLanguage = HelloMessageHandler.getClientLanguage(session); identityDataUsage = privacyService.getIdentityDataUsage(clientLanguage); } else { identityDataUsage = null; } boolean includeAddress; boolean includePhoto; boolean includeCertificates; boolean removeCard; IdentityService identityService = this.identityServiceLocator.locateService(); if (null != identityService) { IdentityRequest identityRequest = identityService.getIdentityRequest(); includeAddress = identityRequest.includeAddress(); includePhoto = identityRequest.includePhoto(); includeCertificates = identityRequest.includeCertificates(); removeCard = identityRequest.removeCard(); } else { includeAddress = this.includeAddress; includePhoto = this.includePhoto; includeCertificates = this.includeCertificates; removeCard = this.removeCard; } RequestContext requestContext = new RequestContext(session); requestContext.setIncludeAddress(includeAddress); requestContext.setIncludePhoto(includePhoto); requestContext.setIncludeCertificates(includeCertificates); IdentificationRequestMessage responseMessage = new IdentificationRequestMessage(includeAddress, includePhoto, includeIntegrityData, includeCertificates, removeCard, identityDataUsage); return responseMessage; } }