List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
From source file:uploadProcess.java
public static boolean encrypt(File inputFolder, File outputFolder, String fileName, String patientID) { try {//from www. j a v a 2s.c o m String ukey = GetKey.getPatientKey(patientID); File filePath = new File(inputFolder.getAbsolutePath() + File.separator + fileName); FileInputStream fis = new FileInputStream(filePath); File outputFile = new File(outputFolder.getAbsolutePath() + File.separator + fileName); FileOutputStream fos = new FileOutputStream(outputFile); 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(outputFolder, fileName, StoragePath.getDropboxDir() + patientID); DeleteDirectory.delete(outputFolder); return true; } catch (Exception e) { System.out.println("Error: " + e); } return false; }
From source file:uploadProcess.java
public static boolean decrypt(File inputFolder, String fileName, String patientID) { try {//from w ww.j a v a2 s . c om //Download File from Cloud.. DropboxUpload download = new DropboxUpload(); download.downloadFile(fileName, StoragePath.getDropboxDir() + patientID, inputFolder); String ukey = GetKey.getPatientKey(patientID); File inputFile = new File(inputFolder.getAbsolutePath() + File.separator + fileName); FileInputStream fis = new FileInputStream(inputFile); File outputFolder = new File(inputFolder.getAbsolutePath() + File.separator + "temp"); if (!outputFolder.exists()) { outputFolder.mkdir(); } FileOutputStream fos = new FileOutputStream(outputFolder.getAbsolutePath() + File.separator + fileName); byte[] k = ukey.getBytes(); SecretKeySpec key = new SecretKeySpec(k, "AES"); Cipher enc = Cipher.getInstance("AES"); enc.init(Cipher.DECRYPT_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(); return true; } catch (Exception e) { System.out.println("Error: " + e); } return false; }
From source file:Logic.security.java
public static String symmetricEncrypt(String text, String secretKey) { byte[] raw;/*from w ww. j a v a2 s . c o m*/ 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:com.hhi.bigdata.platform.push.client.RegisterUtil.java
/** * <pre>/*from w w w . ja va 2s . c o m*/ * private key ? app name? ? ? return * </pre> * @return * @throws Exception */ private static String getAppNameEnc(PrivateKey privKey, String appName) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privKey); return Base64.encodeBase64String(cipher.doFinal(appName.getBytes())); }
From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java
public static Ticket decodeTicket(String ticket) { byte[] encryptedBytes = Base64.decodeBase64(ticket.getBytes()); try {/* w w w . j a v a 2s.c o m*/ Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); // Extract digest from decryptedBytes (MD5 length is 16 bytes) byte[] digest = Arrays.copyOfRange(decryptedBytes, decryptedBytes.length - 16, decryptedBytes.length); byte[] serializedMap = Arrays.copyOfRange(decryptedBytes, 0, decryptedBytes.length - 16); MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM); if (!Arrays.equals(digest, md.digest(serializedMap))) { throw new DigestException(); } ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedMap)); return (Ticket) ois.readObject(); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException | IOException | ClassNotFoundException | DigestException e) { LOGGER.log(Level.SEVERE, "Error when decoding ticket: " + e.getMessage()); } return null; }
From source file:Main.java
/** * More flexible AES encrypt that doesn't encode * @param key AES key typically 128, 192 or 256 bit * @param iv Initiation Vector//from ww w . ja v a 2s . c o m * @param message in bytes (assumed it's already been decoded) * @return Encrypted cipher text (not encoded) * @throws GeneralSecurityException if something goes wrong during encryption */ public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message) throws GeneralSecurityException { final Cipher cipher = Cipher.getInstance(AES_MODE); //IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = cipher.doFinal(message); log("cipherText", cipherText); return cipherText; }
From source file:net.fender.crypto.CryptoUtil.java
/** * NB: Ciphers are not thread safe. Don't reuse the instance returned by * this method in multiple threads./*from w ww . ja va 2s . co m*/ * * @param keyName * @param mode * @return * @throws GeneralSecurityException */ public static Cipher getSystemPropertyCipher(String keyName, int mode) throws GeneralSecurityException { Key key = getSystemPropertyKey(keyName); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(mode, key); return cipher; }
From source file:uploadProcess.java
public static boolean encrypt(String path, FileItemStream item, String patientID) { try {//from ww w.j a v a 2s . com 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:net.fender.crypto.CryptoUtil.java
/** * @param keyName/*from w ww . j av a2 s. c om*/ * @param plaintext * @return * @throws GeneralSecurityException */ public static String encryptUsingSystemPropertyKey(String keyName, String plaintext) throws GeneralSecurityException { Key key = getSystemPropertyKey(keyName); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedByes = cipher.doFinal(plaintext.getBytes()); String encrypted = new String(Base64.encodeBase64(encryptedByes)); return encrypted; }
From source file:net.fender.crypto.CryptoUtil.java
/** * @param keyName//w ww . j a v a2 s .com * @param base64Encoded * @return * @throws GeneralSecurityException */ public static String decryptUsingSystemPropertyKey(String keyName, String base64Encoded) throws GeneralSecurityException { byte[] encryptedBytes = Base64.decodeBase64(base64Encoded.getBytes()); Key key = getSystemPropertyKey(keyName); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); String decrypted = new String(decryptedBytes); return decrypted; }