List of usage examples for javax.crypto Cipher DECRYPT_MODE
int DECRYPT_MODE
To view the source code for javax.crypto Cipher DECRYPT_MODE.
Click Source Link
From source file:uploadProcess.java
public static boolean decrypt(File inputFolder, String fileName, String patientID, String viewKey) { try {//from w w w . ja va 2s.c om String ukey = GetKey.getPatientKey(patientID); if (viewKey.equals(ukey)) { //Download File from Cloud.. DropboxUpload download = new DropboxUpload(); download.downloadFile(fileName, StoragePath.getDropboxDir() + patientID, inputFolder); 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; } else { return false; } } catch (Exception e) { System.out.println("Error: " + e); } return false; }
From source file:ch.cyberduck.core.aquaticprime.DictionaryLicense.java
protected void verify(final NSDictionary dictionary, final String publicKey) throws InvalidLicenseException { if (null == dictionary) { throw new InvalidLicenseException(); }//w ww . j a v a2s. c om final NSData signature = (NSData) dictionary.objectForKey("Signature"); if (null == signature) { log.warn(String.format("Missing key 'Signature' in dictionary %s", dictionary)); throw new InvalidLicenseException(); } // Append all values StringBuilder values = new StringBuilder(); final ArrayList<String> keys = new ArrayList<>(dictionary.keySet()); // Sort lexicographically by key Collections.sort(keys, new NaturalOrderComparator()); for (String key : keys) { if ("Signature".equals(key)) { continue; } values.append(dictionary.objectForKey(key).toString()); } byte[] signaturebytes = signature.bytes(); byte[] plainbytes = values.toString().getBytes(Charset.forName("UTF-8")); try { final BigInteger modulus = new BigInteger(StringUtils.removeStart(publicKey, "0x"), 16); final BigInteger exponent = new BigInteger(Base64.decodeBase64("Aw==")); final KeySpec spec = new RSAPublicKeySpec(modulus, exponent); final PublicKey rsa = KeyFactory.getInstance("RSA").generatePublic(spec); final Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); rsaCipher.init(Cipher.DECRYPT_MODE, rsa); final MessageDigest sha1Digest = MessageDigest.getInstance("SHA1"); if (!Arrays.equals(rsaCipher.doFinal(signaturebytes), sha1Digest.digest(plainbytes))) { throw new InvalidLicenseException(); } } catch (NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException e) { log.warn(String.format("Signature verification failure for key %s", file)); throw new InvalidLicenseException(); } if (log.isInfoEnabled()) { log.info(String.format("Valid key in %s", file)); } }
From source file:corner.util.crypto.Cryptor.java
/** * ?IO?.IO?,.//from w w w. j a va 2 s . c om * @param inputFileName ????. * @param keyFile ??. * @return ?IO?. */ public static InputStream dencryptFileIO(String inputFileName, String keyFile) { if (keyFile == null) { try { return new FileInputStream(new File(inputFileName)); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } SecretKey key = null; ObjectInputStream keyis; try { keyis = new ObjectInputStream(new FileInputStream(keyFile)); key = (SecretKey) keyis.readObject(); keyis.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } //keyCipher Cipher cipher = null; try { //, cipher = Cipher.getInstance("DES"); // ? cipher.init(Cipher.DECRYPT_MODE, key); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } File file = new File(inputFileName); try { //? CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(file)), cipher); return in; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * //from www. j a va2s .com * * @param data * ? * @param key * * @return byte[] ? */ public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception { // KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? // ??? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); // PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, pubKey); return cipher.doFinal(data); }
From source file:hudson.util.Secret.java
/** * Reverse operation of {@link #getEncryptedValue()}. Returns null * if the given cipher text was invalid. *//*from w ww. ja va2 s. c om*/ public static Secret decrypt(String data) { if (data == null) return null; try { byte[] in = Base64.decode(data.toCharArray()); Secret s = tryDecrypt(KEY.decrypt(), in); if (s != null) return s; // try our historical key for backward compatibility Cipher cipher = getCipher("AES"); cipher.init(Cipher.DECRYPT_MODE, getLegacyKey()); return tryDecrypt(cipher, in); } catch (GeneralSecurityException e) { return null; } catch (UnsupportedEncodingException e) { throw new Error(e); // impossible } catch (IOException e) { return null; } }
From source file:dz.alkhwarizmix.framework.java.utils.CryptoUtil.java
/** * decryptString/* w w w .j a v a 2s.c om*/ */ public final String decryptString(String hexStringToDecrypt) { String result = null; byte[] dataToDecrypt = null; byte[] decrypted = null; dataToDecrypt = hex2Byte(hexStringToDecrypt); try { cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); decrypted = cipher.doFinal(dataToDecrypt); result = hexToString(byte2hex(decrypted)); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return result; }
From source file:ch.cyberduck.core.aquaticprime.DonationKey.java
/** * @return True if valid license key/*from ww w. ja v a 2 s .co m*/ */ @Override public boolean verify() { if (null == dictionary) { return false; } final NSData signature = (NSData) dictionary.objectForKey("Signature"); if (null == signature) { log.warn(String.format("Missing key 'Signature' in dictionary %s", dictionary)); return false; } // Append all values StringBuilder values = new StringBuilder(); final ArrayList<String> keys = new ArrayList<>(dictionary.keySet()); // Sort lexicographically by key Collections.sort(keys, new NaturalOrderComparator()); for (String key : keys) { if ("Signature".equals(key)) { continue; } values.append(dictionary.objectForKey(key).toString()); } byte[] signaturebytes = signature.bytes(); byte[] plainbytes = values.toString().getBytes(Charset.forName("UTF-8")); final boolean valid; try { final BigInteger modulus = new BigInteger(StringUtils.removeStart(this.getPublicKey(), "0x"), 16); final BigInteger exponent = new BigInteger(Base64.decodeBase64("Aw==")); final KeySpec spec = new RSAPublicKeySpec(modulus, exponent); final PublicKey rsa = KeyFactory.getInstance("RSA").generatePublic(spec); final Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); rsaCipher.init(Cipher.DECRYPT_MODE, rsa); final MessageDigest sha1Digest = MessageDigest.getInstance("SHA1"); valid = Arrays.equals(rsaCipher.doFinal(signaturebytes), sha1Digest.digest(plainbytes)); } catch (NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException e) { log.warn(String.format("Signature verification failure for key %s", file)); return false; } if (valid) { if (log.isInfoEnabled()) { log.info(String.format("Valid key in %s", file)); } } else { log.warn(String.format("Not a valid key in %s", file)); } return valid; }
From source file:com.predic8.membrane.core.interceptor.balancer.ClusterNotificationInterceptor.java
private Map<String, String> getDecryptedParams(String data) throws Exception { Cipher cipher = Cipher.getInstance("AES"); SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(keyHex.toCharArray()), "AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); return parseQueryString(new String(cipher.doFinal(Base64.decodeBase64(data.getBytes("UTF-8"))), "UTF-8")); }
From source file:org.runway.utils.StringEncryptDecryptUtil.java
public static String decrypt(String value) throws RunwaySecurityException { String result = null;/*from ww w .j av a2 s .co m*/ SecretKeyFactory keyFactory; try { keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance(ALGORITHM); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); result = new String(pbeCipher.doFinal(base64Decode(value))); } catch (NoSuchAlgorithmException e) { throw new RunwaySecurityException(e); } catch (InvalidKeySpecException e) { throw new RunwaySecurityException(e); } catch (NoSuchPaddingException e) { throw new RunwaySecurityException(e); } catch (InvalidKeyException e) { throw new RunwaySecurityException(e); } catch (InvalidAlgorithmParameterException e) { throw new RunwaySecurityException(e); } catch (IllegalBlockSizeException e) { throw new RunwaySecurityException(e); } catch (BadPaddingException e) { throw new RunwaySecurityException(e); } catch (IOException e) { throw new RunwaySecurityException(e); } return result; }
From source file:CipherProvider.java
/** * Cipher setuped for decryption// w w w. ja va 2s . c o m * * @return Cipher setuped for decryption */ Cipher getDecryptCipher() { if (StringUtils.isNotBlank(password) && (decryptCipher == null)) { decryptCipher = getCipher(password, Cipher.DECRYPT_MODE); } return decryptCipher; }