List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException
From source file:Main.java
public static String decryptData(String ciphertext, String password) throws Exception { int iterationCount = 100; //because polaroid int keyLength = 256; String[] fields = ciphertext.split("]"); byte[] iv = Base64.decode(fields[0], 0); byte[] salt = Base64.decode(fields[1], 0); byte[] cipherBytes = Base64.decode(fields[2], 0); Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length); KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivParams); byte[] plaintext = cipher.doFinal(cipherBytes); String plainStr = new String(plaintext, "UTF-8"); return plainStr; }
From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java
private static byte[] decryptData(byte[] bInput) { byte[] outBytes = null; try {//from w ww .ja v a2 s . co m Key key = new SecretKeySpec(secret_key, "DESede"); Cipher cipher = Cipher.getInstance("DESede", "SunJCE"); cipher.init(Cipher.DECRYPT_MODE, key, cipher.getParameters()); outBytes = cipher.doFinal(bInput); } catch (Exception ex) { //log.error("PacketUtil",ex.getMessage(), ex); return outBytes; } return outBytes; }
From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java
private static byte[] encryptData(byte[] content) { byte[] outBytes = null; try {/* ww w . j a v a2 s .co m*/ Key key = new SecretKeySpec(secret_key, "DESede"); Cipher cipher = Cipher.getInstance("DESede", "SunJCE"); cipher.init(Cipher.ENCRYPT_MODE, key, cipher.getParameters()); outBytes = cipher.doFinal(content); } catch (Exception ex) { //log.error("PacketUtil",ex.getMessage(), ex); return outBytes; } return outBytes; }
From source file:com.redsqirl.workflow.utils.FileStream.java
private static byte[] encrypt(byte[] plaintext) throws Exception { SecretKey key = generateKey(); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec); return cipher.doFinal(plaintext); }
From source file:com.redsqirl.workflow.utils.FileStream.java
private static byte[] decrypt(byte[] ciphertext) throws Exception { SecretKey key = generateKey(); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec); return cipher.doFinal(ciphertext); }
From source file:org.runway.utils.StringEncryptDecryptUtil.java
public static String encrypt(String property) throws RunwaySecurityException { String result = null;//from w w w . j a v a 2 s. c om SecretKeyFactory keyFactory; try { keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance(ALGORITHM); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); result = base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (NoSuchAlgorithmException e) { throw new RunwaySecurityException(e); } catch (InvalidKeySpecException e) { throw new RunwaySecurityException(e); } catch (NoSuchPaddingException e) { throw new RunwaySecurityException(e); } catch (InvalidKeyException e) { throw new RunwaySecurityException(e); } catch (InvalidAlgorithmParameterException e) { throw new RunwaySecurityException(e); } catch (IllegalBlockSizeException e) { throw new RunwaySecurityException(e); } catch (BadPaddingException e) { throw new RunwaySecurityException(e); } return result; }
From source file:Logi.GSeries.Libraries.Encryption.java
public static String decrypt(String encryptedString, String password) { try {//from w w w . j a va 2s. c o m byte[] encryptedWithIV = Base64.decodeBase64(encryptedString); byte initialVector[] = new byte[16]; byte[] encrypted = new byte[encryptedWithIV.length - initialVector.length]; System.arraycopy(encryptedWithIV, 0, encrypted, 0, encrypted.length); System.arraycopy(encryptedWithIV, encrypted.length, initialVector, 0, initialVector.length); IvParameterSpec ivspec = new IvParameterSpec(initialVector); SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec); byte[] original = cipher.doFinal(encrypted); return new String(original); } catch (Exception ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); return "Error"; } }
From source file:org.runway.utils.StringEncryptDecryptUtil.java
public static String decrypt(String value) throws RunwaySecurityException { String result = null;//w ww . j ava 2 s .com SecretKeyFactory keyFactory; try { keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance(ALGORITHM); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); result = new String(pbeCipher.doFinal(base64Decode(value))); } catch (NoSuchAlgorithmException e) { throw new RunwaySecurityException(e); } catch (InvalidKeySpecException e) { throw new RunwaySecurityException(e); } catch (NoSuchPaddingException e) { throw new RunwaySecurityException(e); } catch (InvalidKeyException e) { throw new RunwaySecurityException(e); } catch (InvalidAlgorithmParameterException e) { throw new RunwaySecurityException(e); } catch (IllegalBlockSizeException e) { throw new RunwaySecurityException(e); } catch (BadPaddingException e) { throw new RunwaySecurityException(e); } catch (IOException e) { throw new RunwaySecurityException(e); } return result; }
From source file:Logi.GSeries.Libraries.Encryption.java
public static String encrypt(String decryptedString, String password) { try {/*from ww w . java 2 s. c om*/ // build the initialization vector (randomly). SecureRandom random = new SecureRandom(); byte initialVector[] = new byte[16]; //generate random 16 byte IV AES is always 16bytes random.nextBytes(initialVector); IvParameterSpec ivspec = new IvParameterSpec(initialVector); SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec); byte[] encrypted = cipher.doFinal(decryptedString.getBytes()); byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length]; System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length); System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length); return Base64.encodeBase64String(encryptedWithIV); } catch (Exception ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); return "Error"; } }
From source file:com.cloud.utils.crypt.RSAHelper.java
public static String encryptWithSSHPublicKey(String sshPublicKey, String content) { String returnString = null;//w ww .j a va 2s .c o m try { RSAPublicKey publicKey = readKey(sshPublicKey); Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME); cipher.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom()); byte[] encrypted = cipher.doFinal(content.getBytes()); returnString = Base64.encodeBase64String(encrypted); } catch (Exception e) { } return returnString; }