List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
From source file:com.jsmartframework.web.manager.CsrfEncrypter.java
private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception { Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_CSRF_ENCRYPT_CIPHER); if (encryptCipher == null) { encryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); request.setAttribute(REQUEST_CSRF_ENCRYPT_CIPHER, encryptCipher); }//w w w .j a v a 2s . c o m return encryptCipher; }
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 w w .jav a2 s. c o m*/ return decryptCipher; }
From source file:com.jsmartframework.web.manager.AuthEncrypter.java
private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception { Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_AUTH_ENCRYPT_CIPHER); if (encryptCipher == null) { encryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); request.setAttribute(REQUEST_AUTH_ENCRYPT_CIPHER, encryptCipher); }//from w ww .jav a 2s . c om return encryptCipher; }
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); }// w ww .j a va 2s . c o m return decryptCipher; }
From source file:com.lling.qiqu.utils.AesUtils.java
/** * Encrypts a message using a 128bit Bse64 key using AES. * * @param key 128bit Base64 AES key/*from w ww.j a va 2 s . co m*/ * @param message Message to encrypt * @return hex encoded encrypted message */ public static String encrypt(String key, String message) { String encrypted = ""; try { // Generate the secret key specs. byte[] keyArray = Base64.decodeBase64(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypt = cipher.doFinal(message.getBytes()); encrypted = new String(hexCodec.encode(encrypt)); } catch (Exception e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } return encrypted; }
From source file:com.lling.qiqu.utils.AesUtils.java
/** * Encrypts a message using a 128bit Bse64 key using AES. * * @param key 128bit Base64 AES key// w w w .j ava 2 s .c o m * @param message Message to encrypt * @return bse64 encoded encrypted message */ public static String encryptBase64(String key, String message) { String encrypted = ""; try { // Generate the secret key specs. byte[] keyArray = Base64.decodeBase64(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypt = cipher.doFinal(message.getBytes()); encrypted = new String(Base64.encodeBase64(encrypt)); } catch (Exception e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } return encrypted; }
From source file:com.lling.qiqu.utils.AesUtils.java
/** * Decrypts a AES ecrypted message with a Base64 128bit key * * @param key AES Base64 128bit key// ww w . ja v a 2 s . c o m * @param value hex encoded encrypted message to decrypt * @return the original message */ public static String decrypt(String key, String value) { String decrypted = ""; try { // Get the KeyGenerator byte[] keyArray = Base64.decodeBase64(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES"); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); // byte[] encrypted = // urlCodec.decode(value.getBytes(ICoreFrameworkConstants.DEFAULT_STRING_ENCODING)); byte[] encrypted = hexCodec.decode(value.getBytes()); byte[] original = cipher.doFinal(encrypted); decrypted = new String(original); } catch (Exception e) { logger.error(e.getMessage(), e); decrypted = ""; } return decrypted; }
From source file:com.lling.qiqu.utils.AesUtils.java
/** * Decrypts a AES ecrypted message with a Base64 128bit key * * @param key AES Base64 128bit key/*from www .ja v a2 s.c o m*/ * @param value Base64 encoded encrypted message to decrypt * @return the original message */ public static String decryptBase64(String key, String value) { String decrypted = ""; try { // Get the KeyGenerator byte[] keyArray = Base64.decodeBase64(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES"); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); // byte[] encrypted = // urlCodec.decode(value.getBytes(ICoreFrameworkConstants.DEFAULT_STRING_ENCODING)); byte[] encrypted = Base64.decodeBase64(value.getBytes()); byte[] original = cipher.doFinal(encrypted); decrypted = new String(original); } catch (Exception e) { logger.error(e.getMessage(), e); decrypted = ""; } return decrypted; }
From source file:com.dc.util.file.FileSupport.java
private static void copyEncryptedStream(InputStream input, OutputStream output, String key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { Cipher dcipher = Cipher.getInstance("AES"); CipherInputStream cis = null; try {//ww w .ja v a2s . c o m dcipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES")); cis = new CipherInputStream(input, dcipher); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = cis.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } output.flush(); } finally { if (cis != null) { cis.close(); } } }