List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:encrypt.algorithms.AESCBC.java
public String encrypt(String key1, String key2, String value) { try {// ww w .ja va 2 s. c om IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key1.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:Main.java
public static void encrypt(String fileIn, String fileOut, byte key[]) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { // Here you read the cleartext. FileInputStream fis = new FileInputStream(fileIn); // This stream write the encrypted text. This stream will be wrapped by another stream. FileOutputStream fos = new FileOutputStream(fileOut); // Length is 32 bytes //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-256"); key = sha.digest(key);// w w w .j ava 2 s . c om SecretKeySpec sks = new SecretKeySpec(key, "AES"); // Create cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sks); // Wrap the output stream CipherOutputStream cos = new CipherOutputStream(fos, cipher); // Write bytes int b; byte[] d = new byte[8]; while ((b = fis.read(d)) != -1) { cos.write(d, 0, b); } // Flush and close streams. cos.flush(); cos.close(); fis.close(); }
From source file:com.philosophy.LicenseGen.java
public LicenseGen(String text) { license = text;//from w ww .ja v a 2s . com byte[] keyBytes = { 88, 42, 94, 52, 56, 81, 82, 98 }; System.out.println("Original License : " + license); try { SecretKey key = new SecretKeySpec(keyBytes, "DES"); Cipher cipher = Cipher.getInstance("DES"); byte[] data = license.getBytes(); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encrypted = cipher.doFinal(data); System.out.println("Encrypted license : " + new String(encrypted)); //now convert to base64 byte[] base64Enc = Base64.encodeBase64(encrypted); String encodedLicense = new String(base64Enc); System.out.println("Encoded license is : " + encodedLicense); System.out.println("Encoding string length is : " + encodedLicense.length()); } catch (NoSuchAlgorithmException e) { } catch (NoSuchPaddingException e) { } catch (InvalidKeyException e) { } catch (IllegalStateException e) { } catch (IllegalBlockSizeException e) { } catch (BadPaddingException e) { } }
From source file:com.philosophy.LicenseDecoder.java
public LicenseDecoder(String text) { license = text;/* ww w . j a v a2 s .co m*/ byte[] keyBytes = { 88, 42, 94, 52, 56, 81, 82, 98 }; System.out.println("Original License : " + license); try { SecretKey key = new SecretKeySpec(keyBytes, "DES"); Cipher cipher = Cipher.getInstance("DES"); byte[] decoded = Base64.decodeBase64(license.getBytes()); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decrypted = cipher.doFinal(decoded); System.out.println("Decrypted license : " + new String(decrypted)); } catch (NoSuchAlgorithmException e) { } catch (NoSuchPaddingException e) { } catch (InvalidKeyException e) { } catch (IllegalStateException e) { } catch (IllegalBlockSizeException e) { } catch (BadPaddingException e) { } }
From source file:com.agiletec.aps.util.DefaultApsEncrypter.java
public static String encryptString(String plainText) throws ApsSystemException { String encryptedString = null; try {/*from w w w . j a va2 s . c o m*/ Key key = getKey(); Cipher desCipher = Cipher.getInstance(TRIPLE_DES); desCipher.init(Cipher.ENCRYPT_MODE, key); byte[] cleartext = plainText.getBytes(); byte[] ciphertext = desCipher.doFinal(cleartext); encryptedString = new String(Base64.encodeBase64(ciphertext)); } catch (Throwable t) { throw new ApsSystemException("Error detcted while encoding a string", t); } return encryptedString; }
From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java
public static String decrypt(String encryptedValue) throws Exception { Key key = generateKey();/*from www . j a v a 2 s . co m*/ Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = Base64.decodeBase64(encryptedValue); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; }
From source file:info.fcrp.keepitsafe.util.Crypt.java
private static void init() { if (cryptCipher == null || decryptCipher == null) { try {//w ww . j a v a 2s . c om SecretKeySpec keySpec = new SecretKeySpec(password.getBytes(), "AES"); cryptCipher = Cipher.getInstance("AES"); cryptCipher.init(Cipher.ENCRYPT_MODE, keySpec); decryptCipher = Cipher.getInstance("AES"); decryptCipher.init(Cipher.DECRYPT_MODE, keySpec); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } // output = cipher.doFinal(input) // // Cipher c = Cipher.getInstance("AES"); // String plain = "plain"; // byte[] plainBytes = plain.getBytes(); // // c.init(Cipher.ENCRYPT_MODE, k); // c.update(plainBytes); // // byte[] encBytes = c.doFinal(); // String enc = Base64.encodeBase64String(encBytes); // assertNotSame(plain, enc); // // c.init(Cipher.DECRYPT_MODE, k); // c.update(encBytes); // byte[] decBytes = c.doFinal(); // String dec = new String(decBytes); catch (InvalidKeyException e) { throw new RuntimeException(e); } } }
From source file:cr.ac.uia.SistemaGC.utils.AES.java
public static String encrypt(Long cedula, String usuario, String contrasena) { //<editor-fold defaultstate="collapsed" desc="Mtodo para cifrar contraseas"> /*/* ww w . jav a2s .c om*/ * Inspirado en: * http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example */ try { IvParameterSpec iv = new IvParameterSpec(fitString(usuario, 16).getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(fitString(cedula.toString(), 16).getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(contrasena.getBytes()); return Base64.encodeBase64String(encrypted); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } return null; //</editor-fold> }
From source file:com.lecaddyfute.utils.security.AESCrypto.java
public static String decrypt1(String encryptedData) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance(ALGO); keyGen.init(128);/*www . ja va 2s . c om*/ SecretKey key = keyGen.generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.DECRYPT_MODE, key); // byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes()); // byte[] decValue = c.doFinal(decordedValue); byte[] decValue = c.doFinal(encryptedData.getBytes("UTF8")); String decryptedValue = new String(decValue); return decryptedValue; }
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); }//from w w w . ja va 2 s.com return encryptCipher; }