List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:adminpassword.AESDemo.java
public String encrypt(String plainText) throws Exception { //get salt/* w w w .j a va 2 s . c o m*/ salt = generateSalt(); byte[] saltBytes = salt.getBytes("UTF-8"); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); //encrypt the message Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = cipher.getParameters(); ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8")); return new Base64().encodeAsString(encryptedTextBytes); }
From source file:Main.java
/** * This method should be used to decrypt a block of data. * /* w w w.ja v a2 s. c o m*/ * @param fileData the data to decrypt. * @param decryptionKey the key to initialize the cipher-algorithm. * @param decryptCompleted a flag, that indicates, that a multi-part decryption is to be * completed. e.g. false, if the fileData consist of multiple * parts. true, if the fileData consists only of one single part * and so they could be decrypted in one operation. * * @return the decrypted data. */ public static byte[] decryptData(byte[] fileData, byte[] decryptionKey, boolean decryptCompleted) { // Initializing may only be done at the start of a multi-part // crypto-operation. // if it's a single part crypto-operation initialization must always be // done. if (!decryptInitialized) { // Initializing the cipher try { decryptCipher = Cipher.getInstance("AES"); } catch (Exception e) { e.printStackTrace(); return null; } try { SecretKeySpec keySpec = new SecretKeySpec(decryptionKey, "AES"); decryptCipher.init(Cipher.DECRYPT_MODE, keySpec); } catch (Exception e) { Log.e(TAG, "Error while initializing decryption."); return null; } decryptInitialized = true; } // Decrypting try { if (!decryptCompleted) // done in case of multi-part operation fileData = decryptCipher.update(fileData); else // done in case of single part operation or to finish a // multi-part operation fileData = decryptCipher.doFinal(fileData); } catch (Exception e) { Log.e(TAG, "Error during decryption."); return null; } // at the and of an multi-part operation flags must be reset if (decryptCompleted) decryptInitialized = false; return fileData; }
From source file:com.sshutils.utils.CryptHelper.java
public static String decrypt(String strToDecrypt) { try {//from w ww.j a v a 2s . c o m // for (Provider provider: Security.getProviders()) { // log.info(provider.getName()); // for (String key: provider.stringPropertyNames()) // log.info("\t" + key + "\t" + provider.getProperty(key)); //} Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))); return decryptedString; } catch (Exception e) { log.error("Error while decrypting", e); } return null; }
From source file:com.paypal.utilities.AESDecrypt.java
public AESDecrypt(String encryptedString) throws Exception { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH); SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec); SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES"); encrypt = Base64.decodeBase64(encryptedString.getBytes()); eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); eCipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] iv = extractIV(); dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); }
From source file:com.diversityarrays.dal.db.DalDatabaseUtil.java
/** * Calculate an RFC 2104 compliant HMAC signature. * @param key is the signing key/* w w w. ja v a 2 s . com*/ * @param data is the data to be signed * @return the base64-encoded signature as a String */ public static String computeHmacSHA1(String key, String data) { try { byte[] keyBytes = key.getBytes("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8")); // TODO Consider replacing with a simple hex encoder so we don't need commons-codec byte[] hexBytes = new Hex().encode(rawHmac); return new String(hexBytes, "UTF-8"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
From source file:com.sv.udb.controlador.CtrlContras.java
public String decrypt(String encrypted) throws Exception { Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); byte[] enc = decodeBase64(encrypted); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec); byte[] decrypted = cipher.doFinal(enc); return new String(decrypted); }
From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java
public static String decrypt(final String encrypted, final String key) throws PunchOutCipherException { String decrypted = null;/*from w w w . j a v a 2s .c om*/ try { final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM); final Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, skeySpec); final byte[] decodedValue = new Base64().decode(encrypted.getBytes()); final byte[] decryptedValue = cipher.doFinal(decodedValue); decrypted = new String(decryptedValue); } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) { // should never happen LOG.error("System was unable instantiate Cipher.", e); } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { final String msg = "Error occured during decryption." + e.getMessage(); LOG.info(msg); throw new PunchOutCipherException(msg, e); } return decrypted; }
From source file:com.fortmoon.utils.SymmetricCypher.java
public SymmetricCypher() { cipherTransformation = "AES"; keyAlgorithm = "AES"; secretKeySpec = new SecretKeySpec(Base64.decodeBase64(salt.getBytes()), keyAlgorithm); }
From source file:Main.java
/** * This method should be used to encrypt a block of data. * //w ww.jav a 2 s .co m * @param fileData * the data to encrypt. * @param encryptionKey * the key to initialize the cipher-algorithm. * @param encryptCompleted * a flag, that indicates, that a multi-part encryption is to be * completed. e.g. false, if the fileData consist of multiple * parts. true, if the fileData consists only of one single part * and so they could be encrypted in one operation. * * @return the encrypted data. */ public static byte[] encryptData(byte[] fileData, byte[] encryptionKey, boolean encryptCompleted) { // Initializing may only be done at the start of a multi-part crypto-operation. // if it's a single part crypto-operation initialization must always be done. if (!encryptInitialized) { // Initializing the cipher try { encryptCipher = Cipher.getInstance("AES"); } catch (Exception e) { Log.e(TAG, "Error while initializing encryption."); return null; } try { SecretKeySpec keySpec = new SecretKeySpec(encryptionKey, "AES"); encryptCipher.init(Cipher.ENCRYPT_MODE, keySpec); } catch (Exception e) { Log.e(TAG, "Error while initializing encryption."); return null; } encryptInitialized = true; } // encrypting try { if (!encryptCompleted) // done in case of multi-part operation fileData = encryptCipher.update(fileData); else // done in case of single part operation or to finish a multi-part operation fileData = encryptCipher.doFinal(fileData); } catch (Exception e) { Log.e(TAG, "Error during encryption."); return null; } // at the and of an multi-part operation flags must be reset if (encryptCompleted) encryptInitialized = false; return fileData; }
From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java
@Override public String encrypt(String secretkey, String iv, String toEncrypt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, DecoderException { Cipher cipher = Cipher.getInstance(AESMODE); SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray()))); return Base64.encodeBase64String(cipher.doFinal(toEncrypt.getBytes())); }