List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:dz.alkhwarizmix.framework.java.utils.CryptoUtil.java
/** * Constructor./*from w w w. j a va 2 s .co m*/ */ public CryptoUtil(String pKey, String pEncType, String pModeType, boolean pSimple, String pPaddingType) { if (pEncType == null) pEncType = "aes"; pEncType = pEncType.toUpperCase(); if (pModeType == null) pModeType = "cbc"; // ecb, cbc, ofb pModeType = pModeType.toUpperCase(); if (pPaddingType == null) pPaddingType = "pkcs5"; String pHexKey = stringToHex(pKey); pHexKey = pHexKey.substring(0, 64); String pHexIV = pHexKey.substring(0, 32); byte[] kdata = hex2Byte(pHexKey); String pad = ((pPaddingType == "pkcs5") ? "PKCS5" : "No") + "Padding"; String algo = pEncType + "/" + pModeType + "/" + pad; secretKeySpec = new SecretKeySpec(kdata, pEncType); ivParameterSpec = new IvParameterSpec(hex2Byte(pHexIV)); try { cipher = Cipher.getInstance(algo); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } }
From source file:com.rogoman.easyauth.HMAC.java
/** * Calculates the HMAC digest value based on the provided parameters. * * @param msg Message/*ww w .ja va2 s .c o m*/ * @param secretKey Key to be used in the hashing process * @param algorithm HMAC algorithm to be used * @return HMAC digest * @throws java.security.NoSuchAlgorithmException thrown when the passed digest algorithm name cannot be recognized * @throws java.security.InvalidKeyException thrown when the passed secret key value is invalid according to the digest algorithm */ static byte[] hmacDigest(final byte[] msg, final byte[] secretKey, final String algorithm) throws NoSuchAlgorithmException, InvalidKeyException { if (msg == null) { throw new IllegalArgumentException("msg is empty"); } if (secretKey == null) { throw new IllegalArgumentException("secretKey is empty"); } if (StringUtils.isEmpty(algorithm)) { throw new IllegalArgumentException("algo is empty"); } SecretKeySpec key = new SecretKeySpec(secretKey, algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(key); return mac.doFinal(msg); }
From source file:com.github.shredder121.gh_event_api.filter.GithubMACChecker.java
@Autowired public GithubMACChecker(Environment env) { String secret = env.getProperty("secret"); if (secret != null) { Key key = new SecretKeySpec(secret.getBytes(UTF_8), HMAC_SHA1); this.macProvider = new MacProvider(key); } else {/*from w ww . j a v a2s .com*/ this.macProvider = null; } }
From source file:com.aqnote.shared.encrypt.symmetric.AES.java
private static void generateCipher(String rawKey) { try {/*from w ww . j a v a 2 s . com*/ SecretKeySpec keySpec = new SecretKeySpec(rawKey.getBytes(ENCODE_UTF_8), ALGORITHM); encodeCipher = Cipher.getInstance(ALGORITHM); encodeCipher.init(Cipher.ENCRYPT_MODE, keySpec); decodeCipher = Cipher.getInstance(ALGORITHM); decodeCipher.init(Cipher.DECRYPT_MODE, keySpec); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
From source file:com.lecaddyfute.utils.security.AESCrypto.java
public static Key generateKey() throws Exception { String cle = CLE;//from ww w . j a va2 s . c o m try { if (cle.isEmpty()) { InetAddress adrLocale; adrLocale = InetAddress.getLocalHost(); cle = adrLocale.getHostName(); } if (cle.length() > 16) { cle = cle.substring(0, 16); } if (cle.length() < 16) { int chartocomplete = 16 - cle.length(); for (int i = 0; i < chartocomplete; i++) { cle = cle + i; } } logger.log(Level.INFO, "cle par defaut {0}", cle); keyValue = cle.getBytes(); } catch (Exception e) { e.printStackTrace(); } Key key = new SecretKeySpec(keyValue, ALGO); return key; }
From source file:de.adorsys.morphiaencryption.AES256CryptoProvider.java
private SecretKey createKey(String key) { byte[] inputKey = Base64.decodeBase64(key); return new SecretKeySpec(inputKey, "AES"); }
From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java
private static SecretKeySpec getKey(String key) { try {/*from w w w . j a v a 2 s . c om*/ return new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES"); } catch (DecoderException e) { throw new RuntimeException("Failed to generate a secret key spec", e); } }
From source file:jp.co.golorp.emarf.util.CryptUtil.java
/** * ???//from ww w .j a v a2 s . com * * @param opmode * opmode * @param input * input * @return byte[] */ private static byte[] cipher(final int opmode, final byte[] input) { Cipher cipher = null; try { cipher = Cipher.getInstance(ALGORITHM); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new SystemError(e); } try { cipher.init(opmode, new SecretKeySpec(SECRET_KEY, ALGORITHM)); } catch (InvalidKeyException e) { throw new SystemError(e); } byte[] bytes = null; try { LOG.trace(String.valueOf(input)); bytes = cipher.doFinal(input); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new SystemError(e); } return bytes; }
From source file:com.projectsontracks.model.CaribooKey.java
/** * Creates a new AES key/*from w w w .j av a 2 s . co m*/ * * @throws java.security.NoSuchAlgorithmException */ public void createKey() throws NoSuchAlgorithmException { // Initialize the key generator KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(this.size); // Generates the key SecretKey skey = kgen.generateKey(); // Returns the key in its primary encoding format, or null if this key does not support encoding. key = skey.getEncoded(); //used to construct a SecretKey from a byte array, without having to go through a (provider-based) SecretKeyFactory. keySpec = new SecretKeySpec(key, "AES"); }
From source file:com.parworks.androidlibrary.utils.HMacShaPasswordEncoder.java
public String encodePassword(String rawDataToBeEncrypted, Object salt) { byte[] hmacData = null; if (rawDataToBeEncrypted != null) { try {//from w ww.j av a2s . c om SecretKeySpec secretKey = new SecretKeySpec(rawDataToBeEncrypted.getBytes(ENCODING_FOR_ENCRYPTION), this.algorithm); Mac mac = getMac(); mac.init(secretKey); hmacData = mac.doFinal(salt.toString().getBytes(ENCODING_FOR_ENCRYPTION)); if (isEncodeHashAsBas64()) { return new String(Base64.encode(hmacData), ENCODING_FOR_ENCRYPTION); } else { return new String(hmacData, ENCODING_FOR_ENCRYPTION); } } catch (InvalidKeyException ike) { throw new RuntimeException("Invalid Key while encrypting.", ike); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unsupported Encoding while encrypting.", e); } } return ""; }