List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:Main.java
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); return cipher.doFinal(clear); }
From source file:licenceexecuter.Encryptor.java
public static String encrypt(String key, String initVector, String value) { try {/*from w w w . j ava2 s. c o m*/ 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()); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Logic.security.java
public static String symmetricEncrypt(String text, String secretKey) { byte[] raw;//from w ww.j av a 2s . com String encryptedString; SecretKeySpec skeySpec; byte[] encryptText = text.getBytes(); Cipher cipher; try { raw = Base64.decodeBase64(secretKey); skeySpec = new SecretKeySpec(raw, "AES"); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText)); } catch (Exception e) { e.printStackTrace(); return "Error"; } return encryptedString; }
From source file:Main.java
public static String enCrypto(String txt, String key) { if (txt != null && !"".equals(txt) && !"null".equals(txt)) { try {// w ww.ja v a 2 s .co m StringBuffer sb = new StringBuffer(); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory skeyFactory = null; Cipher cipher = null; try { skeyFactory = SecretKeyFactory.getInstance("DES"); cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } SecretKey deskey = skeyFactory.generateSecret(desKeySpec); cipher.init(Cipher.ENCRYPT_MODE, deskey); byte[] cipherText = cipher.doFinal(txt.getBytes()); for (int n = 0; n < cipherText.length; n++) { String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF)); if (stmp.length() == 1) { sb.append("0" + stmp); } else { sb.append(stmp); } } return sb.toString().toUpperCase(Locale.US); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:Main.java
/** * Decrypt data/*w w w . j a v a2s. com*/ * @param secretKey - a secret key used for decryption * @param data - data to decrypt * @return Decrypted data * @throws Exception */ public static String decipher(String secretKey, String data) throws Exception { // Key has to be of length 8 if (secretKey == null || secretKey.length() != 8) throw new Exception("Invalid key length - 8 bytes key needed!"); SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(toByte(data))); }
From source file:Main.java
private static byte[] Des(byte[] byteData, byte[] byteKey, int opmode) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = null;// w w w. jav a 2 s .com try { SecretKeySpec desKey = new SecretKeySpec(byteKey, "DES"); cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(opmode, desKey); return cipher.doFinal(byteData); } finally { cipher = null; } }
From source file:Main.java
public static byte[] encrypt(String clearText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null;/*from www.ja va 2s . com*/ byte[] cipherText = null; try { // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); cipherText = cipher.doFinal(clearText.getBytes()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } return Base64.encode(cipherText, 10); }
From source file:Main.java
public static String decrypt(byte[] cipherText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null;//from w ww . j a va 2 s . c o m byte[] clearText = null; try { // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); clearText = cipher.doFinal(Base64.decode(new String(cipherText), 10)); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) {// TODO Auto-generated catch block e.printStackTrace(); } return new String(clearText); }
From source file:Main.java
public static String encryption(String string) throws Exception { // TODO Auto-generated method stub //generate symmetric key KeyGenerator keygt = KeyGenerator.getInstance("AES"); keygt.init(128);// w ww . ja v a 2 s . c om SecretKey symkey = keygt.generateKey(); //get it encoded byte[] aes_ba = symkey.getEncoded(); //create cipher SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //encryption byte[] EncSymbyteArray = cipher.doFinal(string.getBytes()); //encrypt symKey with PublicKey // Key pubKey = getPublicKey(); Key pubKey = publicKey; //RSA cipher Cipher cipherAsm = Cipher.getInstance("RSA", "BC"); cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey); //RSA encryption byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba); // File f3 = new File(BASE_PATH,"enc.txt"); // File f3key = new File(BASE_PATH,"enckey.txt"); // File f3file = new File(BASE_PATH,"encfile.txt"); // writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray); //byte != new String //return new String(byteMerger(asymEncsymKey, EncSymbyteArray)); return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT); }
From source file:Main.java
/** * Encrypt data// w w w. j av a2 s. c o m * @param secretKey - a secret key used for encryption * @param data - data to encrypt * @return Encrypted data * @throws Exception */ public static String cipher(String secretKey, String data) throws Exception { // Key has to be of length 8 if (secretKey == null || secretKey.length() != 8) throw new Exception("Invalid key length - 8 bytes key needed!"); SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); return toHex(cipher.doFinal(data.getBytes())); }