List of usage examples for javax.crypto SecretKey getAlgorithm
public String getAlgorithm();
From source file:net.mobid.codetraq.utils.PasswordProcessor.java
/** * Encrypts a text using the <code>passPhrase</code> above and an algorithm supported * by your virtual machine implementation. You can change the default algorithm with * another algorithm, but please make sure your virtual machine supports it. * @param valueToEncrypt - text to encrypt * @return an encrypted, Base64 encoded text *///from w w w . j ava 2s. co m public static String encryptString(String valueToEncrypt) { String output = null; try { KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations); SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations); cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); // begin encrypting... byte[] byteToEncrypt = valueToEncrypt.getBytes("UTF8"); byte[] encrypted = cipher.doFinal(byteToEncrypt); output = new Base64().encodeToString(encrypted); } catch (Exception ex) { Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:net.mobid.codetraq.utils.PasswordProcessor.java
/** * Decrypts a text using the <code>passPhrase</code> above and an algorithm supported * by your virtual machine implementation. You can change the default algorithm with * another algorithm, but please make sure your virtual machine supports it. * @param valueToDecrypt - text to decrypt * @return a plain text//from w w w . j av a 2 s . c o m */ public static String decryptString(String valueToDecrypt) { String output = null; try { KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations); SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations); cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); // begin decrypting... byte[] encrypted = new Base64().decode(valueToDecrypt); byte[] utf8 = cipher.doFinal(encrypted); output = new String(utf8, "UTF8"); } catch (Exception ex) { Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:be.fedict.eid.idp.model.CryptoUtil.java
public static Mac getMac(byte[] hmacSecret) throws InvalidKeyException { SecretKey macKey = new SecretKeySpec(hmacSecret, "HmacSHA1"); Mac mac;/*w ww. j a va2 s . c o m*/ try { mac = Mac.getInstance(macKey.getAlgorithm()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("HMAC algo not available: " + e.getMessage()); } mac.init(macKey); return mac; }
From source file:be.fedict.eid.applet.service.impl.UserIdentifierUtil.java
/** * Gives back a non-reversible citizen identifier (NRCID). * //from w ww. ja va2s . com * @param userId * the primary user identifier, i.e. the national registry * number. * @param orgId * the optional organization identifier. * @param appId * the optional application identifier. * @param secret * the application specific secret. Should be at least 128 bit * long. Encoded in hexadecimal format. * @return */ public static String getNonReversibleCitizenIdentifier(String userId, String orgId, String appId, String secret) { if (null == secret) { throw new IllegalArgumentException("secret key is null"); } /* * Avoid XML formatting issues introduced by some web.xml XML editors. */ secret = secret.trim(); if (null != orgId) { orgId = orgId.trim(); } else { LOG.warn("it is advised to use an orgId"); } if (null != appId) { appId = appId.trim(); } else { LOG.warn("it is advised to use an appId"); } /* * Decode the secret key. */ byte[] secretKey; try { secretKey = Hex.decodeHex(secret.toCharArray()); } catch (DecoderException e) { LOG.error("secret is not hexadecimal encoded: " + e.getMessage()); throw new IllegalArgumentException("secret is not hexadecimal encoded"); } if ((128 / 8) > secretKey.length) { /* * 128 bit is seen as secure these days. */ LOG.warn("secret key is too short"); throw new IllegalArgumentException("secret key is too short"); } /* * Construct the HMAC input sequence. */ String input = userId; if (null != appId) { input += appId; } if (null != orgId) { input += orgId; } byte[] inputData = input.getBytes(); SecretKey macKey = new SecretKeySpec(secretKey, HMAC_ALGO); Mac mac; try { mac = Mac.getInstance(macKey.getAlgorithm()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("HMAC algo not available: " + e.getMessage()); } try { mac.init(macKey); } catch (InvalidKeyException e) { LOG.error("invalid secret key: " + e.getMessage(), e); throw new RuntimeException("invalid secret"); } mac.update(inputData); byte[] resultHMac = mac.doFinal(); String resultHex = new String(Hex.encodeHex(resultHMac)).toUpperCase(); return resultHex; }
From source file:com.swdouglass.joid.Crypto.java
private static byte[] hmacShaX(String keySpec, byte[] key, byte[] text) throws InvalidKeyException, NoSuchAlgorithmException { SecretKey sk = new SecretKeySpec(key, keySpec); Mac m = Mac.getInstance(sk.getAlgorithm()); m.init(sk);//w ww .j ava 2 s .co m return m.doFinal(text); }
From source file:net.nicholaswilliams.java.licensing.encryption.Encryptor.java
private static Cipher getEncryptionCipher(SecretKey secretKey) { try {/*from w ww . ja v a 2 s.c o m*/ Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, secretKey, Encryptor.random); return cipher; } catch (NoSuchAlgorithmException e) { throw new AlgorithmNotSupportedException("AES With SHA-1 digest", e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new InappropriateKeyException(e.getMessage(), e); } }
From source file:net.nicholaswilliams.java.licensing.encryption.Encryptor.java
private static Cipher getDecryptionCipher(SecretKey secretKey) { try {/*from www .ja va 2s .c o m*/ Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, secretKey, Encryptor.random); return cipher; } catch (NoSuchAlgorithmException e) { throw new AlgorithmNotSupportedException("AES With SHA-1 digest", e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new InappropriateKeyException(e.getMessage(), e); } }
From source file:eap.util.EDcodeUtil.java
private static byte[] hmac(byte[] data, byte[] key, String algorithm) { try {//from w w w.ja va2s. c om SecretKey secretKey = new SecretKeySpec(key, algorithm); Mac mac = Mac.getInstance(secretKey.getAlgorithm(), provider); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("No such algorithm [" + algorithm + "]"); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
From source file:DesEncrypter.java
DesEncrypter(String passPhrase) throws Exception { int iterationCount = 2; KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:DesEncrypter.java
private DesEncrypter(String passPhrase) { try {/*from ww w .j av a 2 s . co m*/ // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (java.security.InvalidAlgorithmParameterException e) { } catch (java.security.spec.InvalidKeySpecException e) { } catch (javax.crypto.NoSuchPaddingException e) { } catch (java.security.NoSuchAlgorithmException e) { } catch (java.security.InvalidKeyException e) { } }