List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:com.rr.familyPlanning.ui.security.decryptObject.java
/** * Decrypts the String and serializes the object * * @param base64Data//from www . ja v a 2 s . co m * @param base64IV * @return * @throws Exception */ public Object decryptObject(String base64Data, String base64IV) throws Exception { // Decode the data byte[] encryptedData = Base64.decodeBase64(base64Data.getBytes()); // Decode the Init Vector byte[] rawIV = Base64.decodeBase64(base64IV.getBytes()); // Configure the Cipher Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(rawIV); MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(keyString.getBytes()); byte[] key = new byte[16]; System.arraycopy(digest.digest(), 0, key, 0, key.length); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); // Decrypt the data.. byte[] decrypted = cipher.doFinal(encryptedData); // Deserialize the object ByteArrayInputStream stream = new ByteArrayInputStream(decrypted); ObjectInput in = new ObjectInputStream(stream); Object obj = null; try { obj = in.readObject(); } finally { stream.close(); in.close(); } return obj; }
From source file:com.wso2telco.gsma.authenticators.cryptosystem.AESencrp.java
/** * Encrypt./*www. j a v a2s . co m*/ * * @param Data the data * @return the string * @throws Exception the exception */ public static String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); //String encryptedValue = new BASE64Encoder().encode(encVal); byte[] encryptedValue = Base64.encodeBase64(encVal); return new String(encryptedValue); }
From source file:com.thoughtworks.go.server.util.EncryptionHelper.java
public static String encryptUsingRSA(String plainText, String publicKeyContent) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException { Cipher encryptCipher = Cipher.getInstance("RSA"); encryptCipher.init(Cipher.ENCRYPT_MODE, getRSAPublicKeyFrom(publicKeyContent)); return Base64.getEncoder().encodeToString(encryptCipher.doFinal(plainText.getBytes(UTF_8))); }
From source file:Main.java
/** * This method should be used to decrypt a block of data. * /*ww w. j av a 2 s.co 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.lwr.software.reporter.utils.EncryptionUtil.java
public static String decrypt(String encrypted) { try {/* w ww . j a v a 2 s.co m*/ IvParameterSpec iv = new IvParameterSpec( DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec( DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING), DashboardConstants.ALGORITHM); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.wabacus.util.DesEncryptTools.java
public static String encrypt(String originalString) { if (originalString == null || originalString.trim().equals("")) return ""; if (KEY_OBJ == null) { log.warn(""); return originalString; }/*from w ww . j a va 2 s .c o m*/ try { Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, KEY_OBJ); return base64Encode(c1.doFinal(originalString.getBytes())); } catch (Exception e) { log.error("" + originalString + "", e); return null; } }
From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java
public static String decrypt(String key1, String iv1, String encrypted) { try {//w w w . java2 s . c o m IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.elle.analyster.admissions.AESCrypt.java
public static String encrypt(String key, String initVector, String value) { try {//from w ww .j a va 2s. com IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.lecaddyfute.utils.security.AESCrypto.java
public static String encrypt(String Data) throws Exception { Key key = generateKey();/*from ww w .j a va 2s . co m*/ Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); String encryptedValue = new String(Base64.encodeBase64(encVal)); return encryptedValue; }
From source file:com.zf.decipher.DataEn.java
private static byte[] decrypt(byte[] data) throws Exception { if (null == secretKey) { throw new SecurityException("secretKey is null"); }//from w ww. j a va 2s. c om Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, secretKey); return cipher.doFinal(data); }