List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:Authentication.HashPassword.java
public HashPassword() throws Exception { myEncryptionKey = "ThisIsSpartaThisIsSparta"; myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME; arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); ks = new DESedeKeySpec(arrayBytes); skf = SecretKeyFactory.getInstance(myEncryptionScheme); cipher = Cipher.getInstance(myEncryptionScheme); key = skf.generateSecret(ks);/* ww w . j a v a 2 s .com*/ }
From source file:Main.java
/** * This method should be used to encrypt a block of data. * // w w w. j a va 2 s . c om * @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.feedzai.commons.sql.abstraction.util.AESHelper.java
/** * Encrypts a byte[]./*from ww w . j av a 2s .c o m*/ * * @param c The byte[] to encrypt. * @param key The key. * @return The encrypted array as a HEX string. */ public static String encrypt(byte[] c, String key) { try { SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encoded = cipher.doFinal(c); return new String(Hex.encodeHex(encoded)); } catch (Exception e) { logger.warn("Could not encrypt byte[]", e); return null; } }
From source file:com.amazonaws.cognito.sync.devauth.client.AESEncryption.java
/** * Decrypt a cipher in bytes using the specified key * /*w ww . j av a 2 s .c o m*/ * @param cipherBytes encrypted bytes * @param key the key used in decryption * @param iv * @return * @throws Exception */ public static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception { Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); params.init(new IvParameterSpec(iv)); cipher.init(Cipher.DECRYPT_MODE, getKey(key), params); return cipher.doFinal(cipherBytes); }
From source file:com.intera.roostrap.util.EncryptionUtil.java
private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os) throws InvalidKeyException, IOException { DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey)); SecretKey key = null;/*from w ww.j av a 2 s. c om*/ Cipher cipher = null; try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); key = secretKeyFactory.generateSecret(keySpec); cipher = Cipher.getInstance("DES"); } catch (Exception e) { throw new RuntimeException(e); } if (mode == Cipher.ENCRYPT_MODE) { cipher.init(Cipher.ENCRYPT_MODE, key); CipherInputStream cis = new CipherInputStream(is, cipher); doCopy(cis, os); } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
From source file:com.ikon.util.SecureStore.java
/** * DES encoder/*from w ww. ja va 2s . c om*/ */ public static byte[] desEncode(String key, byte[] src) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey sKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, sKey); byte[] dst = cipher.doFinal(src); return dst; }
From source file:algorithm.AesEncryption.java
public AesEncryption(String function) { setKey(function);//from w w w.j a va 2s . com try { encrypt = Cipher.getInstance("AES/ECB/PKCS5Padding"); decrypt = Cipher.getInstance("AES/ECB/PKCS5Padding"); } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) { Logger.getLogger(AesEncryption.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.jsmartframework.web.manager.CsrfEncrypter.java
private static Cipher getDecryptCipher(HttpServletRequest request, String key) throws Exception { Cipher decryptCipher = (Cipher) request.getAttribute(REQUEST_CSRF_DECRYPT_CIPHER); if (decryptCipher == null) { decryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); decryptCipher.init(Cipher.DECRYPT_MODE, secretKey); request.setAttribute(REQUEST_CSRF_DECRYPT_CIPHER, decryptCipher); }/*from w ww . j a v a 2 s . c o m*/ return decryptCipher; }
From source file:gov.nih.nci.cadsr.cadsrpasswordchange.core.OracleObfuscation.java
public OracleObfuscation(String secretString) throws GeneralSecurityException { key = new SecretKeySpec(secretString.getBytes(), algorithm1); cipher = Cipher.getInstance(algorithm2); }
From source file:com.jsmartframework.web.manager.AuthEncrypter.java
private static Cipher getDecryptCipher(HttpServletRequest request, String key) throws Exception { Cipher decryptCipher = (Cipher) request.getAttribute(REQUEST_AUTH_DECRYPT_CIPHER); if (decryptCipher == null) { decryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); decryptCipher.init(Cipher.DECRYPT_MODE, secretKey); request.setAttribute(REQUEST_AUTH_DECRYPT_CIPHER, decryptCipher); }// www .j a v a 2s . c om return decryptCipher; }