List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:com.jsmartframework.web.manager.AuthEncrypter.java
private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception { Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_AUTH_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_AUTH_ENCRYPT_CIPHER, encryptCipher); }/*from w ww . j a va 2 s. c o m*/ return encryptCipher; }
From source file:aajavafx.Kripto.java
public String encrypt(String plainText) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); byte[] byteCipher = cipher.doFinal(plainText.getBytes("UTF-8")); Base64 base64 = new Base64(); String stringToStore = new String(base64.encode(byteCipher)); //byte[] restoredBytes = Base64.decode(stringToStore.getBytes()); return stringToStore; }
From source file:Main.java
private static PKCS8EncodedKeySpec decryptPrivateKey(byte[] encryptedPrivateKey) throws GeneralSecurityException { EncryptedPrivateKeyInfo epkInfo; try {//ww w . j a v a 2 s .c o m epkInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey); } catch (IOException ex) { // Probably not an encrypted key. return null; } char[] password = System.console().readPassword("Password for the private key file: "); SecretKeyFactory skFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName()); Key key = skFactory.generateSecret(new PBEKeySpec(password)); Arrays.fill(password, '\0'); Cipher cipher = Cipher.getInstance(epkInfo.getAlgName()); cipher.init(Cipher.DECRYPT_MODE, key, epkInfo.getAlgParameters()); try { return epkInfo.getKeySpec(cipher); } catch (InvalidKeySpecException ex) { System.err.println("Password may be bad."); throw ex; } }
From source file:org.beangle.security.codec.DESEncrypt.java
public byte[] doEncrypt(byte plainText[]) throws Exception { SecureRandom sr = new SecureRandom(); byte rawKeyData[] = desKey; DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); javax.crypto.SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); cipher.init(1, key, sr);/*www . j a va2s .co m*/ byte data[] = plainText; byte encryptedData[] = cipher.doFinal(data); return encryptedData; }
From source file:com.java.demo.DesDemo.java
public static byte[] Decrypt(byte[] strBytes) { try {/*from www . ja va 2 s .com*/ Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, convertSecretKey); byte[] result = cipher.doFinal(strBytes); return result; } catch (Exception e) { return null; } }
From source file:com.myapp.common.AES4MEncrypt.java
/** * /*from ww w . j av a2 s .com*/ * * @param sSrc * @param sKey KEY * @return * @throws Exception * @author cdduqiang * @date 201443 */ public static String encrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { log.error("Encrypt Key ??"); throw new Exception("Encrypt Key ??"); } byte[] raw = hex2byte(sKey); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] anslBytes = sSrc.getBytes(ENCODING);// string2AnslBytes(sSrc); byte[] encrypted = cipher.doFinal(anslBytes); return byte2hex(encrypted).toUpperCase(); }
From source file:uploadProcess.java
public static boolean encrypt(String path, FileItemStream item, String patientID) { try {// w ww.ja v a 2 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:DesEncrypter.java
DesEncrypter(String passPhrase) throws Exception { int iterationCount = 2; KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:com.aast.encrypt.EncryptManager.java
public static String encryptAES(String key, String value) { try {//w w w . j av a 2 s . c om byte[] keyBytes = getKeyFromString(key); IvParameterSpec iv = new IvParameterSpec(keyBytes); SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "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
private static byte[] doFinal(String key, int opmode, byte[] input) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { key = checkNull(key) ? DEFAULT_KEY : key; if (checkNull(key)) { return null; }/* ww w.j a v a 2s. co m*/ SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(MODE); cipher.init(opmode, securekey, sr); return cipher.doFinal(input); }