List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:CipherProvider.java
/** * Create cipher for encrypt or decrypt backup content * * @param passwd passwd for encryption/*w ww .ja va2s. com*/ * @param mode encrypt/decrypt mode * * @return instance of cipher */ private static Cipher getCipher(final String passwd, final int mode) { /* Derive the key, given password and salt. */ Cipher cipher = null; try { SecretKeyFactory factory = null; factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); String salt = "slNadZlato#%^^&(&(5?@#5166?1561?#%^^*^&54431"; // only pseudorandom salt KeySpec spec = new PBEKeySpec(passwd.toCharArray(), salt.getBytes(), 65536, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), CIPHER_TYPE); // initialization vector byte[] iv = Arrays.copyOfRange(DigestUtils.md5(passwd), 0, 16); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); // Cipher for encryption cipher = Cipher.getInstance(CIPHER_TYPE + "/CBC/PKCS5Padding"); cipher.init(mode, secret, paramSpec); } catch (Exception e) { e.printStackTrace(); //Todo implementovat } return cipher; }
From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java
@Override public String decrypt(String secretkey, String iv, String toDecrypt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, DecoderException { Cipher cipher = Cipher.getInstance(AESMODE); SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray()))); return new String(cipher.doFinal(Base64.decodeBase64(toDecrypt))); }
From source file:com.kolich.havalo.client.signing.algorithms.HMACSHA256Signer.java
/** * Returns a Base-64 encoded HMAC-SHA256 signature. *///from w ww. j a v a 2s. co m @Override public String sign(final HavaloCredentials credentials, final String input) { String result = null; try { // Get a new instance of the HMAC-SHA256 algorithm. final Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM_NAME); // Init it with our secret and the secret-key algorithm. mac.init(new SecretKeySpec(getBytesUtf8(credentials.getSecret()), HMAC_SHA256_ALGORITHM_NAME)); // Sign the input. result = newStringUtf8(encodeBase64(mac.doFinal(getBytesUtf8(input)))); } catch (Exception e) { throw new HavaloClientException("Failed to SHA-256 sign input " + "string: " + input, e); } return result; }
From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java
/** * Create a new token generator/parser using an encryption key. This attempts to fail early by creating a cipher in the constructor. * @param hexKey The encryption key, hex encoded. ATM, this uses AES, so 128, 194, or 256 bit * @throws TokenException Thrown if the key or IV are not properly base64 encoded or the cipher cannot otherwise be created. *///w w w.j a v a2s. com public SecureTokenImpl(String hexKey) throws TokenException { try { byte[] keyBytes = Hex.decodeHex(hexKey.toCharArray()); key = new SecretKeySpec(keyBytes, "AES"); createCipher(Cipher.ENCRYPT_MODE, createIV()); } catch (DecoderException e) { throw new TokenException("Invalid Key or IV", e); } }
From source file:edu.harvard.i2b2.analysis.security.RijndaelAlgorithm.java
public RijndaelAlgorithm(String password, int ksize, String encryptionType, String emethod) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), encryptionType); cipherEnc = Cipher.getInstance(emethod); cipherDec = Cipher.getInstance(emethod); keySize = ksize;//from w w w .jav a 2s. c o m //setKey( pword ); cipherEnc.init(Cipher.ENCRYPT_MODE, skeySpec); cipherDec.init(Cipher.DECRYPT_MODE, skeySpec); }
From source file:com.gvmax.common.util.Enc.java
public Enc(String password, int keyLength) { if (password == null || password.trim().equals("")) { enabled = false;/*ww w . j ava 2s .c o m*/ } else { enabled = true; try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLength); key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); c = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (Exception e) { logger.error(e); } } }
From source file:com.doplgangr.secrecy.filesystem.encryption.AES_ECB_Crypter.java
public AES_ECB_Crypter(String vaultPath, String password) throws InvalidKeyException { this.vaultPath = vaultPath; try {/*from ww w . j ava2 s . c o m*/ byte[] bytes = password.getBytes("UTF-8"); byte[] legitkey = new byte[32]; int i = 0; while (i < 32 && i < bytes.length) { legitkey[i] = bytes[i]; i++; } String KEY_ALGORITHM = "AES"; aesKey = new SecretKeySpec(legitkey, KEY_ALGORITHM); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:com.charandeepmatta.oracle.sqldeveloper.DecodePasswordHash.java
private byte[] decryptPassword(final byte[] result) throws GeneralSecurityException { byte constant = result[0]; if (constant != (byte) 5) { throw new IllegalArgumentException(); }/*from w w w . ja v a 2 s.co m*/ byte[] secretKey = new byte[8]; System.arraycopy(result, 1, secretKey, 0, 8); byte[] encryptedPassword = new byte[result.length - 9]; System.arraycopy(result, 9, encryptedPassword, 0, encryptedPassword.length); byte[] iv = new byte[8]; for (int i = 0; i < iv.length; i++) { iv[i] = 0; } Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "DES"), new IvParameterSpec(iv)); return cipher.doFinal(encryptedPassword); }
From source file:Main.java
public static byte[] aesIGEencrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) { try {/*w ww . j a v a2 s . c o m*/ ByteBuffer out = ByteBuffer.allocate(data.length); byte[] ivp = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2); byte[] iv2p = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length); int len = data.length / AES_BLOCK_SIZE; byte[] xorInput = null; byte[] xorOutput = null; SecretKeySpec keySpec = null; keySpec = new SecretKeySpec(tmpAesKey, "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] input = null; byte[] output = null; for (int i = 0; i < len; i++) { input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE); xorInput = xor(input, ivp); output = cipher.doFinal(xorInput); xorOutput = xor(output, iv2p); out.put(xorOutput); ivp = xorOutput; iv2p = input; } return out.array(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:com.spartan.springmvc.bean.FacebookSignatureBean.java
/** * Parses and verifies a Facebook signed_request parameter. The data of the signed request is returned on successful verification. * * @param signedRequest//from ww w . j a va 2 s. c o m * @param appSecret * @return * @return * @throws FacebookSignatureVerificationFailedException */ @SuppressWarnings("unchecked") public <T> T parseSignature(String signedRequest, String appSecret, Class<T> clazz) throws FacebookSignatureVerificationFailedException { String[] parts = signedRequest.split("\\."); if (parts.length != 2) { throw new FacebookSignatureVerificationFailedException("Invalid signature format."); } String encSig = parts[0]; String encPayload = parts[1]; Base64 decoder = new Base64(true); try { Mac mac = Mac.getInstance("HMACSHA256"); mac.init(new SecretKeySpec(appSecret.getBytes(), mac.getAlgorithm())); byte[] calcSig = mac.doFinal(encPayload.getBytes()); byte[] decodedSig = decoder.decode(encSig); boolean isVerified = Arrays.equals(decodedSig, calcSig); if (isVerified) { try { String unsignedData = new String(decoder.decode(encPayload)); logger.debug("unsignedData: " + unsignedData); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); T data = mapper.readValue(unsignedData, (Class<T>) clazz); return data; } catch (IOException e) { throw new FacebookSignatureVerificationFailedException( "Failed to parse JSON data: " + e.getMessage(), e); } } else { throw new FacebookSignatureVerificationFailedException("Signatures do not match."); } } catch (NoSuchAlgorithmException e) { throw new FacebookSignatureVerificationFailedException(e); } catch (InvalidKeyException e) { throw new FacebookSignatureVerificationFailedException(e); } }