List of usage examples for javax.crypto Cipher ENCRYPT_MODE
int ENCRYPT_MODE
To view the source code for javax.crypto Cipher ENCRYPT_MODE.
Click Source Link
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);// ww w. ja v a 2 s. c o m 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.agiletec.aps.util.DefaultApsEncrypter.java
public static String encryptString(String plainText) throws ApsSystemException { String encryptedString = null; try {//from ww w .j a v a 2 s .co 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:info.fcrp.keepitsafe.util.Crypt.java
private static void init() { if (cryptCipher == null || decryptCipher == null) { try {//from www .j ava 2 s .co m 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:DesEncrypter.java
DesEncrypter(SecretKey key) throws Exception { byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A }; AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:com.philosophy.LicenseGen.java
public LicenseGen(String text) { license = text;// ww w .j av a 2s. 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[] 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.kylinolap.rest.security.PasswordPlaceholderConfigurer.java
public static String encrypt(String strToEncrypt) { try {/*www .jav a2 s . c om*/ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes())); return encryptedString; } catch (Exception e) { } return null; }
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"> /*/*w ww.j a va 2s . co m*/ * 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:de.serverfrog.pw.crypt.SerpentUtil.java
public static CipherOutputStream getOutputStream(OutputStream os, Key key) { try {/*from w w w. java2s .co m*/ Cipher instance = Cipher.getInstance("Serpent", "BC"); instance.init(Cipher.ENCRYPT_MODE, key); return new CipherOutputStream(os, instance); } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException ex) { Logger.getLogger(SerpentUtil.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:uploadProcess.java
public static boolean encrypt(String path, FileItemStream item, String patientID) { try {//from w w w. j a v a2 s.c o m File mainFolder = new File(path + File.separator + "Encrypt"); if (!mainFolder.exists()) { mainFolder.mkdir(); } File patientFolder = new File(mainFolder + File.separator + patientID); if (!patientFolder.exists()) { patientFolder.mkdir(); } String ukey = GetKey.getPatientKey(patientID); InputStream fis = item.openStream(); FileOutputStream fos = new FileOutputStream( patientFolder.getAbsolutePath() + File.separator + item.getName()); byte[] k = ukey.getBytes(); SecretKeySpec key = new SecretKeySpec(k, "AES"); System.out.println(key); Cipher enc = Cipher.getInstance("AES"); enc.init(Cipher.ENCRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(fos, enc); byte[] buf = new byte[1024]; int read; while ((read = fis.read(buf)) != -1) { cos.write(buf, 0, read); } fis.close(); fos.flush(); cos.close(); //Upload File to cloud DropboxUpload upload = new DropboxUpload(); upload.uploadFile(patientFolder, item.getName(), StoragePath.getDropboxDir() + patientID); DeleteDirectory.delete(patientFolder); return true; } catch (Exception e) { System.out.println("Error: " + e); } return false; }
From source file:illab.nabal.util.SecurityHelper.java
/** * AES-encrypt a plain message. Return null if message is empty. * /*from ww w .j av a 2 s.c o m*/ * @param message * @return AES encrypted hex * @throws Exception */ public static String cipher(String message) throws Exception { if (StringHelper.isEmpty(message) == false) { Cipher cipher = Cipher.getInstance(AES); cipher.init(Cipher.ENCRYPT_MODE, SYSTEM_SECRET_KEY_SPEC); return Base64.encodeToString(cipher.doFinal(message.getBytes()), Base64.DEFAULT); } else { return null; } }